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

namespace CyberX8_Themes.UserControls
{
    /// <summary>
    /// TransPorterControl.xaml 的交互逻辑
    /// </summary>
    public partial class TransPorterControl : UserControl
    {
        #region 常量 
        private const int HEIGHT = 93;
        private const int WAFER_HOLDER_TOP= 60;
        #endregion
        public TransPorterControl()
        {
            InitializeComponent();
        }

        public static readonly DependencyProperty TransPorterNameProperty = DependencyProperty.Register(
         "TransPorterName", typeof(string), typeof(TransPorterControl), new FrameworkPropertyMetadata("Loader TransPorter", FrameworkPropertyMetadataOptions.AffectsRender));

        public string TransPorterName
        {
            get { return this.GetValue(TransPorterNameProperty).ToString(); }
            set { this.SetValue(TransPorterNameProperty, value); }
        }

        public static readonly DependencyProperty WaferHolderVisibleProperty = DependencyProperty.Register(
         "WaferHolderVisible", typeof(bool), typeof(TransPorterControl), new FrameworkPropertyMetadata(false, FrameworkPropertyMetadataOptions.AffectsRender));

        public bool WaferHolderVisible
        {
            get { return (bool)this.GetValue(WaferHolderVisibleProperty); }
            set { this.SetValue(WaferHolderVisibleProperty, value); }
        }

        public static readonly DependencyProperty VerticalPositionProperty = DependencyProperty.Register("VerticalPosition", typeof(double), 
            typeof(TransPorterControl), new PropertyMetadata((double)93,new PropertyChangedCallback(VerticalPositionPropertyChanged)));
        public double VerticalPosition
        {
            get { return (double)this.GetValue(VerticalPositionProperty); }
            set
            {
                this.SetValue(VerticalPositionProperty, value);
            }
        }

        public static readonly DependencyProperty LiftHolderPositionProperty = DependencyProperty.Register("LiftHolderPosition", typeof(double), typeof(TransPorterControl), new PropertyMetadata((double)93));
        public double LiftHolderPosition
        {
            get { return (double)this.GetValue(LiftHolderPositionProperty); }
            set
            {
                this.SetValue(LiftHolderPositionProperty, value);
            }
        }
        public static readonly DependencyProperty WaferHolderPositionProperty = DependencyProperty.Register("WaferHolderPosition", typeof(double), typeof(TransPorterControl), new PropertyMetadata((double)93));
        public double WaferHolderPosition
        {
            get { return (double)this.GetValue(WaferHolderPositionProperty); }
            set
            {
                this.SetValue(WaferHolderPositionProperty, value);
            }
        }

        public static readonly DependencyProperty IsLoaderTransporterProperty = DependencyProperty.Register(
     "IsLoaderTransporter", typeof(bool), typeof(TransPorterControl), new FrameworkPropertyMetadata(false, FrameworkPropertyMetadataOptions.AffectsRender));

        public bool IsLoaderTransporter
        {
            get { return (bool)this.GetValue(IsLoaderTransporterProperty); }
            set { this.SetValue(IsLoaderTransporterProperty, value); }
        }

        public static readonly DependencyProperty IsProcessTransporterProperty = DependencyProperty.Register(
     "IsProcessTransporter", typeof(bool), typeof(TransPorterControl), new FrameworkPropertyMetadata(false, FrameworkPropertyMetadataOptions.AffectsRender));

        public bool IsProcessTransporter
        {
            get { return (bool)this.GetValue(IsProcessTransporterProperty); }
            set { this.SetValue(IsProcessTransporterProperty, value); }
        }

        public static readonly DependencyProperty IsWHEnableProperty = DependencyProperty.Register(
           "IsWHEnable", typeof(bool), typeof(TransPorterControl),
           new FrameworkPropertyMetadata(true, FrameworkPropertyMetadataOptions.AffectsRender));
        /// <summary>
        /// 模块名称
        /// </summary>
        public bool IsWHEnable
        {
            get
            {
                return (bool)this.GetValue(IsWHEnableProperty);
            }
            set
            {
                this.SetValue(IsWHEnableProperty, value);
            }
        }
        /// <summary>
        /// 垂直水平发生变化
        /// </summary>
        /// <param name="d"></param>
        /// <param name="e"></param>
        private static void VerticalPositionPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            if(e.NewValue!=null)
            {
                double newValue = (double)e.NewValue;
                d.SetValue(LiftHolderPositionProperty, newValue - HEIGHT);
                d.SetValue(WaferHolderPositionProperty, newValue + WAFER_HOLDER_TOP);
            }
        }

        private void Canvas_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
        {
            string name = "";
            if("Loader Transporter".Equals(TransPorterName)) 
            {
                name = "Transporter2Teach";
            }
            if ("Process Transporter".Equals(TransPorterName))
            {
                name = "Transporter1Teach";
            }
            GlobalEvents.OnSwitchFixedTabItem("HardWare", "Transpoters", $"{name}");
        }

    }
}