using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
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 Aitex.Core.Common.DeviceData;
using Aitex.Core.UI.Control;
using MECF.Framework.Common.DataCenter;
using MECF.Framework.Common.OperationCenter;
namespace Aitex.Core.UI.DeviceControl
{
    /// 
    /// AITChillerControl.xaml 的交互逻辑
    /// 
    public partial class AITChillerControl : UserControl
    {
        public AITChillerControl()
        {
            InitializeComponent();
        }
        // define dependency properties
        public static readonly DependencyProperty CommandProperty = DependencyProperty.Register(
                        "Command", typeof(ICommand), typeof(AITChillerControl),
                        new FrameworkPropertyMetadata(null, FrameworkPropertyMetadataOptions.AffectsRender));
        public static readonly DependencyProperty DeviceDataProperty = DependencyProperty.Register(
                        "DeviceData", typeof(AITChillerData), typeof(AITChillerControl),
                        new FrameworkPropertyMetadata(new AITChillerData(), FrameworkPropertyMetadataOptions.AffectsRender));
        public static readonly DependencyProperty BackColorProperty = DependencyProperty.Register(
                        "BackColor", typeof(Brush), typeof(AITChillerControl),
                         new FrameworkPropertyMetadata(Brushes.DarkMagenta, FrameworkPropertyMetadataOptions.AffectsRender));
        public static readonly DependencyProperty IsAutoModeProperty = DependencyProperty.Register(
                        "IsAutoMode", typeof(bool), typeof(AITChillerControl),
                        new FrameworkPropertyMetadata(false, FrameworkPropertyMetadataOptions.AffectsRender));
        /// 
        /// 输入值是否百分比,默认否
        /// 
        public bool IsPercent { get; set; }
        public ICommand Command
        {
            get
            {
                return (ICommand)this.GetValue(CommandProperty);
            }
            set
            {
                this.SetValue(CommandProperty, value);
            }
        }
        /// 
        /// set, get current progress value AnalogDeviceData
        /// 
        public AITChillerData DeviceData
        {
            get
            {
                return (AITChillerData)this.GetValue(DeviceDataProperty);
            }
            set
            {
                this.SetValue(DeviceDataProperty, value);
            }
        }
        public Brush BackColor
        {
            get
            {
                return (Brush)this.GetValue(BackColorProperty);
            }
            set
            {
                this.SetValue(BackColorProperty, value);
            }
        }
        public bool IsAutoMode
        {
            get
            {
                return (bool)this.GetValue(IsAutoModeProperty);
            }
            set
            {
                this.SetValue(IsAutoModeProperty, value);
            }
        }
        protected override void OnRender(DrawingContext drawingContext)
        {
            base.OnRender(drawingContext);
            //draw background color
            if (DeviceData != null)
            {
                rectBkground.Fill = DeviceData.IsPowerOn ? Brushes.DarkMagenta : Brushes.Gray;
                //draw red board if mfc meets a warning
                rectBkground.Stroke = DeviceData.IsWarning ? Brushes.Red : new SolidColorBrush(System.Windows.Media.Color.FromRgb(0X37, 0X37, 0X37));
                labelValue.Foreground = DeviceData.IsWarning ? Brushes.Pink : Brushes.LightYellow;
                rectBkground.StrokeThickness = DeviceData.IsWarning ? 2 : 1;
                if (dialogBox != null)
                {
                    dialogBox.RealValue = DeviceData.FeedBack.ToString("F1");
                    dialogBox.IsHeaterOn = DeviceData.IsPowerOn;
                    dialogBox.SetPoint = DeviceData.SetPoint;
                }
            }
        }
        private void ExecutePowerOnOff(bool value)
        {
            if (Command == null)
            {
                InvokeClient.Instance.Service.DoOperation($"{DeviceData.Module}.{DeviceData.DeviceName}.SetPowerOnOff", value.ToString());
            }
            else
            {
                Command.Execute(new object[] { DeviceData.DeviceName, AITHeaterOperation.SetPowerOnOff.ToString(), value.ToString() });
            }
        }
        private void ExecuteSetHeaterValue(double power)
        {
            if (Command == null)
            {
                InvokeClient.Instance.Service.DoOperation($"{DeviceData.Module}.{DeviceData.DeviceName}.Ramp",
                    power.ToString(), ((double)QueryDataClient.Instance.Service.GetConfig($"{DeviceData.Module}.{DeviceData.DeviceName}.ChillerTemperatureOffset")).ToString());
            }
            else
            {
                Command.Execute(new object[]
                    {DeviceData.DeviceName, AITHeaterOperation.Ramp.ToString(), power.ToString()});
            }
        }
        private AITHeaterInputDialogBox dialogBox;
        public Window AnalogOwner { get; set; }
        private void Grid_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
        {
            if (IsAutoMode) return;
            if (DeviceData == null)
                return;
            dialogBox = new AITHeaterInputDialogBox
            {
                SetHeaterValueCommandDelegate = ExecuteSetHeaterValue,
                SetHeaterPowerOnOffCommandDelegate = ExecutePowerOnOff,
                DeviceName = DeviceData.DisplayName,
                DeviceId = DeviceData.DeviceSchematicId,
                DefaultValue = DeviceData.DefaultValue,
                RealValue = DeviceData.FeedBack.ToString("F1"),
                SetPoint = Math.Round(DeviceData.SetPoint, 1),
                MaxValue = DeviceData.ScaleMax,
                MinValue = DeviceData.ScaleMin,
                Unit = DeviceData.Unit,
                IsHeaterOn = DeviceData.IsPowerOn,
                HeaterOffName = "Device Off",
                HeaterOnName = "Device On",
            };
            if (AnalogOwner != null)
                dialogBox.Owner = AnalogOwner;
            dialogBox.Topmost = true;
            dialogBox.WindowStartupLocation = WindowStartupLocation.CenterScreen;
            dialogBox.FocasAll();
            dialogBox.ShowDialog();
            dialogBox = null;
        }
        private void Grid_MouseEnter(object sender, MouseEventArgs e)
        {
            if (DeviceData != null)
            {
                string tooltipValue =
                    string.Format("{0}:{1}\r\n\r\nID:{2}\r\nScaleMax:{3} {5}\r\nScaleMin:{4} {5}\r\nSetPoint:{6} {5} \r\nFeedback:{7} {5}\r\nHeaterPower:{8}",
                        DeviceData.Type,
                        DeviceData.DisplayName,
                        DeviceData.DeviceSchematicId,
                        DeviceData.ScaleMax,
                        DeviceData.ScaleMin,
                        DeviceData.Unit,
                        DeviceData.SetPoint.ToString("F1"),
                        DeviceData.FeedBack.ToString("F1"),
                        DeviceData.IsPowerOn ? "On" : "Off");
                ToolTip = tooltipValue;
            }
        }
    }
}