using JSMR.Application.VoiceWorks.Queries.Search;
using JSMR.UI.Blazor.Enums;

namespace JSMR.UI.Blazor.Services;

public static class ImageUrlProvider
{
    public static string GetImageUrl(VoiceWorkSearchResult voiceWork, string size, string extension = "jpg")
    {
        return GetImageURL(voiceWork.OriginalProductId ?? voiceWork.ProductId, voiceWork.HasImage, voiceWork.SalesDate, size, extension);
    }

    public static string GetImageUrl(VoiceWorkSearchResult voiceWork, ImageSize imageSize, ImageExtension imageExtension)
    {
        return GetImageUrl(voiceWork.OriginalProductId ?? voiceWork.ProductId, voiceWork.HasImage, voiceWork.SalesDate.HasValue, imageSize, imageExtension);
    }

    public static string GetImageURL(string? productId, bool hasImage, DateTime? salesDate, string size, string extension = "jpg")
    {
        string folder = "modpub";
        string imageSize = "main";

        string imageWorkType = productId != null ? productId.StartsWith("RJ") ? "doujin" : "professional" : "doujin";

        switch (size)
        {
            case "small":
                imageSize = "sam";
                folder = "modpub";
                break;
            case "300x300":
                imageSize = hasImage ? "main_300x300" : "main";
                folder = "resize";
                break;
            case "240x":
                //imageSize = hasImage ? imageWorkType == "doujin" ? "main_240x240" : "sam_170x" : "main";
                imageSize = hasImage ? imageWorkType == "doujin" ? "main_240x240" : "main_240x240" : "main";
                folder = "resize";
                break;
            case "main":
            default:
                imageSize = "main";
                folder = "modpub";
                break;
        }

        if (hasImage == false || productId == null)
        {
            string noImageUrlTemplate = "/images/web/home/no_img_[imageSize].gif";
            string noImageUrl = noImageUrlTemplate.Replace("[imageSize]", imageSize);

            return noImageUrl;
        }

        var imageUrlTemplate = $"//img.dlsite.jp/[folder]/images2/[imageType1]/[imageWorkType]/[fullRoundedProductId]/[productId][imageType2]_img_[imageSize].{extension}";

        string productIdWithoutPrefixString = productId.Substring(2);
        int productIdWithoutPrefix = Convert.ToInt32(productId.Substring(2));

        string productIdPrefix = productId.Substring(0, 2);

        int roundedProductId = (int)Math.Round(Math.Ceiling((double)productIdWithoutPrefix / 1000) * 1000);

        int productIdWithPrefixStringLength = productIdWithoutPrefixString.Length;
        int zeroPadLength = productIdWithPrefixStringLength - roundedProductId.ToString().Length;

        var fullRoundedProductId = productIdPrefix.PadRight(productIdPrefix.Length + zeroPadLength, '0') + roundedProductId;

        bool hasSalesDate = salesDate.HasValue;

        string imageType1 = hasSalesDate ? "work" : "ana";
        string imageType2 = hasSalesDate ? "" : "_ana";

        string productLinkPage = salesDate.HasValue ? "work" : "announce";

        string imageUrl = imageUrlTemplate
            .Replace("[folder]", folder)
            .Replace("[imageType1]", imageType1)
            .Replace("[imageWorkType]", imageWorkType)
            .Replace("[fullRoundedProductId]", fullRoundedProductId)
            .Replace("[productId]", productId)
            .Replace("[imageType2]", imageType2)
            .Replace("[imageSize]", imageSize);

        return imageUrl;
    }

    public static string GetImageUrl(string? productId, bool hasImage, bool isOnSale, ImageSize size, ImageExtension extension)
    {
        string imageWorkType = productId != null ? productId.StartsWith("RJ") ? "doujin" : "professional" : "doujin";
        (string imageSize, string folder) = GetImageSizeAndFolder(size, hasImage);

        if (hasImage == false || productId == null)
        {
            return $"/images/web/home/no_img_{imageSize}.gif";
        }

        string productIdWithoutPrefixString = productId.Substring(2);
        int productIdWithoutPrefix = Convert.ToInt32(productId.Substring(2));

        string productIdPrefix = productId[..2];

        int roundedProductId = (int)Math.Round(Math.Ceiling((double)productIdWithoutPrefix / 1000) * 1000);

        int productIdWithPrefixStringLength = productIdWithoutPrefixString.Length;
        int zeroPadLength = productIdWithPrefixStringLength - roundedProductId.ToString().Length;

        var fullRoundedProductId = productIdPrefix.PadRight(productIdPrefix.Length + zeroPadLength, '0') + roundedProductId;

        string imageType1 = isOnSale ? "work" : "ana";
        string imageType2 = isOnSale ? "" : "_ana";

        return $"//img.dlsite.jp/{folder}/images2/{imageType1}/{imageWorkType}/{fullRoundedProductId}/{productId}{imageType2}_img_{imageSize}.{extension}";
    }

    private static (string, string) GetImageSizeAndFolder(ImageSize imageSize, bool hasImage)
    {
        return imageSize switch
        {
            ImageSize.Thumb100 => ("sam", "modpub"),
            ImageSize.Square240 => (hasImage ? "main_240x240" : "main", "resize"),
            ImageSize.Square300 => (hasImage ? "main_300x300" : "main", "resize"),
            _ => ("main", "modpub"),
        };
    }
}