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 ThickTcpUiInWindow
{
    /// <summary>
    /// Window_AHelper.xaml 的交互逻辑
    /// </summary>
    public partial class Window_ABHelper : FLY.ControlLibrary.WindowBigClose
    {
        WindowABHelperViewModel viewModel;
        public double A { get { return viewModel.A; } }
        public double B { get { return viewModel.B; } }

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

        public Window_ABHelper()
        {
            InitializeComponent();
            viewModel = new WindowABHelperViewModel();
            this.DataContext = viewModel;
        }

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

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

        private void button_cal_Click(object sender, RoutedEventArgs e)
        {
            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;
        }

    }
    public class WindowABHelperViewModel : INotifyPropertyChanged
    {

        public double X1 { get; set; }
        public double Y1 { get; set; }

        public double X2 { get; set; }

        public double Y2 { get; set; }

        public double A { get; set; }

        public double B { get; set; }

        public event PropertyChangedEventHandler PropertyChanged;
    }
}
