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

namespace AxCopilot.Views;

public partial class SettingsWindow
{
    private void DevModeCheckBox_Checked(object sender, RoutedEventArgs e)
    {
        if (sender is not CheckBox cb || !cb.IsChecked.GetValueOrDefault()) return;
        // 설정 창 로드 중 바인딩에 의한 자동 Checked 이벤트 무시 (이미 활성화된 상태 복원)
        if (!IsLoaded) return;

        // 테마 리소스 조회
        var bgBrush = TryFindResource("LauncherBackground") as Brush ?? new SolidColorBrush(Color.FromRgb(0x1E, 0x1E, 0x2E));
        var fgBrush = TryFindResource("PrimaryText") as Brush ?? Brushes.White;
        var subFgBrush = TryFindResource("SecondaryText") as Brush ?? Brushes.Gray;
        var borderBrush = TryFindResource("BorderColor") as Brush ?? new SolidColorBrush(Color.FromRgb(0x40, 0x40, 0x60));
        var itemBg = TryFindResource("ItemBackground") as Brush ?? new SolidColorBrush(Color.FromRgb(0x2A, 0x2A, 0x40));

        // 비밀번호 확인 다이얼로그
        var dlg = new Window
        {
            Title = "개발자 모드 — 비밀번호 확인",
            Width = 340, SizeToContent = SizeToContent.Height,
            WindowStartupLocation = WindowStartupLocation.CenterOwner,
            Owner = this, ResizeMode = ResizeMode.NoResize,
            WindowStyle = WindowStyle.None, AllowsTransparency = true,
            Background = Brushes.Transparent,
        };
        var border = new Border
        {
            Background = bgBrush, CornerRadius = new CornerRadius(12),
            BorderBrush = borderBrush, BorderThickness = new Thickness(1), Padding = new Thickness(20),
        };
        var stack = new StackPanel();
        stack.Children.Add(new TextBlock
        {
            Text = "\U0001f512 개발자 모드 활성화",
            FontSize = 15, FontWeight = FontWeights.SemiBold,
            Foreground = fgBrush, Margin = new Thickness(0, 0, 0, 12),
        });
        stack.Children.Add(new TextBlock
        {
            Text = "비밀번호를 입력하세요:",
            FontSize = 12, Foreground = subFgBrush, Margin = new Thickness(0, 0, 0, 6),
        });
        var pwBox = new PasswordBox
        {
            FontSize = 14, Padding = new Thickness(8, 6, 8, 6),
            Background = itemBg, Foreground = fgBrush, BorderBrush = borderBrush, PasswordChar = '*',
        };
        stack.Children.Add(pwBox);

        var btnRow = new StackPanel { Orientation = Orientation.Horizontal, HorizontalAlignment = HorizontalAlignment.Right, Margin = new Thickness(0, 16, 0, 0) };
        var cancelBtn = new Button { Content = "취소", Padding = new Thickness(16, 6, 16, 6), Margin = new Thickness(0, 0, 8, 0) };
        cancelBtn.Click += (_, _) => { dlg.DialogResult = false; };
        btnRow.Children.Add(cancelBtn);
        var okBtn = new Button { Content = "확인", Padding = new Thickness(16, 6, 16, 6), IsDefault = true };
        okBtn.Click += (_, _) =>
        {
            if (pwBox.Password == "mouse12#")
                dlg.DialogResult = true;
            else
            {
                pwBox.Clear();
                pwBox.Focus();
            }
        };
        btnRow.Children.Add(okBtn);
        stack.Children.Add(btnRow);
        border.Child = stack;
        dlg.Content = border;
        dlg.Loaded += (_, _) => pwBox.Focus();

        if (dlg.ShowDialog() != true)
        {
            // 비밀번호 실패/취소 — 체크 해제 + DevMode 강제 false
            _vm.DevMode = false;
            cb.IsChecked = false;
        }
        UpdateDevModeContentVisibility();
    }

    private void DevModeCheckBox_Unchecked(object sender, RoutedEventArgs e)
    {
        UpdateDevModeContentVisibility();
    }

    /// <summary>개발자 모드 활성화 상태에 따라 개발자 탭 내용 표시/숨김.</summary>
    private void UpdateDevModeContentVisibility()
    {
        if (DevModeContent != null)
            DevModeContent.Visibility = _vm.DevMode ? Visibility.Visible : Visibility.Collapsed;
    }
}
