using System.Windows;
using System.Windows.Controls;
using System.Windows.Controls.Primitives;
using System.Windows.Input;
using System.Windows.Media;
using AxCopilot.Models;
using AxCopilot.Services;
using AxCopilot.Services.Agent;

namespace AxCopilot.Views;

public partial class ChatWindow
{
    // ─── 커스텀 프리셋 관리 ─────────────────────────────────────────────

    /// <summary>커스텀 프리셋 추가 다이얼로그를 표시합니다.</summary>
    private void ShowCustomPresetDialog(Models.CustomPresetEntry? existing = null)
    {
        bool isEdit = existing != null;
        var dlg = new CustomPresetDialog(
            existingName: existing?.Label ?? "",
            existingDesc: existing?.Description ?? "",
            existingPrompt: existing?.SystemPrompt ?? "",
            existingColor: existing?.Color ?? "#6366F1",
            existingSymbol: existing?.Symbol ?? "\uE713",
            existingTab: existing?.Tab ?? _activeTab)
        {
            Owner = this,
        };

        if (dlg.ShowDialog() == true)
        {
            if (isEdit)
            {
                existing!.Label = dlg.PresetName;
                existing.Description = dlg.PresetDescription;
                existing.SystemPrompt = dlg.PresetSystemPrompt;
                existing.Color = dlg.PresetColor;
                existing.Symbol = dlg.PresetSymbol;
                existing.Tab = dlg.PresetTab;
            }
            else
            {
                Llm.CustomPresets.Add(new Models.CustomPresetEntry
                {
                    Label = dlg.PresetName,
                    Description = dlg.PresetDescription,
                    SystemPrompt = dlg.PresetSystemPrompt,
                    Color = dlg.PresetColor,
                    Symbol = dlg.PresetSymbol,
                    Tab = dlg.PresetTab,
                });
            }
            _settings.Save();
            BuildTopicButtons();
        }
    }

    /// <summary>커스텀 프리셋 우클릭 컨텍스트 메뉴를 표시합니다.</summary>
    private void ShowCustomPresetContextMenu(Border? anchor, Services.TopicPreset preset)
    {
        if (anchor == null || preset.CustomId == null) return;

        var popup = new System.Windows.Controls.Primitives.Popup
        {
            PlacementTarget = anchor,
            Placement = System.Windows.Controls.Primitives.PlacementMode.Bottom,
            StaysOpen = false,
            AllowsTransparency = true,
        };

        var menuBg = ThemeResourceHelper.Background(this);
        var primaryText = ThemeResourceHelper.Primary(this);
        var secondaryText = ThemeResourceHelper.Secondary(this);
        var borderBrush = ThemeResourceHelper.Border(this);

        var menuBorder = new Border
        {
            Background = menuBg,
            CornerRadius = new CornerRadius(10),
            BorderBrush = borderBrush,
            BorderThickness = new Thickness(1),
            Padding = new Thickness(4),
            MinWidth = 120,
            Effect = new System.Windows.Media.Effects.DropShadowEffect
            {
                BlurRadius = 12, ShadowDepth = 2, Opacity = 0.3, Color = Colors.Black,
            },
        };

        var stack = new StackPanel();

        // 편집 버튼
        var editItem = CreateContextMenuItem("\uE70F", "편집", primaryText, secondaryText);
        editItem.MouseLeftButtonDown += (_, _) =>
        {
            popup.IsOpen = false;
            var entry = Llm.CustomPresets.FirstOrDefault(c => c.Id == preset.CustomId);
            if (entry != null) ShowCustomPresetDialog(entry);
        };
        stack.Children.Add(editItem);

        // 삭제 버튼
        var deleteItem = CreateContextMenuItem("\uE74D", "삭제", new SolidColorBrush(Color.FromRgb(0xEF, 0x44, 0x44)), secondaryText);
        deleteItem.MouseLeftButtonDown += (_, _) =>
        {
            popup.IsOpen = false;
            var result = CustomMessageBox.Show(
                $"'{preset.Label}' 프리셋을 삭제하시겠습니까?",
                "프리셋 삭제", MessageBoxButton.YesNo, MessageBoxImage.Question);
            if (result == MessageBoxResult.Yes)
            {
                Llm.CustomPresets.RemoveAll(c => c.Id == preset.CustomId);
                _settings.Save();
                BuildTopicButtons();
            }
        };
        stack.Children.Add(deleteItem);

        menuBorder.Child = stack;
        popup.Child = menuBorder;
        popup.IsOpen = true;
    }

