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;

namespace HistoryWallEditTool.ControlsForData.MediaControllerLayout
{
    /// <summary>
    /// CustomerSliderStyle.xaml 的互動邏輯
    /// </summary>
    public partial class CustomerSliderStyle : UserControl
    {
        private bool isMouseDown = false;

        public double Percent
        {
            get { return (double)GetValue(PercentProperty); }
            set { SetValue(PercentProperty, value); }
        }

        public static DependencyProperty PercentProperty =
            DependencyProperty.Register(
            "Percent",
            typeof(double),
            typeof(CustomerSliderStyle),
            new FrameworkPropertyMetadata(new PropertyChangedCallback(PercentPropertyChanged)));

        private static void PercentPropertyChanged(DependencyObject src, DependencyPropertyChangedEventArgs e)
        {
            CustomerSliderStyle slider = src as CustomerSliderStyle;
            if (slider.ActualWidth.Equals(0.0))
            {
                slider.Loaded += (obj, args) =>
                {
                    double value = double.Parse(e.NewValue.ToString());
                    if (value > 100)
                    {
                        value = 100;
                    }
                    if (value < 0)
                    {
                        value = 0;
                    }
                    double width = ((slider.Width - 17 - 15) / 100) * value + 15;
                    slider.uxTickGrid.Width = width;
                    slider.uxThumbImage.Margin = new Thickness(width - 15, 0, 0, 0);
                };
            }
            else
            {
                double value = double.Parse(e.NewValue.ToString());
                if (value > 100)
                {
                    value = 100;
                }
                if (value < 0)
                {
                    value = 0;
                }
                double width = ((slider.ActualWidth - 17 - 15) / 100) * value + 15;
                slider.uxTickGrid.Width = width;
                slider.uxThumbImage.Margin = new Thickness(width - 15, 0, 0, 0);
            }
        }
        public event EventHandler<ValueChangeEventArgs> OnValueChanged;
        public event EventHandler<EventArgs> OnSliderChangedEnd;

        public CustomerSliderStyle()
        {
            InitializeComponent();
        }

        protected virtual void SliderChangedEnd()
        {
            if (OnSliderChangedEnd != null)
            {
                OnSliderChangedEnd(this, null);
            }
        }

        protected virtual void ValueChanged(ValueChangeEventArgs e)
        {
            if (OnValueChanged != null)
            {
                OnValueChanged(this, e);
            }
        }

        private void GetCurSliderValue(Point pt)
        {
            double value = (pt.X - 17) / ((this.ActualWidth - 17 - 15) / 100);

            if (value > 100)
            {
                value = 100;
            }
            if (value < 0)
            {
                value = 0;
            }
            Percent = value;
            ValueChanged(new ValueChangeEventArgs(Percent));
        }

        private void uxRootGrid_MouseDown(object sender, MouseButtonEventArgs e)
        {
            isMouseDown = true;
        }

        private void uxRootGrid_MouseMove(object sender, MouseEventArgs e)
        {
            if (isMouseDown)
            {
                Point pt = e.GetPosition(this);

                GetCurSliderValue(pt);
            }
        }

        private void uxRootGrid_MouseLeave(object sender, MouseEventArgs e)
        {
            if (isMouseDown)
            {
                SliderChangedEnd();
            }
            isMouseDown = false;
        }

        private void uxRootGrid_MouseUp(object sender, MouseButtonEventArgs e)
        {
            if (isMouseDown)
            {
                Point pt = e.GetPosition(this);

                GetCurSliderValue(pt);
                SliderChangedEnd();
            }
            isMouseDown = false;
        }
    }

    public class ValueChangeEventArgs : EventArgs
    {
        public double percent;

        public ValueChangeEventArgs(double value)
        {
            percent = value;
        }
    }
}
