using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using WinForm = System.Windows.Forms;

namespace PDF_Office.CustomControl
{
    /// <summary>
    /// ColorDropBoxPop.xaml 的交互逻辑
    /// </summary>
    public class ColorToBrushConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            if (value is Color)
            {
                return new SolidColorBrush((Color)value);
            }
            return Brushes.Transparent;
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }
    /// <summary>
    /// ColorDropBoxPop.xaml 的交互逻辑
    /// </summary>
    public partial class ColorDropBoxPop : UserControl
    {
        public event EventHandler<Color> ColorSelected;
        public ObservableCollection<Color> RecentlyColors { get; private set; }
        public ObservableCollection<Color> ThemeColors { get; private set; }
        public ColorDropBoxPop()
        {
            InitializeComponent();
            SetLangText();
            RecentlyColors = new ObservableCollection<Color>();
            ThemeColors = new ObservableCollection<Color>();
            CreateThemeColors();
            RecentlyColorList.ItemsSource = RecentlyColors;
            ThemeColorList.ItemsSource = ThemeColors;
        }

        private void SetLangText()
        {
            //RecentlyColorText.Text = App.MainPageLoader.GetString("Themes_RecentlyUsed");
            //TransparentText.Text = App.MainPageLoader.GetString("Themes_Transparent");
            //OtherColorsText.Text = App.MainPageLoader.GetString("Themes_More");
            //ThemeColorsText.Text = App.MainPageLoader.GetString("ProperPanel_Color");
        }

        public void CreateThemeColors()
        {
            ThemeColors.Add(Color.FromRgb(0xFF, 0xFF, 0xFF));
            ThemeColors.Add(Color.FromRgb(0x00, 0x00, 0x00));
            ThemeColors.Add(Color.FromRgb(0x25, 0x1C, 0x16));
            ThemeColors.Add(Color.FromRgb(0xCC, 0x00, 0x00));
            ThemeColors.Add(Color.FromRgb(0xCC, 0x4E, 0x00));
            ThemeColors.Add(Color.FromRgb(0x99, 0x70, 0x00));
            ThemeColors.Add(Color.FromRgb(0x1C, 0x70, 0x17));
            ThemeColors.Add(Color.FromRgb(0x00, 0x8D, 0x99));
            ThemeColors.Add(Color.FromRgb(0x00, 0x3F, 0x99));
            ThemeColors.Add(Color.FromRgb(0x61, 0x00, 0x99));

            ThemeColors.Add(Color.FromRgb(0x88, 0x88, 0x88));
            ThemeColors.Add(Color.FromRgb(0x1B, 0x1B, 0x1B));
            ThemeColors.Add(Color.FromRgb(0x4A, 0x37, 0x2C));
            ThemeColors.Add(Color.FromRgb(0xFF, 0x00, 0x00));
            ThemeColors.Add(Color.FromRgb(0xFF, 0x61, 0x00));
            ThemeColors.Add(Color.FromRgb(0xCC, 0x96, 0x00));
            ThemeColors.Add(Color.FromRgb(0x27, 0x9D, 0x20));
            ThemeColors.Add(Color.FromRgb(0x00, 0xBC, 0xCC));
            ThemeColors.Add(Color.FromRgb(0x00, 0x54, 0xcc));
            ThemeColors.Add(Color.FromRgb(0x82, 0x00, 0xcc));

            ThemeColors.Add(Color.FromRgb(0xA0, 0xA0, 0xA0));
            ThemeColors.Add(Color.FromRgb(0x29, 0x29, 0x29));
            ThemeColors.Add(Color.FromRgb(0x6E, 0x53, 0x42));
            ThemeColors.Add(Color.FromRgb(0xFF, 0x33, 0x33));
            ThemeColors.Add(Color.FromRgb(0xFF, 0x81, 0x33));
            ThemeColors.Add(Color.FromRgb(0xFF, 0xBB, 0x00));
            ThemeColors.Add(Color.FromRgb(0x32, 0xCA, 0x29));
            ThemeColors.Add(Color.FromRgb(0x00, 0xEB, 0xFF));
            ThemeColors.Add(Color.FromRgb(0x00, 0x69, 0xFF));
            ThemeColors.Add(Color.FromRgb(0xA2, 0x00, 0xFF));

            ThemeColors.Add(Color.FromRgb(0xB8, 0xB8, 0xB8));
            ThemeColors.Add(Color.FromRgb(0x44, 0x44, 0x44));
            ThemeColors.Add(Color.FromRgb(0x93, 0x6E, 0x58));
            ThemeColors.Add(Color.FromRgb(0xFF, 0x66, 0x66));
            ThemeColors.Add(Color.FromRgb(0xFF, 0xA0, 0x66));
            ThemeColors.Add(Color.FromRgb(0xFF, 0xC9, 0x33));
            ThemeColors.Add(Color.FromRgb(0x38, 0xE0, 0x2E));
            ThemeColors.Add(Color.FromRgb(0x33, 0xEF, 0xFF));
            ThemeColors.Add(Color.FromRgb(0x33, 0x87, 0xFF));
            ThemeColors.Add(Color.FromRgb(0xB5, 0x33, 0xFF));

            ThemeColors.Add(Color.FromRgb(0xCF, 0xCF, 0xCF));
            ThemeColors.Add(Color.FromRgb(0x5F, 0x5F, 0x5F));
            ThemeColors.Add(Color.FromRgb(0xB8, 0x8A, 0x6E));
            ThemeColors.Add(Color.FromRgb(0xFF, 0x99, 0x99));
            ThemeColors.Add(Color.FromRgb(0xFF, 0xB0, 0x80));
            ThemeColors.Add(Color.FromRgb(0xFF, 0xD6, 0x66));
            ThemeColors.Add(Color.FromRgb(0x60, 0xE6, 0x58));
            ThemeColors.Add(Color.FromRgb(0x66, 0xF3, 0xFF));
            ThemeColors.Add(Color.FromRgb(0x66, 0xA5, 0xFF));
            ThemeColors.Add(Color.FromRgb(0xC7, 0x66, 0xFF));

            ThemeColors.Add(Color.FromRgb(0xE7, 0xE7, 0xE7));
            ThemeColors.Add(Color.FromRgb(0x7A, 0x7A, 0x7A));
            ThemeColors.Add(Color.FromRgb(0xC6, 0xA1, 0x8B));
            ThemeColors.Add(Color.FromRgb(0xFF, 0xCC, 0xCC));
            ThemeColors.Add(Color.FromRgb(0xFF, 0xC4, 0x9F));
            ThemeColors.Add(Color.FromRgb(0xFF, 0xE4, 0x99));
            ThemeColors.Add(Color.FromRgb(0x88, 0xEC, 0x82));
            ThemeColors.Add(Color.FromRgb(0x99, 0xF7, 0xFF));
            ThemeColors.Add(Color.FromRgb(0x99, 0xC3, 0xFF));
            ThemeColors.Add(Color.FromRgb(0xDA, 0x99, 0xFF));
        }

        private void SystemColorPicker_ClickPop(object sender, MouseButtonEventArgs e)
        {
            this.Visibility = Visibility.Collapsed;
            WinForm.ColorDialog colorPicker = new WinForm.ColorDialog();
            WinForm.DialogResult result = colorPicker.ShowDialog();
            if (result == WinForm.DialogResult.OK)
            {

                Color selectColor = Color.FromArgb(colorPicker.Color.A, colorPicker.Color.R, colorPicker.Color.G, colorPicker.Color.B);
                AddColorToRecent(selectColor);
                if (ColorSelected != null)
                {
                    ColorSelected?.Invoke(this, selectColor);
                }
            }
        }

        private void TransparentColor_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            if (ColorSelected != null)
            {
                ColorSelected?.Invoke(this, Colors.Transparent);
            }
            this.Visibility = Visibility.Collapsed;
        }

        private void Rectangle_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            this.Visibility = Visibility.Collapsed;
            Rectangle clickRectangle = sender as Rectangle;
            if (clickRectangle != null)
            {
                SolidColorBrush fillBrush = clickRectangle.Fill as SolidColorBrush;
                if (fillBrush != null)
                {
                    AddColorToRecent(fillBrush.Color);
                    if (ColorSelected != null)
                    {
                        ColorSelected?.Invoke(this, fillBrush.Color);
                    }
                }
            }
        }

        public void AddColorToRecent(Color recentColor)
        {
            if(recentColor==Colors.Transparent)
            {
                return;
            }
            if (RecentlyColors.AsEnumerable().Where(x => x == recentColor).Count() == 0)
            {
                while (RecentlyColors.Count >= 10)
                {
                    RecentlyColors.RemoveAt(0);
                }
                RecentlyColors.Add(recentColor);
            }
        }
        public void SetTransparentPanelVisible(bool visible)
        {
            TransparentPanel.Visibility = visible ? Visibility.Visible : Visibility.Collapsed;
        }
    }
}
