using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
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 System.Windows.Ink;
using System.ComponentModel;

namespace Platform.Controls.ComponentContainer
{
    /// <summary>
    /// ColorPicker.xaml 的交互逻辑
    /// </summary>
    [DesignTimeVisible(false)]
    public partial class ColorPicker : UserControl
    {

        #region 变量
        private DrawingAttributes drawingAttributes = new DrawingAttributes();
        private Color selectedColor = Colors.Transparent;
        private Boolean IsMouseDown = false;
        #endregion

        #region 初始化
        public ColorPicker()
        {
            InitializeComponent();
            this.Loaded += new RoutedEventHandler(ColorPicker_Loaded);
        }
        #endregion

        #region 公共属性
        /// <summary>
        /// 颜色选择器
        /// </summary>
        public Color SelectedColor
        {
            get { return selectedColor; }
            private set
            {
                if (selectedColor != value)
                {
                    selectedColor = value;
                    CreateAlphaLinearBrush();
                    UpdateTextBoxes();
                    UpdateInk();
                }
            }
        }
        #endregion

        #region 私有方法
        /// <summary>
        /// 颜色选择器加载时，默认的颜色值
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void ColorPicker_Loaded(object sender, RoutedEventArgs e)
        {
            SelectedColor = Colors.Black;
        }

        /// <summary>
        /// 为滑动条创建一个默认的渐变色
        /// </summary>
        private void CreateAlphaLinearBrush()
        {
            Color startColor = Color.FromArgb(
                    (byte)0,
                    SelectedColor.R,
                    SelectedColor.G,
                    SelectedColor.B);

            Color endColor = Color.FromArgb(
                    (byte)255,
                    SelectedColor.R,
                    SelectedColor.G,
                    SelectedColor.B);

            LinearGradientBrush alphaBrush =
                new LinearGradientBrush(startColor, endColor,
                    new Point(0, 0), new Point(1, 0));

            AlphaBorder.Background = alphaBrush;
        }


        /// <summary>
        /// 选择调色器图案
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Swatch_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            System.Windows.Controls.Image img = (sender as System.Windows.Controls.Image);
            ColorImage.Source = img.Source;
        }


        /// <summary>
        /// 鼠标指针移动时发生
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void CanvImage_MouseMove(object sender, MouseEventArgs e)
        {
            if (!IsMouseDown)
                return;

            try
            {
                CroppedBitmap cb = new CroppedBitmap(ColorImage.Source as BitmapSource,
                    new Int32Rect((int)Mouse.GetPosition(CanvImage).X,
                        (int)Mouse.GetPosition(CanvImage).Y, 1, 1));

                byte[] pixels = new byte[4];

                try
                {
                    cb.CopyPixels(pixels, 4, 0);
                }
                catch (Exception)
                {

                }

                //当鼠标指针选择该点的时候，填充该点的颜色
                ellipsePixel.SetValue(Canvas.LeftProperty, (double)(Mouse.GetPosition(CanvImage).X - 5));
                ellipsePixel.SetValue(Canvas.TopProperty, (double)(Mouse.GetPosition(CanvImage).Y - 5));
                CanvImage.InvalidateVisual();
                SelectedColor = Color.FromArgb((byte)AlphaSlider.Value, pixels[2], pixels[1], pixels[0]);
            }
            catch (Exception)
            {

            }
        }
        /// <summary>
        /// 按下任何鼠标按钮时发生
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void CanvImage_MouseDown(object sender, MouseButtonEventArgs e)
        {
            IsMouseDown = true;
        }

        /// <summary>
        /// 释放任何鼠标按钮时发生
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void CanvImage_MouseUp(object sender, MouseButtonEventArgs e)
        {
            IsMouseDown = false;
        }
        /// <summary>
        /// 更新TextBox颜色值
        /// </summary>
        private void UpdateTextBoxes()
        {
            txtAlpha.Text = SelectedColor.A.ToString();
            txtAlphaHex.Text = SelectedColor.A.ToString("X");
            txtRed.Text = SelectedColor.R.ToString();
            txtRedHex.Text = SelectedColor.R.ToString("X");
            txtGreen.Text = SelectedColor.G.ToString();
            txtGreenHex.Text = SelectedColor.G.ToString("X");
            txtBlue.Text = SelectedColor.B.ToString();
            txtBlueHex.Text = SelectedColor.B.ToString("X");
            txtAll.Text = String.Format("#{0}{1}{2}{3}",
                    txtAlphaHex.Text, txtRedHex.Text,
                    txtGreenHex.Text, txtBlueHex.Text);
        }

        /// <summary>
        /// 在调色器选中颜色值后，更改预览的颜色
        /// </summary>
        private void UpdateInk()
        {
            drawingAttributes.Color = SelectedColor;
            drawingAttributes.StylusTip = StylusTip.Ellipse;
            drawingAttributes.Width = 5;
            foreach (Stroke s in previewPresenter.Strokes)
            {
                s.DrawingAttributes = drawingAttributes;
            }
        }

        /// <summary>
        /// 滑动条更改的事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void AlphaSlider_ValueChanged(object sender,
            RoutedPropertyChangedEventArgs<double> e)
        {
            SelectedColor =
                Color.FromArgb(
                    (byte)AlphaSlider.Value,
                    SelectedColor.R,
                    SelectedColor.G,
                    SelectedColor.B);
        }


        #endregion
    }
}
