using OpenSEMI.ClientBase;
using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media.Animation;

namespace CyberX8_Themes.CustomControls
{
   

    public class GuangChuanRobotControl : Control
    {
        static GuangChuanRobotControl()
        {
            DefaultStyleKeyProperty.OverrideMetadata(typeof(GuangChuanRobotControl), new FrameworkPropertyMetadata(typeof(GuangChuanRobotControl)));
        }

        public static readonly DependencyProperty WaferProperty = DependencyProperty.Register("Wafer", typeof(int), typeof(GuangChuanRobotControl));
        public int Wafer { get => (int)GetValue(WaferProperty); set => SetValue(WaferProperty, value); }


        public static readonly DependencyProperty ExtendTimeProperty = DependencyProperty.Register("ExtendTime", typeof(KeyTime), typeof(GuangChuanRobotControl),new PropertyMetadata (KeyTime.FromTimeSpan(TimeSpan.FromSeconds(9))));
        public KeyTime ExtendTime { get => (KeyTime)GetValue(ExtendTimeProperty); set => SetValue(ExtendTimeProperty, value); }

        public static readonly DependencyProperty RobotWaferProperty = DependencyProperty.Register(
          "RobotWafer",
          typeof(WaferInfo),
          typeof(GuangChuanRobotControl));

        public WaferInfo RobotWafer
        {
            get => (WaferInfo)GetValue(RobotWaferProperty);
            set => SetValue(RobotWaferProperty, value);
        }

        public static readonly DependencyProperty RobotXActionProperty = DependencyProperty.Register(
            "RobotXAction",
            typeof(WaferRobotXAction),
            typeof(GuangChuanRobotControl),
            new PropertyMetadata(WaferRobotXAction.X_Origin, RobotXActionPropertyChangedCallback));
        public WaferRobotXAction RobotXAction
        {
            get => (WaferRobotXAction)GetValue(RobotXActionProperty);
            set => SetValue(RobotXActionProperty, value);
        }

        private static void RobotXActionPropertyChangedCallback(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            //KeyTime value = KeyTime.FromTimeSpan(TimeSpan.FromSeconds(9));
            var control = d as GuangChuanRobotControl;
            var oldAct = (WaferRobotXAction)e.OldValue;
            var newAct = (WaferRobotXAction)e.NewValue;
            switch (newAct)
            {
                case WaferRobotXAction.X_Origin:
                    VisualStateManager.GoToState(control, newAct.ToString(), true);
                    break;
                case WaferRobotXAction.Extend:
                    if (newAct != oldAct)
                    {
                        VisualStateManager.GoToState(control, newAct.ToString(), true);
                    }
                    break;
                case WaferRobotXAction.Extend2:
                    if (newAct != oldAct)
                    {
                        VisualStateManager.GoToState(control, newAct.ToString(), true);
                    }
                    break;
                case WaferRobotXAction.Retract2:
                    if (newAct != oldAct)
                    {
                        VisualStateManager.GoToState(control, newAct.ToString(), true);
                    }
                    break;
                case WaferRobotXAction.Retract:
                    if (newAct != oldAct)
                    {
                        VisualStateManager.GoToState(control, newAct.ToString(), true);
                    }
                    break;
                default:
                    break;
            }
        }

        public static readonly DependencyProperty RobotTActionProperty = DependencyProperty.Register(
            "RobotTAction",
            typeof(WaferRobotTAction),
            typeof(GuangChuanRobotControl),
            new PropertyMetadata(WaferRobotTAction.T_Origin, RobotTActionPropertyChangedCallback));

        public WaferRobotTAction RobotTAction
        {
            get => (WaferRobotTAction)GetValue(RobotTActionProperty);
            set => SetValue(RobotTActionProperty, value);
        }


       public string OriginT { get; set; }

        private static void RobotTActionPropertyChangedCallback(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            var control = d as GuangChuanRobotControl;
            var oldAct = (WaferRobotTAction)e.OldValue;
            var newAct = (WaferRobotTAction)e.NewValue;
            if(oldAct!=newAct)
            {
                VisualStateManager.GoToState(control, newAct.ToString(), true);
            }
        }

        public override void OnApplyTemplate()
        {
            base.OnApplyTemplate();

            VisualStateManager.GoToState(this, WaferRobotXAction.X_Origin.ToString(), true);
            VisualStateManager.GoToState(this, OriginT, true);
        }
    }
}