using Aitex.Core.UI.MVVM;
using Aitex.Core.Utilities;
using MECF.Framework.Common.CommonData.Prewet;
using MECF.Framework.Common.CommonData.SRD;
using MECF.Framework.Common.OperationCenter;
using MECF.Framework.Common.Persistent.Prewet;
using MECF.Framework.Common.Persistent.SRD;
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.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 PunkHPX8_Themes.UserControls
{
    /// 
    /// PrewetLeftSideControl.xaml 的交互逻辑
    ///  
    public partial class PrewetLeftSideControl : UserControl
    {
        public PrewetLeftSideControl()
        {
            KeyDownCommand = new DelegateCommand(KeyDownAction);
            InitializeComponent();
        }
        #region 属性
        public static readonly DependencyProperty ModuleNameProperty = DependencyProperty.Register(
           "ModuleName", typeof(string), typeof(PrewetLeftSideControl),
           new FrameworkPropertyMetadata("", FrameworkPropertyMetadataOptions.AffectsRender));
        /// 
        /// 模块名称
        ///  
        public string ModuleName
        {
            get
            {
                return (string)this.GetValue(ModuleNameProperty);
            }
            set
            {
                this.SetValue(ModuleNameProperty, value);
            }
        }
        public static readonly DependencyProperty StateMachineProperty = DependencyProperty.Register(
            "StateMachine", typeof(string), typeof(PrewetLeftSideControl),
                new FrameworkPropertyMetadata("", FrameworkPropertyMetadataOptions.AffectsRender));
        /// 
        /// StateMachine
        ///  
        public string StateMachine
        {
            get
            {
                return (string)this.GetValue(StateMachineProperty);
            }
            set
            {
                this.SetValue(StateMachineProperty, value);
            }
        }
        public static readonly DependencyProperty StatusValueProperty = DependencyProperty.Register(
                "StatusValue", typeof(string), typeof(PrewetLeftSideControl),
                    new FrameworkPropertyMetadata("", FrameworkPropertyMetadataOptions.AffectsRender));
        /// 
        /// StatusValue
        ///  
        public string StatusValue
        {
            get
            {
                return (string)this.GetValue(StatusValueProperty);
            }
            set
            {
                this.SetValue(StatusValueProperty, value);
            }
        }
        public static readonly DependencyProperty IsManualProperty = DependencyProperty.Register(
             "IsManual", typeof(bool), typeof(PrewetLeftSideControl), new FrameworkPropertyMetadata(false, FrameworkPropertyMetadataOptions.AffectsRender));
        /// 
        /// OperatingMode
        ///  
        public bool IsManual
        {
            get
            {
                return (bool)this.GetValue(IsManualProperty);
            }
            set
            {
                this.SetValue(IsManualProperty, value);
            }
        }
        public static readonly DependencyProperty StateProperty = DependencyProperty.Register(
            "State", typeof(string), typeof(PrewetLeftSideControl),
            new FrameworkPropertyMetadata("", FrameworkPropertyMetadataOptions.AffectsRender));
        /// 
        /// State
        ///  
        public string State
        {
            get
            {
                return (string)this.GetValue(StateProperty);
            }
            set
            {
                this.SetValue(StateProperty, value);
            }
        }
        public static readonly DependencyProperty PumpDataProperty = DependencyProperty.Register(
          "PumpData", typeof(PrewetPumpData), typeof(PrewetLeftSideControl), new FrameworkPropertyMetadata(null, FrameworkPropertyMetadataOptions.AffectsRender));
        /// 
        /// PumpData
        ///  
        public PrewetPumpData PumpData
        {
            get
            {
                return (PrewetPumpData)this.GetValue(PumpDataProperty);
            }
            set
            {
                this.SetValue(PumpDataProperty, value);
            }
        }
        public static readonly DependencyProperty InputPumpSpeedProperty = DependencyProperty.Register(
        "InputPumpSpeed", typeof(short), typeof(PrewetLeftSideControl), new FrameworkPropertyMetadata((short)300, FrameworkPropertyMetadataOptions.AffectsRender));
        /// 
        /// InputLowThreshold
        ///  
        public short InputPumpSpeed
        {
            get
            {
                return (short)this.GetValue(InputPumpSpeedProperty);
            }
            set
            {
                this.SetValue(InputPumpSpeedProperty, value);
            }
        }
        public static readonly DependencyProperty LinmotPostionProperty = DependencyProperty.Register(
        "LinmotPostion", typeof(double), typeof(PrewetLeftSideControl), new FrameworkPropertyMetadata(0.00, FrameworkPropertyMetadataOptions.AffectsRender));
        /// 
        /// LinmotPostion
        ///  
        public double LinmotPostion
        {
            get
            {
                return (double)this.GetValue(LinmotPostionProperty);
            }
            set
            {
                this.SetValue(LinmotPostionProperty, value);
            }
        }
        public static readonly DependencyProperty CurrentScanedCompletedProperty = DependencyProperty.Register(
            "CurrentScanedCompleted", typeof(int), typeof(PrewetLeftSideControl), new FrameworkPropertyMetadata(0, FrameworkPropertyMetadataOptions.AffectsRender));
        /// 
        /// ScanedTimes
        ///  
        public int CurrentScanedCompleted
        {
            get
            {
                return (int)this.GetValue(CurrentScanedCompletedProperty);
            }
            set
            {
                this.SetValue(CurrentScanedCompletedProperty, value);
            }
        }
        public static readonly DependencyProperty KeepWetLimitProperty = DependencyProperty.Register(
           "KeepWetLimit", typeof(int), typeof(PrewetLeftSideControl), new FrameworkPropertyMetadata(0, FrameworkPropertyMetadataOptions.AffectsRender));
        /// 
        /// ScanedTimes
        ///  
        public int KeepWetLimit
        {
            get
            {
                return (int)this.GetValue(KeepWetLimitProperty);
            }
            set
            {
                this.SetValue(KeepWetLimitProperty, value);
            }
        }
        public static readonly DependencyProperty PersistentValueProperty = DependencyProperty.Register(
            "PersistentValue", typeof(PrewetPersistentValue), typeof(PrewetLeftSideControl), new FrameworkPropertyMetadata(null, new PropertyChangedCallback(OnItemsSourceChanged)));
        /// 
        /// PersistentValue
        ///  
        public PrewetPersistentValue PersistentValue
        {
            get
            {
                return (PrewetPersistentValue)this.GetValue(PersistentValueProperty);
            }
            set
            {
                this.SetValue(PersistentValueProperty, value);
            }
        }
        private static void OnItemsSourceChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            if (e.NewValue != null)
            {
                PrewetPersistentValue data = (PrewetPersistentValue)e.NewValue;
                d.SetValue(InputPumpSpeedProperty, data.Speed);
              
            }
        }
        #endregion
        [IgnorePropertyChange]
        public ICommand KeyDownCommand
        {
            get;
            private set;
        }
        private void KeyDownAction(object[] param)
        {
            if (param.Length >= 1)
            {
                if (short.TryParse(param[1].ToString(), out short paramValue))
                {
                    if(paramValue < 300 || paramValue > 7300)
                    {
                        MessageBox.Show("qualified values 300 ~ 7300", "Invalid Speed Input", MessageBoxButton.OK, MessageBoxImage.Error);
                    }
                    else
                    {
                        InvokeClient.Instance.Service.DoOperation($"{ModuleName}.PumpSpeedKeyDown", paramValue);
                    }
                    
                }
            }
        }
        #region 按钮事件
        private void PreparetoTransfer_Click(object sender, RoutedEventArgs e)
        {
            InvokeClient.Instance.Service.DoOperation($"{ModuleName}.PrepareToTransfer");
        }
        private void KeepWet_Click(object sender, RoutedEventArgs e)
        {
             InvokeClient.Instance.Service.DoOperation($"{ModuleName}.KeepWet");
        }
        private void PumpOff_Click(object sender, RoutedEventArgs e)
        {
            InvokeClient.Instance.Service.DoOperation($"{ModuleName}.PumpDisable");
        }
        private void PumpOn_Click(object sender, RoutedEventArgs e)
        {
            InvokeClient.Instance.Service.DoOperation($"{ModuleName}.PumpEnable");
        }
        private void Auto_Click(object sender, RoutedEventArgs e)
        {
            InvokeClient.Instance.Service.DoOperation($"{ModuleName}.PumpSpeedAuto");
        }
        private void Manual_Click(object sender, RoutedEventArgs e)
        {
            InvokeClient.Instance.Service.DoOperation($"{ModuleName}.PumpSpeedManual");
        }
        private void Close_Click(object sender, RoutedEventArgs e)
        {
            InvokeClient.Instance.Service.DoOperation($"{ModuleName}.PumpValveOff");
        }
        private void Open_Click(object sender, RoutedEventArgs e)
        {
            InvokeClient.Instance.Service.DoOperation($"{ModuleName}.PumpValveOn");
        }
        #endregion
    }
}