using Aitex.Core.Common;
using LiveCharts;
using MECF.Framework.Common.OperationCenter;
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
{
    /// <summary>
    /// TemperatureControllerControl.xaml 的交互逻辑
    /// </summary>
    public partial class TemperatureControllerControl : UserControl
    {
        #region 属性
        public static readonly DependencyProperty ModuleNameProperty = DependencyProperty.Register(
          "ModuleName", typeof(string), typeof(TemperatureControllerControl), new FrameworkPropertyMetadata("", FrameworkPropertyMetadataOptions.AffectsRender));
        /// <summary>
        /// 模块名称
        /// </summary>
        public string ModuleName
        {
            get
            {
                return (string)this.GetValue(ModuleNameProperty);
            }
            set
            {
                this.SetValue(ModuleNameProperty, value);
            }
        }
        public static readonly DependencyProperty ModuleTitleProperty = DependencyProperty.Register(
           "ModuleTitle", typeof(string), typeof(TemperatureControllerControl), new FrameworkPropertyMetadata("", FrameworkPropertyMetadataOptions.AffectsRender));
        /// <summary>
        /// 标题
        /// </summary>
        public string ModuleTitle
        {
            get
            {
                return (string)this.GetValue(ModuleTitleProperty);
            }
            set
            {
                this.SetValue(ModuleTitleProperty, value);
            }
        }
        public static readonly DependencyProperty DisableStatusProperty = DependencyProperty.Register(
          "DisableStatus", typeof(string), typeof(TemperatureControllerControl), new FrameworkPropertyMetadata("", FrameworkPropertyMetadataOptions.AffectsRender));
        /// <summary>
        /// 可用性状态
        /// </summary>
        public string DisableStatus
        {
            get
            {
                return (string)this.GetValue(DisableStatusProperty);
            }
            set
            {
                this.SetValue(DisableStatusProperty, value);
            }
        }
        public static readonly DependencyProperty IsEnableProperty = DependencyProperty.Register(
        "IsEnable", typeof(bool), typeof(TemperatureControllerControl), new FrameworkPropertyMetadata(false, FrameworkPropertyMetadataOptions.AffectsRender));
        /// <summary>
        ///IsEnable
        /// </summary>
        public bool IsEnable
        {
            get
            {
                return (bool)this.GetValue(IsEnableProperty);
            }
            set
            {
                this.SetValue(IsEnableProperty, value);
            }
        }
        public static readonly DependencyProperty IsApplyEnableProperty = DependencyProperty.Register(
            "IsApplyEnable", typeof(bool), typeof(TemperatureControllerControl), new FrameworkPropertyMetadata(false, FrameworkPropertyMetadataOptions.AffectsRender));
        /// <summary>
        ///IsApplyEnable
        /// </summary>
        public bool IsApplyEnable
        {
            get
            {
                return (bool)this.GetValue(IsApplyEnableProperty);
            }
            set
            {
                this.SetValue(IsApplyEnableProperty, value);
            }
        }
        public static readonly DependencyProperty StatusProperty = DependencyProperty.Register(
         "Status", typeof(string), typeof(TemperatureControllerControl), new FrameworkPropertyMetadata("Normal", FrameworkPropertyMetadataOptions.AffectsRender));
        /// <summary>
        /// 状态
        /// </summary>
        public string Status
        {
            get
            {
                return (string)this.GetValue(StatusProperty);
            }
            set
            {
                this.SetValue(StatusProperty, value);
            }
        }
        public static readonly DependencyProperty AutoStatusProperty = DependencyProperty.Register(
         "AutoStatus", typeof(string), typeof(TemperatureControllerControl), new FrameworkPropertyMetadata("Auto", FrameworkPropertyMetadataOptions.AffectsRender));
        /// <summary>
        /// 自动性状态
        /// </summary>
        public string AutoStatus
        {
            get
            {
                return (string)this.GetValue(AutoStatusProperty);
            }
            set
            {
                this.SetValue(AutoStatusProperty, value);
            }
        }

        public static readonly DependencyProperty IsConnectedProperty = DependencyProperty.Register(
         "IsConnected", typeof(bool), typeof(TemperatureControllerControl), new FrameworkPropertyMetadata(false, FrameworkPropertyMetadataOptions.AffectsRender));
        /// <summary>
        /// IsConnected
        /// </summary>
        public bool IsConnected
        {
            get
            {
                return (bool)this.GetValue(IsConnectedProperty);
            }
            set
            {
                this.SetValue(IsConnectedProperty, value);
            }
        }
        public static readonly DependencyProperty TemperatureReachedProperty = DependencyProperty.Register(
          "TemperatureReached", typeof(bool), typeof(TemperatureControllerControl), new FrameworkPropertyMetadata(false, FrameworkPropertyMetadataOptions.AffectsRender));
        /// <summary>
        /// TemperatureReached
        /// </summary>
        public bool TemperatureReached
        {
            get
            {
                return (bool)this.GetValue(TemperatureReachedProperty);
            }
            set
            {
                this.SetValue(TemperatureReachedProperty, value);
            }
        }
        public static readonly DependencyProperty DeltaExceededProperty = DependencyProperty.Register(
          "DeltaExceeded", typeof(bool), typeof(TemperatureControllerControl), new FrameworkPropertyMetadata(false, FrameworkPropertyMetadataOptions.AffectsRender));
        /// <summary>
        /// DeltaExceeded
        /// </summary>
        public bool DeltaExceeded
        {
            get
            {
                return (bool)this.GetValue(DeltaExceededProperty);
            }
            set
            {
                this.SetValue(DeltaExceededProperty, value);
            }
        }


        #region Alarm

        public static readonly DependencyProperty AlarmDataProperty = DependencyProperty.Register(
          "AlarmData", typeof(string), typeof(TemperatureControllerControl), new FrameworkPropertyMetadata(null, new PropertyChangedCallback(AlarmDataChanged)));
        /// <summary>
        /// AlarmData
        /// </summary>
        public string AlarmData
        {
            get
            {
                return (string)this.GetValue(AlarmDataProperty);
            }
            set
            {
                this.SetValue(AlarmDataProperty, value);
            }
        }
        private static void AlarmDataChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            if (e.NewValue != null)
            {
                string alarm = (string)e.NewValue;
                string[] strAry = alarm.Split('-');
                int result = 0;
                if (strAry.Length == 12)
                {
                    result += SetAlarmProperty(d, HighTempCutoffProperty, strAry[3] == "1");
                    result += SetAlarmProperty(d, LowTempCutoffProperty, strAry[2] == "1");
                    result += SetAlarmProperty(d, FanProperty, strAry[1] == "1");
                    result += SetAlarmProperty(d, OutputFailureProperty, strAry[0] == "1");
                    result += SetAlarmProperty(d, TempLimitWarnProperty, strAry[7] == "1");
                    result += SetAlarmProperty(d, RemoteOffProperty, strAry[6] == "1");
                    result += SetAlarmProperty(d, ThermostatProperty, strAry[5] == "1");
                    result += SetAlarmProperty(d, PowerFailureProperty, strAry[4] == "1");
                    result += SetAlarmProperty(d, ExtSensorFailureProperty, strAry[11] == "1");
                    result += SetAlarmProperty(d, IntSensorFailureProperty, strAry[10] == "1");
                    result += SetAlarmProperty(d, AutoTuningProperty, strAry[9] == "1");
                    result += SetAlarmProperty(d, LeakProperty, strAry[8] == "1");
                    //if (result == 0)
                    //{
                    //    d.SetValue(StatusProperty, "Normal");
                    //}
                    //else
                    //{
                    //    d.SetValue(StatusProperty, "Error");
                    //}
                }
            }
        }

        private static int SetAlarmProperty(DependencyObject d,DependencyProperty dependency, bool value)
        {
            d.SetValue(dependency, value);
            return value ? 1 : 0;
        }

        public static readonly DependencyProperty AutoTuningProperty = DependencyProperty.Register(
           "AutoTuning", typeof(bool), typeof(TemperatureControllerControl), new FrameworkPropertyMetadata(false, FrameworkPropertyMetadataOptions.AffectsRender));
        /// <summary>
        /// AutoTuning
        /// </summary>
        public bool AutoTuning
        {
            get
            {
                return (bool)this.GetValue(AutoTuningProperty);
            }
            set
            {
                this.SetValue(AutoTuningProperty, value);
            }
        }
        public static readonly DependencyProperty FanProperty = DependencyProperty.Register(
          "Fan", typeof(bool), typeof(TemperatureControllerControl), new FrameworkPropertyMetadata(false, FrameworkPropertyMetadataOptions.AffectsRender));
        /// <summary>
        /// Fan
        /// </summary>
        public bool Fan
        {
            get
            {
                return (bool)this.GetValue(FanProperty);
            }
            set
            {
                this.SetValue(FanProperty, value);
            }
        }

        public static readonly DependencyProperty RemoteOffProperty = DependencyProperty.Register(
          "RemoteOff", typeof(bool), typeof(TemperatureControllerControl), new FrameworkPropertyMetadata(false, FrameworkPropertyMetadataOptions.AffectsRender));
        /// <summary>
        /// RemoteOff
        /// </summary>
        public bool RemoteOff
        {
            get
            {
                return (bool)this.GetValue(RemoteOffProperty);
            }
            set
            {
                this.SetValue(RemoteOffProperty, value);
            }
        }
        public static readonly DependencyProperty ExtSensorFailureProperty = DependencyProperty.Register(
         "ExtSensorFailure", typeof(bool), typeof(TemperatureControllerControl), new FrameworkPropertyMetadata(false, FrameworkPropertyMetadataOptions.AffectsRender));
        /// <summary>
        /// ExtSensorFailure
        /// </summary>
        public bool ExtSensorFailure
        {
            get
            {
                return (bool)this.GetValue(ExtSensorFailureProperty);
            }
            set
            {
                this.SetValue(ExtSensorFailureProperty, value);
            }
        }
        public static readonly DependencyProperty HighTempCutoffProperty = DependencyProperty.Register(
           "HighTempCutoff", typeof(bool), typeof(TemperatureControllerControl), new FrameworkPropertyMetadata(false, FrameworkPropertyMetadataOptions.AffectsRender));
        /// <summary>
        /// HighTempCutoff
        /// </summary>
        public bool HighTempCutoff
        {
            get
            {
                return (bool)this.GetValue(HighTempCutoffProperty);
            }
            set
            {
                this.SetValue(HighTempCutoffProperty, value);
            }
        }
        public static readonly DependencyProperty LowTempCutoffProperty = DependencyProperty.Register(
           "LowTempCutoff", typeof(bool), typeof(TemperatureControllerControl), new FrameworkPropertyMetadata(false, FrameworkPropertyMetadataOptions.AffectsRender));
        /// <summary>
        /// LowTempCutoff
        /// </summary>
        public bool LowTempCutoff
        {
            get
            {
                return (bool)this.GetValue(LowTempCutoffProperty);
            }
            set
            {
                this.SetValue(LowTempCutoffProperty, value);
            }
        }
        public static readonly DependencyProperty OutputFailureProperty = DependencyProperty.Register(
           "OutputFailure", typeof(bool), typeof(TemperatureControllerControl), new FrameworkPropertyMetadata(false, FrameworkPropertyMetadataOptions.AffectsRender));
        /// <summary>
        /// OutputFailure
        /// </summary>
        public bool OutputFailure
        {
            get
            {
                return (bool)this.GetValue(OutputFailureProperty);
            }
            set
            {
                this.SetValue(OutputFailureProperty, value);
            }
        }
        public static readonly DependencyProperty IntSensorFailureProperty = DependencyProperty.Register(
           "IntSensorFailure", typeof(bool), typeof(TemperatureControllerControl), new FrameworkPropertyMetadata(false, FrameworkPropertyMetadataOptions.AffectsRender));
        /// <summary>
        /// IntSensorFailure
        /// </summary>
        public bool IntSensorFailure
        {
            get
            {
                return (bool)this.GetValue(IntSensorFailureProperty);
            }
            set
            {
                this.SetValue(IntSensorFailureProperty, value);
            }
        }
        public static readonly DependencyProperty TempLimitWarnProperty = DependencyProperty.Register(
           "TempLimitWarn", typeof(bool), typeof(TemperatureControllerControl), new FrameworkPropertyMetadata(false, FrameworkPropertyMetadataOptions.AffectsRender));
        /// <summary>
        /// TempLimitWarn
        /// </summary>
        public bool TempLimitWarn
        {
            get
            {
                return (bool)this.GetValue(TempLimitWarnProperty);
            }
            set
            {
                this.SetValue(TempLimitWarnProperty, value);
            }
        }
        public static readonly DependencyProperty PowerFailureProperty = DependencyProperty.Register(
           "PowerFailure", typeof(bool), typeof(TemperatureControllerControl), new FrameworkPropertyMetadata(false, FrameworkPropertyMetadataOptions.AffectsRender));
        /// <summary>
        /// PowerFailure
        /// </summary>
        public bool PowerFailure
        {
            get
            {
                return (bool)this.GetValue(PowerFailureProperty);
            }
            set
            {
                this.SetValue(PowerFailureProperty, value);
            }
        }
        public static readonly DependencyProperty LeakProperty = DependencyProperty.Register(
           "Leak", typeof(bool), typeof(TemperatureControllerControl), new FrameworkPropertyMetadata(false, FrameworkPropertyMetadataOptions.AffectsRender));
        /// <summary>
        /// Leak
        /// </summary>
        public bool Leak
        {
            get
            {
                return (bool)this.GetValue(LeakProperty);
            }
            set
            {
                this.SetValue(LeakProperty, value);
            }
        }
        public static readonly DependencyProperty ThermostatProperty = DependencyProperty.Register(
           "Thermostat", typeof(bool), typeof(TemperatureControllerControl), new FrameworkPropertyMetadata(false, FrameworkPropertyMetadataOptions.AffectsRender));
        /// <summary>
        /// Thermostat
        /// </summary>
        public bool Thermostat
        {
            get
            {
                return (bool)this.GetValue(ThermostatProperty);
            }
            set
            {
                this.SetValue(ThermostatProperty, value);
            }
        }
        #endregion

        public static readonly DependencyProperty SetPointValueProperty = DependencyProperty.Register(
         "SetPointValue", typeof(double), typeof(TemperatureControllerControl), new FrameworkPropertyMetadata((double)0, FrameworkPropertyMetadataOptions.AffectsRender));
        /// <summary>
        /// SetPointValue
        /// </summary>
        public double SetPointValue
        {
            get
            {
                return (double)this.GetValue(SetPointValueProperty);
            }
            set
            {
                this.SetValue(SetPointValueProperty, value);
            }
        }
        public static readonly DependencyProperty SetPointMinValueProperty = DependencyProperty.Register(
      "SetPointMinValue", typeof(double), typeof(TemperatureControllerControl), new FrameworkPropertyMetadata((double)0, FrameworkPropertyMetadataOptions.AffectsRender));
        /// <summary>
        /// SetPointMinValue
        /// </summary>
        public double SetPointMinValue
        {
            get
            {
                return (double)this.GetValue(SetPointMinValueProperty);
            }
            set
            {
                this.SetValue(SetPointMinValueProperty, value);
            }
        }
        public static readonly DependencyProperty SetPointMaxValueProperty = DependencyProperty.Register(
      "SetPointMaxValue", typeof(double), typeof(TemperatureControllerControl), new FrameworkPropertyMetadata((double)0, FrameworkPropertyMetadataOptions.AffectsRender));
        /// <summary>
        /// SetPointMaxValue
        /// </summary>
        public double SetPointMaxValue
        {
            get
            {
                return (double)this.GetValue(SetPointMaxValueProperty);
            }
            set
            {
                this.SetValue(SetPointMaxValueProperty, value);
            }
        }

        public static readonly DependencyProperty TargetTemperatureValueProperty = DependencyProperty.Register(
    "TargetTemperatureValue", typeof(double), typeof(TemperatureControllerControl), new FrameworkPropertyMetadata((double)0, FrameworkPropertyMetadataOptions.AffectsRender));
        /// <summary>
        /// TargetTemperatureValue
        /// </summary>
        public double TargetTemperatureValue
        {
            get
            {
                return (double)this.GetValue(TargetTemperatureValueProperty);
            }
            set
            {
                this.SetValue(TargetTemperatureValueProperty, value);
            }
        }
        public static readonly DependencyProperty ReserviorValueProperty = DependencyProperty.Register(
   "ReserviorValue", typeof(double), typeof(TemperatureControllerControl), new FrameworkPropertyMetadata((double)0, FrameworkPropertyMetadataOptions.AffectsRender));
        /// <summary>
        /// ReserviorValue
        /// </summary>
        public double ReserviorValue
        {
            get
            {
                return (double)this.GetValue(ReserviorValueProperty);
            }
            set
            {
                this.SetValue(ReserviorValueProperty, value);
            }
        }
        public static readonly DependencyProperty HeatExchangerValueProperty = DependencyProperty.Register(
   "HeatExchangerValue", typeof(double), typeof(TemperatureControllerControl), new FrameworkPropertyMetadata((double)0, FrameworkPropertyMetadataOptions.AffectsRender));
        /// <summary>
        /// HeatExchangerValue
        /// </summary>
        public double HeatExchangerValue
        {
            get
            {
                return (double)this.GetValue(HeatExchangerValueProperty);
            }
            set
            {
                this.SetValue(HeatExchangerValueProperty, value);
            }
        }

        #region 曲线
        public static readonly DependencyProperty ReserviorSeriesProperty = DependencyProperty.Register("ReserviorSeries", typeof(ChartValues<double>), typeof(TemperatureControllerControl),
            new FrameworkPropertyMetadata(new ChartValues<double>(), FrameworkPropertyMetadataOptions.AffectsRender));
        /// <summary>
        /// ReserviorSeries
        /// </summary>
        public ChartValues<double> ReserviorSeries
        {
            get
            {
                return (ChartValues<double>)this.GetValue(ReserviorSeriesProperty);
            }
            set
            {
                this.SetValue(ReserviorSeriesProperty, value);
            }
        }
        public static readonly DependencyProperty HeatExchangerSeriesProperty = DependencyProperty.Register("HeatExchangerSeries", typeof(ChartValues<double>), typeof(TemperatureControllerControl),
            new FrameworkPropertyMetadata(new ChartValues<double>(), FrameworkPropertyMetadataOptions.AffectsRender));
        /// <summary>
        /// HeatExchangerSeries
        /// </summary>
        public ChartValues<double> HeatExchangerSeries
        {
            get
            {
                return (ChartValues<double>)this.GetValue(HeatExchangerSeriesProperty);
            }
            set
            {
                this.SetValue(HeatExchangerSeriesProperty, value);
            }
        }
        public static readonly DependencyProperty LabelSeriesProperty = DependencyProperty.Register("LabelSeries", typeof(ChartValues<string>), typeof(TemperatureControllerControl),
            new FrameworkPropertyMetadata(new ChartValues<string>(), FrameworkPropertyMetadataOptions.AffectsRender));
        /// <summary>
        /// LabelSeries
        /// </summary>
        public ChartValues<string> LabelSeries
        {
            get
            {
                return (ChartValues<string>)this.GetValue(LabelSeriesProperty);
            }
            set
            {
                this.SetValue(LabelSeriesProperty, value);
            }
        }

        #endregion

        #endregion
        /// <summary>
        /// 构造函数
        /// </summary>
        public TemperatureControllerControl()
        {
            InitializeComponent();
        }
        #region Button Click
        private void Disable_Click(object sender, RoutedEventArgs e)
        {
            InvokeClient.Instance.Service.DoOperation($"{ModuleName}.Disable");
        }
        private void Enable_Click(object sender, RoutedEventArgs e)
        {
            InvokeClient.Instance.Service.DoOperation($"{ModuleName}.Enable");
        }
        private void Apply_Click(object sender, RoutedEventArgs e)
        {
            InvokeClient.Instance.Service.DoOperation($"{ModuleName}.Apply", SetPointValue, SetPointMinValue,SetPointMaxValue);
        }
        #endregion

    }
}