using OpenSEMI.ClientBase;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media.Animation;
using CyberX8_Themes.UserControls;

namespace CyberX8_Themes.CustomControls
{
    public class SERobot : Control
    {
        public enum SERobotXAction
        {
            X_Origin,
            Extend,
            ToVCE,
            FromVCE,
            FromVCEToVPA,
            Retract,
            X_Origin2,
            Extend2,
            ToVCE2,
            FromVCE2,
            FromVCEToVPA2,
            Retract2
        }

        public enum SERobotTAction
        {
            T_Origin,
            PMA,
            PMB,
            PMC,
            VCE1,
            VPA,
            VPARight,
            RightLocation,
            LeftLocation

        }
        static SERobot()
        {
            DefaultStyleKeyProperty.OverrideMetadata(typeof(SERobot), new FrameworkPropertyMetadata(typeof(SERobot)));
        }
        //注册Wafer、ExtendTime、RobotWafer、RobotXAction属性
        public static readonly DependencyProperty PMCIsInstalledProperty = DependencyProperty.Register(
    "PMCIsInstalled", typeof(bool), typeof(SERobot));
        public bool PMCIsInstalled
        {
            get { return (bool)this.GetValue(PMCIsInstalledProperty); }
            set { SetValue(PMCIsInstalledProperty, value); }
        }
        public static readonly DependencyProperty WaferProperty = DependencyProperty.Register("Wafer", typeof(int), typeof(SERobot));
        public int Wafer { get => (int)GetValue(WaferProperty); set => SetValue(WaferProperty, value); }


        public static readonly DependencyProperty ExtendTimeProperty = DependencyProperty.Register("ExtendTime", typeof(KeyTime), typeof(SERobot), 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(SERobot));

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

        public static readonly DependencyProperty RobotXActionProperty = DependencyProperty.Register(
            "RobotXAction",
            typeof(SERobotXAction),
            typeof(SERobot),
            new PropertyMetadata(SERobotXAction.X_Origin, RobotXActionPropertyChangedCallback));
        public SERobotXAction RobotXAction
        {
            get => (SERobotXAction)GetValue(RobotXActionProperty);
            set => SetValue(RobotXActionProperty, value);
        }
        //RobotXAction依赖属性的值被改变之后此委托会被调用
        private static void RobotXActionPropertyChangedCallback(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            //KeyTime value = KeyTime.FromTimeSpan(TimeSpan.FromSeconds(9));
            var control = d as SERobot; //d转换为SERobot
            var oldAct = (SERobotXAction)e.OldValue; //现在的位置
            var newAct = (SERobotXAction)e.NewValue; //下一个位置
            switch (newAct)
            {
                case SERobotXAction.X_Origin:
                    VisualStateManager.GoToState(control, newAct.ToString(), true);
                    break;
                case SERobotXAction.Extend:
                    if (newAct != oldAct)
                    {
                        VisualStateManager.GoToState(control, newAct.ToString(), true);
                    }
                    break;
                case SERobotXAction.Retract:
                    if (newAct != oldAct)
                    {
                        VisualStateManager.GoToState(control, newAct.ToString(), true);
                    }
                    break;
                case SERobotXAction.ToVCE:
                    if (newAct != oldAct)
                    {
                        VisualStateManager.GoToState(control, newAct.ToString(), true);
                    }
                    break;
                case SERobotXAction.FromVCEToVPA:
                    if (newAct != oldAct)
                    {
                        VisualStateManager.GoToState(control, newAct.ToString(), true);
                    }
                    break;
                case SERobotXAction.X_Origin2:
                    VisualStateManager.GoToState(control, newAct.ToString(), true);
                    break;
                case SERobotXAction.Extend2:
                    if (newAct != oldAct)
                    {
                        VisualStateManager.GoToState(control, newAct.ToString(), true);
                    }
                    break;
                case SERobotXAction.Retract2:
                    if (newAct != oldAct)
                    {
                        VisualStateManager.GoToState(control, newAct.ToString(), true);
                    }
                    break;
                case SERobotXAction.ToVCE2:
                    if (newAct != oldAct)
                    {
                        VisualStateManager.GoToState(control, newAct.ToString(), true);
                    }
                    break;
                case SERobotXAction.FromVCEToVPA2:
                    if (newAct != oldAct)
                    {
                        VisualStateManager.GoToState(control, newAct.ToString(), true);
                    }
                    break;
                default:
                    break;
            }
        }

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

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

        public static readonly DependencyProperty RobotSpeedProperty = DependencyProperty.Register(
           "RobotSpeed",
           typeof(double),
           typeof(SERobot),
            new PropertyMetadata(9.0d, RobotSpeedPropertyChangedCallback));

        public double RobotSpeed
        {
            get => (double)GetValue(RobotSpeedProperty);
            set => SetValue(RobotSpeedProperty, value);
        }
        private static void RobotSpeedPropertyChangedCallback(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {


        }
        public string OriginX { get; set; }
        public string OriginT { get; set; }

        private static void RobotTActionPropertyChangedCallback(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            var control = d as SERobot;
            var oldAct = (SERobotTAction)e.OldValue;
            var newAct = (SERobotTAction)e.NewValue;
            while (newAct.ToString() == "VPA"&&!control.PMCIsInstalled)
            {
                newAct = SERobotTAction.VPARight;
            }
            if (oldAct != newAct)
            {
                VisualStateManager.GoToState(control, newAct.ToString(), true);//前后动作不一致,改变控件状态
            }
        }

        public override void OnApplyTemplate()
        {
            base.OnApplyTemplate();
            VisualStateManager.GoToState(this, OriginX, true);
            //VisualStateManager.GoToState(this, SERobotXAction.X_Origin.ToString(), true);
            
            VisualStateManager.GoToState(this, OriginT, true);
        }
    }
}