using MECF.Framework.Common.CommonData.Reservoir;
using MECF.Framework.Common.OperationCenter;
using MECF.Framework.Common.Persistent.Reservoirs;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Reflection;
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>
    /// CalibrateControl.xaml 的交互逻辑
    /// </summary>
    public partial class CalibrateControl : UserControl
    {
        public CalibrateControl()
        {
            InitializeComponent();
        }

        public static readonly DependencyProperty ModuleNameProperty = DependencyProperty.Register(
           "ModuleName", typeof(string), typeof(CalibrateControl), new FrameworkPropertyMetadata("", FrameworkPropertyMetadataOptions.AffectsRender));
        /// <summary>
        /// 模块名称
        /// </summary>
        public string ModuleName
        {
            get
            {
                return (string)this.GetValue(ModuleNameProperty);
            }
            set
            {
                this.SetValue(ModuleNameProperty, value);
            }
        }

        public static readonly DependencyProperty ReplenDatasProperty = DependencyProperty.Register(
           "ReplenDatas", typeof(ObservableCollection<ReplenData>), typeof(CalibrateControl), new FrameworkPropertyMetadata(null, FrameworkPropertyMetadataOptions.AffectsRender));
        /// <summary>
        /// ReplenDatas
        /// </summary>
        public ObservableCollection<ReplenData> ReplenDatas
        {
            get
            {
                return (ObservableCollection<ReplenData>)this.GetValue(ReplenDatasProperty);
            }
            set
            {
                this.SetValue(ReplenDatasProperty, value);
            }
        }
        public static readonly DependencyProperty ReplenNumProperty = DependencyProperty.Register(
           "ReplenNum", typeof(int), typeof(CalibrateControl), new FrameworkPropertyMetadata(0, FrameworkPropertyMetadataOptions.AffectsRender));
        /// <summary>
        /// Replen数量
        /// </summary>
        public int ReplenNum
        {
            get
            {
                return (int)this.GetValue(ReplenNumProperty);
            }
            set
            {
                this.SetValue(ReplenNumProperty, value);
            }
        }
        public static readonly DependencyProperty ReplensPersistentCollectionProperty = DependencyProperty.Register(
           "ReplensPersistentCollection", typeof(ObservableCollection<ReplenPersistentValue>), typeof(CalibrateControl), new FrameworkPropertyMetadata(null, FrameworkPropertyMetadataOptions.AffectsRender));
        /// <summary>
        /// Replen Persistent数据集合
        /// </summary>
        public ObservableCollection<ReplenPersistentValue> ReplensPersistentCollection
        {
            get
            {
                return (ObservableCollection<ReplenPersistentValue>)this.GetValue(ReplensPersistentCollectionProperty);
            }
            set
            {
                this.SetValue(ReplensPersistentCollectionProperty, value);
            }
        }

        private void Calibrate1_Click(object sender, RoutedEventArgs e)
        {          
            if (MeasuredVolumeBox.Text != "" && double.Parse(MeasuredVolumeBox.Text) != 0)
            {
                double pumpFactor = double.Parse(TargetVolumeBox.Text) / double.Parse(MeasuredVolumeBox.Text);
                InvokeClient.Instance.Service.DoOperation($"{ModuleName}.SetPumpFactor", ReplenDatas[0].ReplenName, Math.Round(pumpFactor, 3));
            }
            else
            {
                MessageBox.Show("MeasuredVolume can't be set as zero", "Calibrate1", MessageBoxButton.OK, MessageBoxImage.Error);
            }
       
        }

        private void Calibrate2_Click(object sender, RoutedEventArgs e)
        {
            if (MeasuredVolumeBox.Text != "" && double.Parse(MeasuredVolumeBox.Text) != 0)
            {
                double pumpFactor = double.Parse(TargetVolumeBox.Text) / double.Parse(MeasuredVolumeBox.Text);
                InvokeClient.Instance.Service.DoOperation($"{ModuleName}.SetPumpFactor", ReplenDatas[1].ReplenName, Math.Round(pumpFactor, 3));
            }
            else
            {
                MessageBox.Show("MeasuredVolume can't be set as zero", "Calibrate2", MessageBoxButton.OK, MessageBoxImage.Error);
            }
        }
        private void Calibrate3_Click(object sender, RoutedEventArgs e)
        {
            if (MeasuredVolumeBox.Text != "" && double.Parse(MeasuredVolumeBox.Text) != 0)
            {
                double pumpFactor = double.Parse(TargetVolumeBox.Text) / double.Parse(MeasuredVolumeBox.Text);
                InvokeClient.Instance.Service.DoOperation($"{ModuleName}.SetPumpFactor", ReplenDatas[2].ReplenName, Math.Round(pumpFactor, 3));
            }
            else
            {
                MessageBox.Show("MeasuredVolume can't be set as zero", "Calibrate3", MessageBoxButton.OK, MessageBoxImage.Error);
            }
        }
        private void Calibrate4_Click(object sender, RoutedEventArgs e)
        {
            if (MeasuredVolumeBox.Text != "" && double.Parse(MeasuredVolumeBox.Text) != 0)
            {
                double pumpFactor = double.Parse(TargetVolumeBox.Text) / double.Parse(MeasuredVolumeBox.Text);
                InvokeClient.Instance.Service.DoOperation($"{ModuleName}.SetPumpFactor", ReplenDatas[3].ReplenName, Math.Round(pumpFactor, 3));
            }
            else
            {
                MessageBox.Show("MeasuredVolume can't be set as zero", "Calibrate4", MessageBoxButton.OK, MessageBoxImage.Error);
            }
        }
    }
}