using CommunityToolkit.Mvvm.Input;
using FLY.Thick.FilmCasting.Common;
using FLY.Thick.FilmCasting.IService;
using System;
using System.ComponentModel;
using System.Windows;


namespace FLY.Thick.FilmCasting.UI
{
    /// <summary>
    /// WdShrinkHelper.xaml 的交互逻辑
    /// </summary>
    public partial class WdShrinkHelper : FLY.ControlLibrary.WindowBigClose
    {
        WdShrinkHelperVm viewModel;
        public WdShrinkHelper()
        {
            InitializeComponent();

        }

        public void Init(IBoltMapFilmCastingService boltMapFilmCastingService, FilmCastingProfileParam set_param)
        {
            viewModel = new WdShrinkHelperVm();

            viewModel.Init(boltMapFilmCastingService, set_param);


            this.DataContext = viewModel;
        }

        private void button_apply_Click(object sender, RoutedEventArgs e)
        {
            viewModel.Apply();
            this.DialogResult = true;
            this.Close();
        }

    }
    public class WdShrinkHelperVm : INotifyPropertyChanged
    {

        /// <summary>
        /// 真实螺丝位1
        /// </summary>
        public double RealBoltNo1 { get; set; }

        /// <summary>
        /// 真实螺丝位2
        /// </summary>
        public double RealBoltNo2 { get; set; }

        /// <summary>
        /// 测厚仪显示螺丝位1
        /// </summary>
        public double ShowBoltNo1 { get; set; }

        /// <summary>
        /// 测厚仪显示螺丝位2
        /// </summary>
        public double ShowBoltNo2 { get; set; }

        /// <summary>
        /// 计算出来的收缩率
        /// </summary>
        public double UniformShrinkPercent { get; set; }
        public int MidOffset { get; set; }
        public RelayCommand CalCmd { get; private set; }

        double midBoltNo;


        IBoltMapFilmCastingService boltMapFilmCastingService;
        FilmCastingProfileParam set_param;
        public WdShrinkHelperVm()
        {
            CalCmd = new RelayCommand(Cal);
        }
        public void Init(IBoltMapFilmCastingService boltMapFilmCastingService, FilmCastingProfileParam set_param)
        {
            this.boltMapFilmCastingService = boltMapFilmCastingService;
            this.set_param = set_param;
            midBoltNo = boltMapFilmCastingService.LeftEmptyBolts + (boltMapFilmCastingService.BoltCnt - (boltMapFilmCastingService.LeftEmptyBolts + boltMapFilmCastingService.RightEmptyBolts)) / 2.0;

            RealBoltNo2 = (int)midBoltNo;
            ShowBoltNo2 = (int)midBoltNo;
            UniformShrinkPercent = this.boltMapFilmCastingService.UniformShrinkPercent;
            MidOffset = this.boltMapFilmCastingService.MidOffset;
        }
        private void Cal()
        {
            //还原
            double showBoltNo1 = ToShrink0ShowBoltNo(ShowBoltNo1);
            double showBoltNo2 = ToShrink0ShowBoltNo(ShowBoltNo2);
            double shrink = 1 - (showBoltNo2 - showBoltNo1) / (RealBoltNo2 - RealBoltNo1);
            UniformShrinkPercent = shrink;

            double showMidBoltNo = (1 - shrink) * (midBoltNo - RealBoltNo1) + showBoltNo1;
            double midOffset = (showMidBoltNo - midBoltNo) * boltMapFilmCastingService.BoltWidth + boltMapFilmCastingService.MidOffset;
            MidOffset = (int)midOffset;

        }
        /// <summary>
        /// 转为收缩率为0时，显示的螺丝号
        /// </summary>
        /// <param name="currBoltNo"></param>
        /// <returns></returns>
        double ToShrink0ShowBoltNo(double currBoltNo)
        {
            double orgBoltNo = midBoltNo - (1 - boltMapFilmCastingService.UniformShrinkPercent) * (midBoltNo - currBoltNo);
            return orgBoltNo;
        }
        public void Apply()
        {
            this.set_param.UniformShrinkPercent = Math.Round(UniformShrinkPercent, 3);

            if (Math.Abs(MidOffset) < boltMapFilmCastingService.BoltWidth)
            {
                this.set_param.MidOffset = 0;
            }
            else
            {
                this.set_param.MidOffset = MidOffset;
            }
        }
        public event PropertyChangedEventHandler PropertyChanged;
    }
}
