using MECF.Framework.Common.Equipment;
using MECF.Framework.Common.OperationCenter;
using MECF.Framework.Common.Persistent.Prewet;
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 CyberX8_Themes.UserControls
{
    /// 
    /// PrewetMotionPanel.xaml 的交互逻辑
    /// 
    public partial class PrewetMotionPanel : UserControl
    {
        public PrewetMotionPanel()
        {
            InitializeComponent();
        }
        #region 属性
        public static readonly DependencyProperty ModuleNameProperty = DependencyProperty.Register(
            "ModuleName", typeof(string), typeof(PrewetMotionPanel),
                new FrameworkPropertyMetadata("", FrameworkPropertyMetadataOptions.AffectsRender));
        /// 
        /// 模块名称
        /// 
        public string ModuleName
        {
            get
            {
                return (string)this.GetValue(ModuleNameProperty);
            }
            set
            {
                this.SetValue(ModuleNameProperty, value);
            }
        }
        public static readonly DependencyProperty LinmotNameProperty = DependencyProperty.Register(
            "LinmotName", typeof(string), typeof(PrewetMotionPanel),
                new FrameworkPropertyMetadata("", FrameworkPropertyMetadataOptions.AffectsRender));
        /// 
        /// LinmotName
        /// 
        public string LinmotName
        {
            get
            {
                return (string)this.GetValue(LinmotNameProperty);
            }
            set
            {
                this.SetValue(LinmotNameProperty, value);
            }
        }
        public static readonly DependencyProperty InputScanTimesProperty = DependencyProperty.Register(
          "InputScanTimes", typeof(int), typeof(PrewetMotionPanel), new FrameworkPropertyMetadata(0, FrameworkPropertyMetadataOptions.AffectsRender));
        /// 
        /// InputScanTimes
        /// 
        public int InputScanTimes
        {
            get
            {
                return (int)this.GetValue(InputScanTimesProperty);
            }
            set
            {
                this.SetValue(InputScanTimesProperty, value);
            }
        }
        public static readonly DependencyProperty CurrentScanedTimesProperty = DependencyProperty.Register(
            "CurrentScanedTimes", typeof(int), typeof(PrewetMotionPanel), new FrameworkPropertyMetadata(0, FrameworkPropertyMetadataOptions.AffectsRender));
        /// 
        /// ScanedTimes
        /// 
        public int CurrentScanedTimes
        {
            get
            {
                return (int)this.GetValue(CurrentScanedTimesProperty);
            }
            set
            {
                this.SetValue(CurrentScanedTimesProperty, value);
            }
        }
        public static readonly DependencyProperty ScanSpeedProperty = DependencyProperty.Register(
            "ScanSpeed", typeof(double), typeof(PrewetMotionPanel), new FrameworkPropertyMetadata(0.00, FrameworkPropertyMetadataOptions.AffectsRender));
        /// 
        ///  ScanSpeed
        /// 
        public double ScanSpeed
        {
            get
            {
                return (double)this.GetValue(ScanSpeedProperty);
            }
            set
            {
                this.SetValue(ScanSpeedProperty, value);
            }
        }
        public static readonly DependencyProperty LinMotErrorProperty = DependencyProperty.Register(
           " LinMotError", typeof(double), typeof(PrewetMotionPanel), new FrameworkPropertyMetadata(0.00, FrameworkPropertyMetadataOptions.AffectsRender));
        /// 
        ///  LinMotError
        /// 
        public double LinMotError
        {
            get
            {
                return (double)this.GetValue(LinMotErrorProperty);
            }
            set
            {
                this.SetValue(LinMotErrorProperty, value);
            }
        }
        public static readonly DependencyProperty LinmotWarningProperty = DependencyProperty.Register(
           " LinmotWarning", typeof(double), typeof(PrewetMotionPanel), new FrameworkPropertyMetadata(0.00, FrameworkPropertyMetadataOptions.AffectsRender));
        /// 
        ///  LinmotWarning
        /// 
        public double LinmotWarning
        {
            get
            {
                return (double)this.GetValue(LinmotWarningProperty);
            }
            set
            {
                this.SetValue(LinmotWarningProperty, value);
            }
        }
        public static readonly DependencyProperty StatusWordProperty = DependencyProperty.Register("StatusWord", typeof(short), typeof(PrewetMotionPanel),
         new FrameworkPropertyMetadata((short)0, new PropertyChangedCallback(StatusWordPropertyChangedCallBack)));
        /// 
        /// 状态字
        /// 
        public short StatusWord
        {
            get { return (short)this.GetValue(StatusWordProperty); }
            set
            {
                this.SetValue(StatusWordProperty, value);
            }
        }
        private static void StatusWordPropertyChangedCallBack(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            short statusWord = (short)e.NewValue;
            d.SetValue(ReadyProperty, (statusWord & 0x01) == 1);
            d.SetValue(DisableProperty, (statusWord & 0b0100) >> 2 == 0);
            d.SetValue(ErrorProperty, ((statusWord & 0b01000) >> 3 == 1) || ((statusWord & 0b01000000000000) >> 12 == 1));
            d.SetValue(InitProperty, (statusWord & 0b0100000000000) >> 11 == 1);
            d.SetValue(RunProperty, (statusWord & 0b010000000000000) >> 13 == 1);
        }
        public static readonly DependencyProperty ReadyProperty = DependencyProperty.Register("Ready", typeof(bool), typeof(PrewetMotionPanel),
new FrameworkPropertyMetadata(false, FrameworkPropertyMetadataOptions.AffectsRender));
        public bool Ready
        {
            get { return (bool)this.GetValue(ReadyProperty); }
            set { SetValue(ReadyProperty, value); }
        }
        public static readonly DependencyProperty DisableProperty = DependencyProperty.Register("Disable", typeof(bool), typeof(PrewetMotionPanel),
        new FrameworkPropertyMetadata(false, FrameworkPropertyMetadataOptions.AffectsRender));
        public bool Disable
        {
            get { return (bool)this.GetValue(DisableProperty); }
            set { SetValue(DisableProperty, value); }
        }
        public static readonly DependencyProperty RunProperty = DependencyProperty.Register("Run", typeof(bool), typeof(PrewetMotionPanel),
        new FrameworkPropertyMetadata(false, FrameworkPropertyMetadataOptions.AffectsRender));
        public bool Run
        {
            get { return (bool)this.GetValue(RunProperty); }
            set { SetValue(RunProperty, value); }
        }
        public static readonly DependencyProperty ErrorProperty = DependencyProperty.Register("Error", typeof(bool), typeof(PrewetMotionPanel),
        new FrameworkPropertyMetadata(false, FrameworkPropertyMetadataOptions.AffectsRender));
        public bool Error
        {
            get { return (bool)this.GetValue(ErrorProperty); }
            set { SetValue(ErrorProperty, value); }
        }
        public static readonly DependencyProperty InitProperty = DependencyProperty.Register("Init", typeof(bool), typeof(PrewetMotionPanel),
        new FrameworkPropertyMetadata(false, FrameworkPropertyMetadataOptions.AffectsRender));
        public bool Init
        {
            get { return (bool)this.GetValue(InitProperty); }
            set { SetValue(InitProperty, value); }
        }
        #endregion
        private void Start_Click(object sender, RoutedEventArgs e)
        {
            if (InputScanTimes <= 0)
            {
                MessageBox.Show("Current ScanTimes Error!", $"Current ScanTimes is {InputScanTimes}", MessageBoxButton.OK, MessageBoxImage.Error);
            }
            else
            {
                InvokeClient.Instance.Service.DoOperation($"{LinmotName}.StartPosition", InputScanTimes);
            }
        }
        private void Stop_Click(object sender, RoutedEventArgs e)
        {
            InvokeClient.Instance.Service.DoOperation($"{LinmotName}.Stop");
        }
    }
}