    /// <summary>컨텍스트 메뉴 항목을 생성합니다.</summary>
    private Border CreateContextMenuItem(string icon, string label, Brush fg, Brush secondaryFg)
    {
        var item = new Border
        {
            Background = Brushes.Transparent,
            CornerRadius = new CornerRadius(6),
            Padding = new Thickness(10, 6, 14, 6),
            Cursor = Cursors.Hand,
        };

        var sp = new StackPanel { Orientation = Orientation.Horizontal };
        sp.Children.Add(new TextBlock
        {
            Text = icon,
            FontFamily = ThemeResourceHelper.SegoeMdl2,
            FontSize = 13, Foreground = fg,
            VerticalAlignment = VerticalAlignment.Center,
            Margin = new Thickness(0, 0, 8, 0),
        });
        sp.Children.Add(new TextBlock
        {
            Text = label, FontSize = 13, Foreground = fg,
            VerticalAlignment = VerticalAlignment.Center,
        });
        item.Child = sp;

        var hoverBg = ThemeResourceHelper.HoverBg(this);
        item.MouseEnter += (s, _) => { if (s is Border b) b.Background = hoverBg; };
        item.MouseLeave += (s, _) => { if (s is Border b) b.Background = Brushes.Transparent; };

        return item;
    }

    /// <summary>현재 탭의 커스텀 프리셋 목록 팝업을 표시합니다.</summary>
    internal void ShowPresetManagePopup()
    {
        var customPresets = Llm.CustomPresets
            .Where(c => c.Tab.Equals(_activeTab, StringComparison.OrdinalIgnoreCase))
            .ToList();

        var primaryText   = ThemeResourceHelper.Primary(this);
        var secondaryText = ThemeResourceHelper.Secondary(this);
        var menuBg        = ThemeResourceHelper.Background(this);
        var borderBrush   = ThemeResourceHelper.Border(this);
        var hoverBg       = ThemeResourceHelper.HoverBg(this);

        var popup = new Popup
        {
            PlacementTarget = SettingsPanel,
            Placement       = PlacementMode.Left,
            VerticalOffset  = -40,
            StaysOpen       = false,
            AllowsTransparency = true,
        };

        var outer = new Border
        {
            Background      = menuBg,
            CornerRadius    = new CornerRadius(12),
            BorderBrush     = borderBrush,
            BorderThickness = new Thickness(1),
            Padding         = new Thickness(4),
            MinWidth        = 230,
            MaxHeight       = 340,
            Effect = new System.Windows.Media.Effects.DropShadowEffect
            {
                BlurRadius = 16, ShadowDepth = 2, Opacity = 0.35, Color = Colors.Black,
            },
        };

        var sv = new ScrollViewer
        {
            VerticalScrollBarVisibility   = ScrollBarVisibility.Auto,
            HorizontalScrollBarVisibility = ScrollBarVisibility.Disabled,
        };

        var stack = new StackPanel { Margin = new Thickness(4) };

        // 헤더
        stack.Children.Add(new TextBlock
        {
            Text       = $"{_activeTab} 탭 프리셋 ({customPresets.Count}개)",
            FontSize   = 12,
            FontWeight = FontWeights.SemiBold,
            Foreground = primaryText,
            Margin     = new Thickness(8, 8, 8, 6),
        });

        if (customPresets.Count == 0)
        {
            stack.Children.Add(new TextBlock
            {
                Text       = "커스텀 프리셋이 없습니다.",
                FontSize   = 12,
                Foreground = secondaryText,
                Margin     = new Thickness(8, 0, 8, 8),
            });
        }
        else
        {
            foreach (var cp in customPresets)
            {
                var capturedCp = cp;

                var row = new Border
                {
                    Background    = Brushes.Transparent,
                    CornerRadius  = new CornerRadius(6),
                    Padding       = new Thickness(6, 5, 6, 5),
                    Cursor        = Cursors.Hand,
                };
                row.MouseEnter += (s, _) => { if (s is Border b) b.Background = hoverBg; };
                row.MouseLeave += (s, _) => { if (s is Border b) b.Background = Brushes.Transparent; };

                var rowGrid = new Grid();
                rowGrid.ColumnDefinitions.Add(new ColumnDefinition { Width = new GridLength(1, GridUnitType.Star) });
                rowGrid.ColumnDefinitions.Add(new ColumnDefinition { Width = GridLength.Auto });
                rowGrid.ColumnDefinitions.Add(new ColumnDefinition { Width = GridLength.Auto });

                rowGrid.Children.Add(new TextBlock
                {
                    Text            = capturedCp.Label,
                    FontSize        = 12,
                    Foreground      = primaryText,
                    VerticalAlignment = VerticalAlignment.Center,
                    TextTrimming    = TextTrimming.CharacterEllipsis,
                });

                var editIcon = new TextBlock
                {
                    Text          = "\uE70F",
                    FontFamily    = ThemeResourceHelper.SegoeMdl2,
                    FontSize      = 12,
                    Foreground    = secondaryText,
                    VerticalAlignment = VerticalAlignment.Center,
                    Cursor        = Cursors.Hand,
                    Margin        = new Thickness(8, 0, 4, 0),
                };
                Grid.SetColumn(editIcon, 1);
                editIcon.MouseLeftButtonDown += (_, _) =>
                {
                    popup.IsOpen = false;
                    var entry = Llm.CustomPresets.FirstOrDefault(c => c.Id == capturedCp.Id);
                    if (entry != null) ShowCustomPresetDialog(entry);
                };

                var delIcon = new TextBlock
                {
                    Text          = "\uE74D",
                    FontFamily    = ThemeResourceHelper.SegoeMdl2,
                    FontSize      = 12,
                    Foreground    = new SolidColorBrush(Color.FromRgb(0xEF, 0x44, 0x44)),
                    VerticalAlignment = VerticalAlignment.Center,
                    Cursor        = Cursors.Hand,
                    Margin        = new Thickness(4, 0, 0, 0),
                };
                Grid.SetColumn(delIcon, 2);
                delIcon.MouseLeftButtonDown += (_, _) =>
                {
                    popup.IsOpen = false;
                    var result = CustomMessageBox.Show(
                        $"'{capturedCp.Label}' 프리셋을 삭제하시겠습니까?",
                        "프리셋 삭제", MessageBoxButton.YesNo, MessageBoxImage.Question);
                    if (result == MessageBoxResult.Yes)
                    {
                        Llm.CustomPresets.RemoveAll(c => c.Id == capturedCp.Id);
                        _settings.Save();
                        BuildTopicButtons();
                    }
                };

                rowGrid.Children.Add(editIcon);
                rowGrid.Children.Add(delIcon);
                row.Child = rowGrid;
                stack.Children.Add(row);
            }
        }

        // 구분선 + 새 프리셋 추가
        stack.Children.Add(new Border
        {
            Height     = 1,
            Background = borderBrush,
            Margin     = new Thickness(4, 4, 4, 4),
        });

        var addItem = CreateContextMenuItem("\uE710", "새 프리셋 추가", primaryText, secondaryText);
        addItem.MouseLeftButtonDown += (_, _) =>
        {
            popup.IsOpen = false;
            ShowCustomPresetDialog(null);
        };
        stack.Children.Add(addItem);

        sv.Content   = stack;
        outer.Child  = sv;
        popup.Child  = outer;
        popup.IsOpen = true;
    }

