using CommunityToolkit.Mvvm.Input;
using FLY.Thick.Base.IService;
using System;
using System.ComponentModel;
using System.Linq;
using System.Windows.Controls;
using Unity;

namespace FLY.Thick.Base.UI
{
    /// <summary>
    /// Page_GetSample.xaml 的交互逻辑
    /// </summary>
    public partial class PgGetSample : Page
    {
        IGetSampleService getSampleService;
        IInitParamService initParamService;

        PgGetSampleVm viewModel;

        public PgGetSample()
        {
            InitializeComponent();
        }
        [InjectionMethod]
        public void Init(
            IGetSampleService getSampleService,
            IInitParamService initParamService,
            string infoName = null)
        {

            this.getSampleService = getSampleService;
            this.initParamService = initParamService;

            viewModel = new PgGetSampleVm();
            viewModel.Init(getSampleService, infoName);
            this.DataContext = viewModel;
            this.grid_initparam.DataContext = this.initParamService;
        }
    }

    public class PgGetSampleVm : INotifyPropertyChanged
    {
        #region Markno
        const int MARKNO_UPDATE_SAMPLE = 1;
        const int MARKNO_UPDATE_FEATURE = 2;

        #endregion
        #region 参数

        /// <summary>
        /// 参数：使能
        /// </summary>
        public bool Enable { get; set; }

        /// <summary>
        /// 参数：样品点范围
        /// </summary>
        public int SampleRange { get; set; }

        /// <summary>
        /// 参数：移动滤波 ，窗口值
        /// </summary>
        public int Window { get; set; }

        /// <summary>
        /// 使用%方式检查异常
        /// </summary>
        public bool IsCheckByPercent { get; set; }
        /// <summary>
        /// 异常%
        /// </summary>
        public double ErrPercent { get; set; }
        /// <summary>
        /// 异常值
        /// </summary>
        public int ErrValue { get; set; }

        /// <summary>
        /// 当前 样品X_AD/样品0_AD 与 上一次比较 差异比例 单位不是%
        /// </summary>
        public double CrossErrPercent { get; set; }

        /// <summary>
        /// 参数：样品点参数
        /// </summary>
        public SampleCellView[] Samples { get; set; }
        /// <summary>
        /// 参数：特征查找范围
        /// </summary>
        public int SearchRange { get; set; }
        /// <summary>
        /// 参数：特征参数
        /// </summary>
        public SampleFeatureView[] Features { get; set; }

        #endregion

        #region Command
        public RelayCommand ApplyCmd { get; private set; }
        #endregion

        public event PropertyChangedEventHandler PropertyChanged;


        public string InfoName { get; set; }

        IGetSampleService getSampleService;

        public PgGetSampleVm()
        {
            ApplyCmd = new RelayCommand(Apply);
        }



        public void Init(
            IGetSampleService getSampleService,
            string infoName)
        {
            InfoName = infoName;
            this.getSampleService = getSampleService;
            Misc.BindingOperations.SetBinding(this.getSampleService, nameof(this.getSampleService.Enable), this, nameof(Enable));
            Misc.BindingOperations.SetBinding(this.getSampleService, nameof(this.getSampleService.SampleRange), this, nameof(SampleRange));
            Misc.BindingOperations.SetBinding(this.getSampleService, nameof(this.getSampleService.Window), this, nameof(Window));
            Misc.BindingOperations.SetBinding(this.getSampleService, nameof(this.getSampleService.IsCheckByPercent), this, nameof(IsCheckByPercent));
            Misc.BindingOperations.SetBinding(this.getSampleService, nameof(this.getSampleService.ErrPercent), this, nameof(ErrPercent));
            Misc.BindingOperations.SetBinding(this.getSampleService, nameof(this.getSampleService.ErrValue), this, nameof(ErrValue));
            Misc.BindingOperations.SetBinding(this.getSampleService, nameof(this.getSampleService.CrossErrPercent), this, nameof(CrossErrPercent));


            Misc.BindingOperations.SetBinding(this.getSampleService, nameof(this.getSampleService.SearchRange), this, nameof(SearchRange));

            Misc.BindingOperations.SetBinding(this.getSampleService, nameof(this.getSampleService.Samples), () =>
            {
                FObjBase.PollModule.Current.Poll_JustOnce(updateSamples, this, MARKNO_UPDATE_SAMPLE);
            });

            Misc.BindingOperations.SetBinding(this.getSampleService, nameof(this.getSampleService.Features), () =>
            {
                FObjBase.PollModule.Current.Poll_JustOnce(updateFeatures, this, MARKNO_UPDATE_FEATURE);
            });
        }

