using Aitex.Core.RT.Device;
using Aitex.Core.UI.MVVM;
using Aitex.Core.Utilities;
using MECF.Framework.Common.CommonData.PUF;
using MECF.Framework.Common.Equipment;
using MECF.Framework.Common.OperationCenter;
using MECF.Framework.Common.Persistent.SRD;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Runtime.InteropServices;
using System.Text;
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;

namespace CyberX8_Themes.UserControls
{
    /// <summary>
    /// WaferPresenceControl.xaml 的交互逻辑
    /// </summary>
    public partial class WaferPresenceControl : UserControl
    {
        /// <summary>
        /// 构造函数
        /// </summary>
        public WaferPresenceControl()
        {
            KeyDownCommand = new DelegateCommand<object[]>(KeyDownAction);
            InitializeComponent();
        }

        #region 属性
        public static readonly DependencyProperty ModuleNameProperty = DependencyProperty.Register(
          "ModuleName", typeof(string), typeof(WaferPresenceControl), new FrameworkPropertyMetadata("", FrameworkPropertyMetadataOptions.AffectsRender));
        /// <summary>
        /// 模块名称
        /// </summary>
        public string ModuleName
        {
            get
            {
                return (string)this.GetValue(ModuleNameProperty);
            }
            set
            {
                this.SetValue(ModuleNameProperty, value);
            }
        }
        public static readonly DependencyProperty InputLowThresholdProperty = DependencyProperty.Register(
           "InputLowThreshold", typeof(double), typeof(WaferPresenceControl), new FrameworkPropertyMetadata(0.00, FrameworkPropertyMetadataOptions.AffectsRender));
        /// <summary>
        /// InputLowThreshold
        /// </summary>
        public double InputLowThreshold
        {
            get
            {
                return (double)this.GetValue(InputLowThresholdProperty);
            }
            set
            {
                this.SetValue(InputLowThresholdProperty, value);
            }
        }

        public static readonly DependencyProperty InputHighThresholdProperty = DependencyProperty.Register(
            "InputHighThreshold", typeof(double), typeof(WaferPresenceControl), new FrameworkPropertyMetadata(0.00, FrameworkPropertyMetadataOptions.AffectsRender));
        /// <summary>
        /// InputHighThreshold
        /// </summary>
        public double InputHighThreshold
        {
            get
            {
                return (double)this.GetValue(InputHighThresholdProperty);
            }
            set
            {
                this.SetValue(InputHighThresholdProperty, value);
            }
        }

        public static readonly DependencyProperty InputEmptyThresholdProperty = DependencyProperty.Register(
            "InputEmptyThreshold", typeof(double), typeof(WaferPresenceControl), new FrameworkPropertyMetadata(0.00, FrameworkPropertyMetadataOptions.AffectsRender));
        /// <summary>
        /// InputEmptyThreshold
        /// </summary>
        public double InputEmptyThreshold
        {
            get
            {
                return (double)this.GetValue(InputEmptyThresholdProperty);
            }
            set
            {
                this.SetValue(InputEmptyThresholdProperty, value);
            }
        }
        public static readonly DependencyProperty PersistentValueProperty = DependencyProperty.Register(
            "PersistentValue", typeof(SRDPersistentValue), typeof(WaferPresenceControl), new FrameworkPropertyMetadata(null, new PropertyChangedCallback(OnItemsSourceChanged)));
        /// <summary>
        /// PersistentValue
        /// </summary>
        public SRDPersistentValue PersistentValue
        {
            get
            {
                return (SRDPersistentValue)this.GetValue(PersistentValueProperty);
            }
            set
            {
                this.SetValue(PersistentValueProperty, value);
            }
        }

        public static readonly DependencyProperty IsWaferPresenceProperty = DependencyProperty.Register(
            "IsWaferPresence", typeof(bool), typeof(WaferPresenceControl), new FrameworkPropertyMetadata(true, new PropertyChangedCallback(OnIsWaferPresenceChanged)));
        /// <summary>
        /// IsWaferPresence
        /// </summary>
        public bool IsWaferPresence
        {
            get
            {
                return (bool)this.GetValue(IsWaferPresenceProperty);
            }
            set
            {
                this.SetValue(IsWaferPresenceProperty, value);
            }
        }
        private static void OnItemsSourceChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            if (e.NewValue != null)
            {
                SRDPersistentValue data = (SRDPersistentValue)e.NewValue;
                d.SetValue(InputHighThresholdProperty, data.WellPlacedHighThreshold);
                d.SetValue(InputLowThresholdProperty, data.WellPlacedLowThreshold);
                d.SetValue(InputEmptyThresholdProperty, data.EmptyThreshold);
            }
        }
        private static void OnIsWaferPresenceChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            WaferPresenceControl waferPresenceControl = (WaferPresenceControl)d;
            if (e.NewValue != null)
            {
                bool data = (bool)e.NewValue;
                waferPresenceControl.checkbox1.IsChecked = !data;    
            }
        }
        //private static void OnSourceChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        //{
        //    if (e.NewValue != null)
        //    {
        //        bool data = (bool)e.NewValue;
        //        d.SetValue(IsWaferPresenceProperty, data);
        //    }
        //}

        #endregion

        [IgnorePropertyChange]
        public ICommand KeyDownCommand
        {
            get;
            private set;
        }

        private void KeyDownAction(object[] param)
        {
            if (param.Length >= 2)
            {
                if (double.TryParse(param[1].ToString(), out double paramValue))
                {
                    InvokeClient.Instance.Service.DoOperation($"{ModuleName}.KeyDown", param[0].ToString(), paramValue);
                }
            }
        }

        private void CheckBox_Click(object sender, RoutedEventArgs e)
        {
            CheckBox checkBox = (sender as CheckBox);
            if (string.IsNullOrEmpty(ModuleName)) return;
            if ((bool)checkBox.IsChecked)
            {
                InvokeClient.Instance.Service.DoOperation($"{ModuleName}.UpdateIsWaferPresenceAction", false);
            }
            else
            {
                InvokeClient.Instance.Service.DoOperation($"{ModuleName}.UpdateIsWaferPresenceAction", true);
            }
            
        }
    }
}