    /// <summary>대화 주제 선택 — 프리셋 시스템 프롬프트 + 카테고리 적용.</summary>
    private void SelectTopic(Services.TopicPreset preset)
    {
        bool hasMessages;
        lock (_convLock) hasMessages = _currentConversation?.Messages.Count > 0;

        // 입력란에 텍스트가 있으면 기존 대화를 유지 (입력 내용 보존)
        bool hasInput = !string.IsNullOrEmpty(InputBox.Text);
        bool keepConversation = hasMessages || hasInput;

        if (!keepConversation)
        {
            // 메시지도 입력 텍스트도 없으면 새 대화 시작
            StartNewConversation();
        }

        // 프리셋 적용 (기존 대화에도 프리셋 변경 가능)
        lock (_convLock)
        {
            if (_currentConversation != null)
            {
                _currentConversation.SystemCommand = preset.SystemPrompt;
                _currentConversation.Category = preset.Category;
            }
        }

        if (!keepConversation)
            EmptyState.Visibility = Visibility.Collapsed;

        InputBox.Focus();

        if (!string.IsNullOrEmpty(preset.Placeholder))
        {
            _promptCardPlaceholder = preset.Placeholder;
            if (!keepConversation) ShowPlaceholder();
        }

        if (keepConversation)
            ShowToast($"프리셋 변경: {preset.Label}");

        // Cowork 탭: 하단 바 갱신
        if (_activeTab == "Cowork")
            BuildBottomBar();
    }
}