        void updateSamples()
        {
            var samples = this.getSampleService.Samples;

            if (samples == null)
            {
                Samples = null;
            }
            else
            {
                var sampleViews = new SampleCellView[samples.Count()];
                for (int i = 0; i < sampleViews.Count(); i++)
                {
                    sampleViews[i] = new SampleCellView() { Name = i.ToString() };
                    var sampleView = sampleViews[i];
                    var sample = samples[i];
                    Misc.BindingOperations.SetBinding(sample, nameof(SampleCell.Enable), sampleView, nameof(SampleCellView.Enable));
                    Misc.BindingOperations.SetBinding(sample, nameof(SampleCell.JustForCheck), sampleView, nameof(SampleCellView.JustForCheck));
                    Misc.BindingOperations.SetBinding(sample, nameof(SampleCell.OrgAD), sampleView, nameof(SampleCellView.OrgAD));
                    Misc.BindingOperations.SetBinding(sample, nameof(SampleCell.Position), sampleView, nameof(SampleCellView.Position));

                    Misc.BindingOperations.SetBinding(sample, nameof(SampleCell.AD), sampleView, nameof(SampleCellView.AD));
                    Misc.BindingOperations.SetBinding(sample, nameof(SampleCell.SampleValue), sampleView, nameof(SampleCellView.SampleValue));
                }
                updateSampleCellViewVisible(sampleViews);

                Samples = sampleViews;

                for (int i = 0; i < Samples.Count(); i++)
                {
                    var sampleView = Samples[i];
                    sampleView.PropertyChanged += (s, e) =>
                    {
                        if (e.PropertyName == nameof(SampleCellView.Enable))
                        {
                            updateSampleCellViewVisible();
                        }
                    };
                }
            }
        }
        void updateFeatures()
        {
            var features = this.getSampleService.Features;
            if (features == null)
            {
                Features = null;
            }
            else
            {
                int cnt = Math.Min(features.Count(), 2);
                var featureViews = new SampleFeatureView[cnt];
                for (int i = 0; i < featureViews.Count(); i++)
                {
                    featureViews[i] = new SampleFeatureView();
                    var featureView = featureViews[i];
                    if (i == 0)
                        featureView.Name = "正向";
                    else
                        featureView.Name = "反向";
                    var feature = features[i];
                    Misc.BindingOperations.SetBinding(feature, nameof(SampleFeature.Enable), featureView, nameof(SampleFeatureView.Enable));
                    Misc.BindingOperations.SetBinding(feature, nameof(SampleFeature.StartPos), featureView, nameof(SampleFeatureView.StartPos));
                    Misc.BindingOperations.SetBinding(feature, nameof(SampleFeature.EndPos), featureView, nameof(SampleFeatureView.EndPos));

                    Misc.BindingOperations.SetBinding(feature, nameof(SampleFeature.MaxRelevancy), featureView, nameof(SampleFeatureView.MaxRelevancy));
                    Misc.BindingOperations.SetBinding(feature, nameof(SampleFeature.MaxOffset), featureView, nameof(SampleFeatureView.MaxOffset));

                }
                Features = featureViews;
            }
        }
        protected void updateSampleCellViewVisible(SampleCellView[] sampleViews)
        {
            if (sampleViews == null)
                return;

            for (int i = 0; i < sampleViews.Count(); i++)
            {
                if (i == 0)
                    sampleViews[i].IsVisible = true;
                else
                {
                    sampleViews[i].IsVisible = sampleViews[i - 1].Enable;
                }
            }
        }
        protected void updateSampleCellViewVisible()
        {
            updateSampleCellViewVisible(Samples);
        }
        private void Apply()
        {
            if (!WdPassword.Authorize("GetSample"))
                return;



            this.getSampleService.Enable = this.Enable;
            this.getSampleService.SampleRange = this.SampleRange;
            this.getSampleService.Window = this.Window;
            this.getSampleService.IsCheckByPercent = this.IsCheckByPercent;
            this.getSampleService.ErrPercent = this.ErrPercent;
            this.getSampleService.ErrValue = this.ErrValue;
            this.getSampleService.CrossErrPercent = this.CrossErrPercent;


            for (int i = 0; i < Samples.Count(); i++)
            {
                var sample_src = this.Samples[i];
                var sample_desp = this.getSampleService.Samples[i];

                sample_desp.Enable = sample_src.Enable;
                sample_desp.JustForCheck = sample_src.JustForCheck;
                sample_desp.OrgAD = sample_src.OrgAD;
                sample_desp.Position = sample_src.Position;
            }
            this.getSampleService.SearchRange = this.SearchRange;
            for (int i = 0; i < Features.Count(); i++)
            {
                var feature_src = this.Features[i];
                var feature_desp = this.getSampleService.Features[i];


                feature_desp.Enable = feature_src.Enable;
                feature_desp.StartPos = feature_src.StartPos;
                feature_desp.EndPos = feature_src.EndPos;
            }

            getSampleService.Apply();
            FLY.ControlLibrary.Window_Tip.Show("应用成功",
                null,
                TimeSpan.FromSeconds(2));
        }


    }
    public class PgGetSampleVmUt : PgGetSampleVm
    {
        public PgGetSampleVmUt()
        {
            Samples = new SampleCellView[3];
            for (int i = 0; i < Samples.Count(); i++)
            {
                SampleCellView sampleCell = new SampleCellView() { Name = i.ToString() };
                Samples[i] = sampleCell;
            }

            Features = new SampleFeatureView[2];
            Features[0] = new SampleFeatureView() { Name = "正向" };
            Features[1] = new SampleFeatureView() { Name = "反向" };


            Enable = true;
            Samples[0].Enable = true;

            Features[0].Enable = true;

            updateSampleCellViewVisible();
        }
    }

