using System.Windows;
using System.Windows.Media;

namespace AxCopilot.Views;

/// <summary>
/// Phase 33-E: 테마 리소스 헬퍼.
/// <c>TryFindResource("XXX") as Brush ?? Brushes.YYY</c> 패턴이
/// ChatWindow.xaml.cs에 176회 이상 반복되는 것을 단일 메서드로 통합합니다.
///
/// 사용법:
/// <code>
/// var fg = ThemeResourceHelper.Brush(this, "PrimaryText", Brushes.White);
/// var bg = ThemeResourceHelper.Brush(this, "LauncherBackground");
/// </code>
/// </summary>
public static class ThemeResourceHelper
{
    // ─── 사전 정의 리소스 키 (매직 스트링 방지) ──────────────────

    public const string PrimaryText = "PrimaryText";
    public const string SecondaryText = "SecondaryText";
    public const string AccentColor = "AccentColor";
    public const string LauncherBackground = "LauncherBackground";
    public const string ItemBackground = "ItemBackground";
    public const string ItemHoverBackground = "ItemHoverBackground";
    public const string BorderColor = "BorderColor";
    public const string HintBackground = "HintBackground";
    public const string HintText = "HintText";
    public const string SeparatorColor = "SeparatorColor";

    /// <summary>
    /// 테마 리소스에서 Brush를 조회합니다.
    /// 리소스가 없으면 <paramref name="fallback"/>을 반환합니다.
    /// </summary>
    public static Brush Brush(FrameworkElement element, string key, Brush? fallback = null)
        => element.TryFindResource(key) as Brush ?? fallback ?? Brushes.Transparent;

    /// <summary>Brush 오버로드 — Application 수준 리소스 조회.</summary>
    public static Brush Brush(string key, Brush? fallback = null)
        => Application.Current?.TryFindResource(key) as Brush ?? fallback ?? Brushes.Transparent;

    /// <summary>PrimaryText 색상을 반환합니다.</summary>
    public static Brush Primary(FrameworkElement el)
        => Brush(el, PrimaryText, Brushes.White);

    /// <summary>SecondaryText 색상을 반환합니다.</summary>
    public static Brush Secondary(FrameworkElement el)
        => Brush(el, SecondaryText, Brushes.Gray);

    /// <summary>AccentColor 색상을 반환합니다.</summary>
    public static Brush Accent(FrameworkElement el)
        => Brush(el, AccentColor, Brushes.DodgerBlue);

    /// <summary>LauncherBackground 색상을 반환합니다.</summary>
    public static Brush Background(FrameworkElement el)
        => Brush(el, LauncherBackground, Brushes.Black);

    /// <summary>ItemBackground 색상을 반환합니다.</summary>
    public static Brush ItemBg(FrameworkElement el)
        => Brush(el, ItemBackground, Brushes.DarkGray);

    /// <summary>ItemHoverBackground 색상을 반환합니다.</summary>
    public static Brush HoverBg(FrameworkElement el)
        => Brush(el, ItemHoverBackground, Brushes.DimGray);

    /// <summary>BorderColor 색상을 반환합니다.</summary>
    public static Brush Border(FrameworkElement el)
        => Brush(el, BorderColor, Brushes.Gray);

    /// <summary>HintBackground 색상을 반환합니다.</summary>
    public static Brush Hint(FrameworkElement el)
        => Brush(el, HintBackground, Brushes.DarkGray);

    /// <summary>HintText 색상을 반환합니다.</summary>
    public static Brush HintFg(FrameworkElement el)
        => Brush(el, HintText, Brushes.DarkGray);

    /// <summary>SeparatorColor 색상을 반환합니다.</summary>
    public static Brush Separator(FrameworkElement el)
        => Brush(el, SeparatorColor, Brushes.Gray);

    /// <summary>
    /// Segoe MDL2 Assets 글리프 TextBlock을 생성합니다.
    /// ChatWindow에서 50+ 회 반복되는 패턴을 통합합니다.
    /// </summary>
    public static System.Windows.Controls.TextBlock Icon(
        string glyph, double fontSize = 14,
        Brush? foreground = null, VerticalAlignment vAlign = VerticalAlignment.Center)
    {
        return new System.Windows.Controls.TextBlock
        {
            Text = glyph,
            FontFamily = SegoeMdl2,
            FontSize = fontSize,
            Foreground = foreground ?? Brushes.White,
            VerticalAlignment = vAlign,
        };
    }

    /// <summary>
    /// HEX 문자열을 SolidColorBrush로 변환합니다.
    /// <c>new SolidColorBrush((Color)ColorConverter.ConvertFromString(hex))</c> 패턴을 통합합니다.
    /// </summary>
    public static SolidColorBrush HexBrush(string hex)
        => new((Color)ColorConverter.ConvertFromString(hex));

    /// <summary>
    /// HEX 문자열을 Color로 변환합니다.
    /// </summary>
    public static Color HexColor(string hex)
        => (Color)ColorConverter.ConvertFromString(hex);

    /// <summary>Segoe MDL2 Assets FontFamily (캐시됨).</summary>
    public static readonly FontFamily SegoeMdl2 = new("Segoe MDL2 Assets");

    /// <summary>Consolas FontFamily (캐시됨).</summary>
    public static readonly FontFamily Consolas = new("Consolas");

    /// <summary>Cascadia Code → Consolas → monospace 폴백 체인 (코드 블록용, 캐시됨).</summary>
    public static readonly FontFamily CascadiaCode = new("Cascadia Code, Consolas, monospace");

    /// <summary>Consolas → Cascadia Code → Segoe UI 폴백 체인 (인라인 코드용, 캐시됨).</summary>
    public static readonly FontFamily ConsolasCode = new("Consolas, Cascadia Code, Segoe UI");

    /// <summary>Consolas → Courier New 폴백 체인 (터미널 스타일, 캐시됨).</summary>
    public static readonly FontFamily ConsolasCourierNew = new("Consolas, Courier New");
}
