using System;
using System.Collections.Generic;
using System.Globalization;
using System.Windows.Data;
using System.Windows.Media;

namespace MarcusLaw.OutlookAddin.UI.Converters
{
    /// <summary>
    /// Maps EspoCRM entity type names (English, server-side) to short
    /// Hebrew labels for display in group headers and chips.
    /// </summary>
    public sealed class EntityTypeToHebrewConverter : IValueConverter
    {
        private static readonly Dictionary<string, string> Map =
            new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase)
            {
                ["Case"] = "תיק",
                ["Contact"] = "איש קשר",
                ["Account"] = "לקוח",
                ["Lead"] = "ליד",
                ["Opportunity"] = "הזדמנות",
                ["Document"] = "מסמך",
                ["Task"] = "משימה",
                ["Meeting"] = "פגישה",
                ["Call"] = "שיחה"
            };

        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            var s = value as string ?? string.Empty;
            return Map.TryGetValue(s, out var hebrew) ? hebrew : s;
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
            => throw new NotSupportedException();
    }

    /// <summary>
    /// Picks an accent brush per entity type — used for the colored stripe /
    /// chip in the result groups. Falls back to a neutral gray.
    /// </summary>
    public sealed class EntityTypeToBrushConverter : IValueConverter
    {
        private static readonly Dictionary<string, Brush> Map =
            new Dictionary<string, Brush>(StringComparer.OrdinalIgnoreCase)
            {
                ["Case"] = FromHex("#3B82F6"),         // blue
                ["Contact"] = FromHex("#10B981"),      // emerald
                ["Account"] = FromHex("#8B5CF6"),      // violet
                ["Lead"] = FromHex("#F59E0B"),         // amber
                ["Opportunity"] = FromHex("#EC4899"),  // pink
            };

        private static readonly Brush Default = FromHex("#9CA3AF"); // gray-400

        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            var s = value as string ?? string.Empty;
            return Map.TryGetValue(s, out var brush) ? brush : Default;
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
            => throw new NotSupportedException();

        private static SolidColorBrush FromHex(string hex)
        {
            var brush = (SolidColorBrush)new BrushConverter().ConvertFromString(hex)!;
            brush.Freeze();
            return brush;
        }
    }

    /// <summary>
    /// Maps EspoCRM Case status values (English) to Hebrew labels. Unknown
    /// values pass through unchanged so installs with custom statuses
    /// still render correctly.
    /// </summary>
    public sealed class CaseStatusToHebrewConverter : IValueConverter
    {
        private static readonly Dictionary<string, string> Map =
            new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase)
            {
                ["New"] = "חדש",
                ["Assigned"] = "הוקצה",
                ["Pending"] = "ממתין",
                ["PendingHearing"] = "ממתין לדיון",
                ["Pending Hearing"] = "ממתין לדיון",
                ["PendingDecision"] = "ממתין להחלטה",
                ["Pending Decision"] = "ממתין להחלטה",
                ["In Progress"] = "בטיפול",
                ["InProgress"] = "בטיפול",
                ["Open"] = "פתוח",
                ["Closed"] = "סגור",
                ["Closed Lost"] = "סגור — אבד",
                ["Closed Won"] = "סגור — נצח",
                ["Rejected"] = "נדחה",
                ["Duplicate"] = "כפילות",
                ["On Hold"] = "מושהה",
                ["OnHold"] = "מושהה",
                ["Resolved"] = "טופל",
                ["Cancelled"] = "בוטל",
                ["Canceled"] = "בוטל"
            };

        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            var s = value as string;
            if (string.IsNullOrWhiteSpace(s)) return string.Empty;
            return Map.TryGetValue(s!, out var hebrew) ? hebrew : s!;
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
            => throw new NotSupportedException();
    }

    /// <summary>
    /// Returns a small emoji marker per entity type to use next to the
    /// Hebrew label in group headers.
    /// </summary>
    public sealed class EntityTypeToIconConverter : IValueConverter
    {
        private static readonly Dictionary<string, string> Map =
            new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase)
            {
                ["Case"] = "📁",
                ["Contact"] = "👤",
                ["Account"] = "🏢",
                ["Lead"] = "✨",
                ["Opportunity"] = "💼",
            };

        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            var s = value as string ?? string.Empty;
            return Map.TryGetValue(s, out var icon) ? icon : "•";
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
            => throw new NotSupportedException();
    }
}
