using Meezi.API.Models.Cafes;
using Meezi.Core.Branding;
using Meezi.Infrastructure.Branding;

namespace Meezi.API.Services;

public static class CafeThemeMapping
{
    public static CafeThemeDto ToDto(CafeTheme theme) => new(
        theme.PaletteId,
        theme.PanelStyle,
        theme.MenuStyle,
        theme.MenuTexture,
        theme.Density,
        theme.Radius,
        theme.Custom is null
            ? null
            : new CafeThemeCustomColorsDto(
                theme.Custom.Primary,
                theme.Custom.Secondary,
                theme.Custom.Accent,
                theme.Custom.Background,
                theme.Custom.Surface,
                theme.Custom.Text,
                theme.Custom.TextMuted,
                theme.Custom.Destructive,
                theme.Custom.Success,
                theme.Custom.PrimaryOpacity,
                theme.Custom.SecondaryOpacity,
                theme.Custom.AccentOpacity,
                theme.Custom.BackgroundOpacity,
                theme.Custom.SurfaceOpacity,
                theme.Custom.TextOpacity,
                theme.Custom.TextMutedOpacity,
                theme.Custom.DestructiveOpacity,
                theme.Custom.SuccessOpacity));

    public static CafeTheme FromDto(CafeThemeDto dto)
    {
        var theme = new CafeTheme
        {
            PaletteId = dto.PaletteId,
            PanelStyle = dto.PanelStyle,
            MenuStyle = dto.MenuStyle,
            MenuTexture = dto.MenuTexture,
            Density = dto.Density,
            Radius = dto.Radius,
            Custom = dto.Custom is null
                ? null
                : new CafeThemeCustomColors
                {
                    Primary = dto.Custom.Primary,
                    Secondary = dto.Custom.Secondary,
                    Accent = dto.Custom.Accent,
                    Background = dto.Custom.Background,
                    Surface = dto.Custom.Surface,
                    Text = dto.Custom.Text,
                    TextMuted = dto.Custom.TextMuted,
                    Destructive = dto.Custom.Destructive,
                    Success = dto.Custom.Success,
                    PrimaryOpacity = dto.Custom.PrimaryOpacity,
                    SecondaryOpacity = dto.Custom.SecondaryOpacity,
                    AccentOpacity = dto.Custom.AccentOpacity,
                    BackgroundOpacity = dto.Custom.BackgroundOpacity,
                    SurfaceOpacity = dto.Custom.SurfaceOpacity,
                    TextOpacity = dto.Custom.TextOpacity,
                    TextMutedOpacity = dto.Custom.TextMutedOpacity,
                    DestructiveOpacity = dto.Custom.DestructiveOpacity,
                    SuccessOpacity = dto.Custom.SuccessOpacity
                }
        };
        return CafeThemeSerializer.Normalize(theme);
    }

    public static CafeThemeDto FromJson(string? json) => ToDto(CafeThemeSerializer.Parse(json));
}