    /// <summary>
    /// 样品点
    /// </summary>
    public class SampleCellView : INotifyPropertyChanged
    {
        /// <summary>
        /// 序号
        /// </summary>
        public string Name { get; set; }

        /// <summary>
        /// 显示出来
        /// </summary>
        public bool IsVisible { get; set; }

        /// <summary>
        /// 参数：使能
        /// </summary>
        public bool Enable { get; set; }

        /// <summary>
        /// 参数：只检查不标定
        /// </summary>
        public bool JustForCheck { get; set; }

        /// <summary>
        /// 参数：原始AD值
        /// </summary>
        public int OrgAD { get; set; }

        /// <summary>
        /// 参数：位置
        /// </summary>
        public int Position { get; set; }

        public int AD { get; set; }
        public double SampleValue { get; set; }


        #region INotifyPropertyChanged 成员

        public event PropertyChangedEventHandler PropertyChanged;

        #endregion

    }

    /// <summary>
    /// 样品正向校正特征
    /// </summary>
    public class SampleFeatureView : INotifyPropertyChanged
    {
        /// <summary>
        /// 序号
        /// </summary>
        public string Name { get; set; }

        /// <summary>
        /// 参数：使能
        /// </summary>
        public bool Enable { get; set; }


        /// <summary>
        /// 参数：开始位置
        /// </summary>
        public int StartPos { get; set; }


        /// <summary>
        /// 参数：结束位置
        /// </summary>
        public int EndPos { get; set; }

        public double MaxRelevancy { get; set; }
        public int MaxOffset { get; set; }
        #region INotifyPropertyChanged 成员

        public event PropertyChangedEventHandler PropertyChanged;


        #endregion
    }
}
