using ImageMagick;
using PosterMaker.Interfaces.Services;
using PosterMaker.Models;

namespace PosterMaker.Services;

public class CollectionPosterMakerService : ICollectionPosterMakerService
{
    private static readonly string RefDir = Path.Combine("Resources", "Collections");
    private static readonly string RefDirectory = Path.Combine(Directory.GetCurrentDirectory(), RefDir);
    private static readonly string TitleFont = Path.Combine(RefDirectory, "Arial Bold.ttf");
    private static readonly string CollectionFont = Path.Combine(RefDirectory, "MyriadRegular.ttf");
    private static readonly string Gradient = Path.Combine(RefDirectory, "gradient.png");
    private static readonly string Frame = Path.Combine(RefDirectory, "frame.png");
    
    private const string FontColor = "white";
    private const float FontSize = 1.0F;
    
    public CollectionPosterMakerService()
    {
        
    }
    
    public byte[] CreatePoster(string file, ImageProperties imageProperties)
    {
        using var image = new MagickImage(file);
        var resizeGeometry = new MagickGeometry(1892, 2892)
        {
            FillArea = true
        };
        image.Resize(resizeGeometry);
        image.Extent(2000, 3000, Gravity.Center);

        ApplyGradient(image);
        using var frame = new MagickImage(Frame);
        frame.BackgroundColor = new MagickColor("transparent");
        image.Composite(frame, Gravity.Center, CompositeOperator.Over);
      
        //AddLogo(image, imageProperties.Logo, imageProperties.LogoOption);
        AddTitle(image, imageProperties);
        image.Resize(new Percentage(50));
        return image.ToByteArray();
    }
    
    private static void ApplyGradient(MagickImage image)
    {
        using var gradient = new MagickImage(Gradient);
        image.Composite(gradient, Gravity.Center, CompositeOperator.Multiply);
    }

    private void AddTitle(MagickImage image, ImageProperties properties)
    {
        if (string.IsNullOrEmpty(properties.Title))
            return;

        var readSettings = new MagickReadSettings
        {
            BackgroundColor = MagickColors.Transparent,
            FillColor = new MagickColor(FontColor),
            Font = TitleFont,
            FontPointsize = 180 * FontSize,
            TextGravity = Gravity.Center,
            TextKerning = 2
        };

        var labelImages = new MagickImageCollection();
        if (!string.IsNullOrEmpty(properties.Title))
        {
            labelImages.Add(new MagickImage($"label:{properties.Title.ToUpper()}", readSettings));
        }
        
        if (!string.IsNullOrEmpty(properties.Title2))
        {
            labelImages.Add(new MagickImage($"label:{properties.Title2.ToUpper()}", readSettings));
        }
        readSettings.FontPointsize = 70;
        readSettings.TextKerning = 50;
        readSettings.Font = CollectionFont;
        
        labelImages.Add(new MagickImage($"label:COLLECTION", readSettings));
        using var labels = labelImages.SmushVertical(44);
        image.Composite(labels, Gravity.South, 0, 266, CompositeOperator.Atop);
    }
}