using Misc;
using System;
using System.Collections.Generic;
using System.ComponentModel;
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.Shapes;


namespace FLY.Thick.Base.UI
{
    /// <summary>
    /// Window_AHelper.xaml 的交互逻辑
    /// </summary>
    public partial class WdAbHelper2 : FLY.ControlLibrary.WindowBigClose
    {
        WdAbHelperVm viewModel;
        public double A { get { return viewModel.A; } }
        public double B { get { return viewModel.B; } }

        //外部值
        private double a;
        private double b;

        public WdAbHelper2()
        {
            InitializeComponent();
            viewModel = new WdAbHelperVm();
            this.DataContext = viewModel;
        }

        public void Init(double a,double b, ParamDictionary paramDictionary)
        {
            this.a = a;
            this.b = b;

            viewModel.A = a;
            viewModel.B = b;

            paramDictionary.SetBinding(viewModel, nameof(viewModel.IsJustB), "AbHelper.IsJustB", true);

        }

        private void button_cal_Click(object sender, RoutedEventArgs e)
        {
            if (viewModel.IsJustB)
            {
                //只是平移补偿
                //只有X1,Y1
                viewModel.A = a;
                viewModel.B = viewModel.Y1 - viewModel.X1 + b;
            }
            else
            {

                if ((viewModel.X2 == viewModel.X1) && (viewModel.Y2 != viewModel.Y1))
                {
                    FLY.ControlLibrary.Window_WarningTip.Show("异常!!!!",
                        "X2 == X1 但 Y2 != Y1",
                        TimeSpan.FromSeconds(2));
                    return;
                }
                if ((viewModel.X2 != viewModel.X1) && (viewModel.Y2 == viewModel.Y1))
                {
                    FLY.ControlLibrary.Window_WarningTip.Show("异常!!!!",
                        "X2 != X1 但 Y2 == Y1",
                        TimeSpan.FromSeconds(2));
                    return;
                }
                else if (a == 0)
                {
                    FLY.ControlLibrary.Window_WarningTip.Show("异常!!!!",
                        "A==0",
                        TimeSpan.FromSeconds(2));
                    return;
                }

                if ((viewModel.X2 == viewModel.X1) && (viewModel.Y2 == viewModel.Y1))
                {
                    viewModel.A = a;
                    viewModel.B = viewModel.Y2 - viewModel.X2 + b;
                }
                else
                {
                    double x2 = (viewModel.X2 - b) / a;
                    double x1 = (viewModel.X1 - b) / a;

                    viewModel.A = (viewModel.Y2 - viewModel.Y1) / (x2 - x1);
                    viewModel.B = viewModel.Y2 - viewModel.A * x2;
                }
            }
            FLY.ControlLibrary.Window_Tip.Show("成功",
                "计算成功",
                TimeSpan.FromSeconds(2));
        }
        private void button_apply_Click(object sender, RoutedEventArgs e)
        {
            this.DialogResult = true;
        }

    }
}
