using MahApps.Metro.Controls.Dialogs;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Linq;
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;
using WSDDBBrowser.Core;
using WSDDBBrowser.Core.Model;

namespace WSDDBBrowser
{
    /// <summary>
    /// PgChartConfig.xaml 的交互逻辑
    /// </summary>
    public partial class PgChartConfig : Page
    {
        PgChartConfigViewModel viewModel;
        public PgChartConfig()
        {
            InitializeComponent();
        }

        public void Init()
        {
            viewModel = new PgChartConfigViewModel();
            viewModel.Init();
            this.DataContext = viewModel;
            sp_statistics.DataContext = DBViewerModel.Instance;
        }

        private void ButtonApplyClick(object sender, RoutedEventArgs e)
        {
            viewModel.Apply();
        }

        private void ButtonCteateCuttingClick(object sender, RoutedEventArgs e)
        {
            viewModel.CreateCuttingMap();
        }
    }

    public class PgChartConfigViewModel : INotifyPropertyChanged
    {
        public g_tgProcess Process { get; private set; }

        /// <summary>
        /// 混合数
        /// </summary>
        public int Mix { get; set; }
        public double Step { get; set; }

        public double YRangePercent { get; set; }

        public int FilmWidth { get; private set; }

        /// <summary>
        /// 分区表
        /// </summary>
        public ObservableCollection<Bolt> CuttingMap { get; } = new ObservableCollection<Bolt>();

        //分条宽度 mm
        public int CuttingWidth { get; set; }

        /// <summary>
        /// 分条双数
        /// </summary>
        public bool IsCuttingCntEven { get; set; }


        public List<TechnologyItem> TechnologyItems { get; private set; }
        public List<g_tgNumber> Numbers { get; private set; }
        public List<ScanDataItem> ScanDataItems { get; private set; }
        public PgChartConfigViewModel()
        {

        }
        public void Init()
        {
            Process = DBViewerModel.Instance.tgProcess;
            Numbers = DBViewerModel.Instance.TGProcessPacks.Last().Numbers;


            Mix = DBViewerModel.Instance.Mix;
            Step = DBViewerModel.Instance.Step;


            ScanDataItems = DBViewerModel.Instance.ScanDataItems;
            TechnologyItems = new List<TechnologyItem>();
            foreach (var scanDataItem in ScanDataItems)
            {
                var tech = new TechnologyItem();
                tech.DeviceID = scanDataItem.DeviceID;
                tech.DeviceName = scanDataItem.DeviceName;
                tech.Target = scanDataItem.Target;
                tech.Tolerance = scanDataItem.Tolerance;
                tech.Ctrlline = scanDataItem.Ctrlline;
                TechnologyItems.Add(tech);
            }

            YRangePercent = DBViewerModel.Instance.YRangePercent;

            FilmWidth = DBViewerModel.Instance.FilmWidth;
            CuttingMap.Clear();
            for (int i = 0; i < DBViewerModel.Instance.CuttingMap.Count(); i++)
                CuttingMap.Add(DBViewerModel.Instance.CuttingMap[i].Clone() as Bolt);
            IsCuttingCntEven = DBViewerModel.Instance.IsCuttingCntEven;
            CuttingWidth = DBViewerModel.Instance.CuttingWidth;
        }
        public async void CreateCuttingMap()
        {
            if (CuttingWidth < 20)
            {
                await App.MetroWindow.ShowMessageAsync("参数出错",
                        $"分切宽度 < 20mm");
                return;
            }
            if (FilmWidth < 50)
            {
                await App.MetroWindow.ShowMessageAsync("参数出错",
                        $"基材宽度 < 50mm");
                return;
            }
            if (FilmWidth < CuttingWidth)
            {
                await App.MetroWindow.ShowMessageAsync("参数出错",
                        $"基材宽度 < 分切宽度 ");
                return;
            }
            CuttingMap.Clear();

            var cuttingmap = DBViewerModel.Instance.CreateCuttingMap(CuttingWidth, IsCuttingCntEven);
            if (cuttingmap == null)
            {
                await App.MetroWindow.ShowMessageAsync("参数出错",
                    $"未知错误");
                return;
            }
            foreach (var cutting in cuttingmap)
                CuttingMap.Add(cutting);

            await App.MetroWindow.ShowMessageAsync("通知",
                    $"分切表创建成功！");
        }
        public async void Apply()
        {
            foreach (var tech in TechnologyItems)
            {
                var scanDataItem = DBViewerModel.Instance.ScanDataItems.Find(sdi => sdi.DeviceID == tech.DeviceID);
                if (scanDataItem == null)
                {
                    await App.MetroWindow.ShowMessageAsync("异常",
                        $"不能找到 设备ID 为 {tech.DeviceID} 的数据");
                    return;
                }

                scanDataItem.Target = tech.Target;
                scanDataItem.Tolerance = tech.Tolerance;
                scanDataItem.Ctrlline = tech.Ctrlline;
            }

            DBViewerModel.Instance.YRangePercent = YRangePercent;
            DBViewerModel.Instance.Mix = Mix;
            DBViewerModel.Instance.Step = Step;

            var bolts = CuttingMap.OrderBy((b) => b.Begin);
            DBViewerModel.Instance.SetCuttingMap(bolts);
            DBViewerModel.Instance.CuttingWidth = CuttingWidth;
            DBViewerModel.Instance.IsCuttingCntEven = IsCuttingCntEven;

            DBViewerModel.Instance.Save();


            //开始加载
            var controller = await App.MetroWindow.ShowProgressAsync("请等待...", "重新开始计算",
                settings: new MetroDialogSettings()
                {
                    NegativeButtonText = "停止",
                    AnimateShow = false,
                    AnimateHide = false,

                });
            //controller.SetCancelable(true);
            controller.SetIndeterminate();

            DBViewerModel.Instance.Update4MixChanged();

            await controller.CloseAsync();

            //计算成功
            await App.MetroWindow.ShowMessageAsync("计算成功",
                $"分切数={DBViewerModel.Instance.CuttingMap.Count()}");
        }


        public event PropertyChangedEventHandler PropertyChanged;
    }
    public class PgChartConfigViewModel_UnitTest : INotifyPropertyChanged
    {
        public g_tgProcess Process { get; set; }

        public double Step { get; set; } = 1;
        /// <summary>
        /// 混合数
        /// </summary>
        public int Mix { get; set; } = 6;

        public int FilmWidth { get; set; } = 1550;

        public double YRangePercent { get; set; } = 1.5;

        public List<TechnologyItem> TechnologyItems { get; } = new List<TechnologyItem>();
        public List<ScanDataItem> ScanDataItems { get; } = new List<ScanDataItem>();

        public List<g_tgNumber> Numbers { get; } = new List<g_tgNumber>();

        /// <summary>
        /// 分区表
        /// </summary>
        public List<Bolt> CuttingMap { get; } = new List<Bolt>();

        //分条宽度 mm
        public int CuttingWidth { get; set; }

        /// <summary>
        /// 分条双数
        /// </summary>
        public bool IsCuttingCntEven { get; set; }

        public PgChartConfigViewModel_UnitTest()
        {
            Process = new g_tgProcess()
            {
                cOrderNo = "71412312311",
                cProjectNo = "000-00-00-01",
                cTechnologyNo = "000-00-00-00"
            };


            CuttingMap.Add(new Bolt(40, 90));
            CuttingMap.Add(new Bolt(90, 140));
            CuttingMap.Add(new Bolt(140, 190));
            CuttingMap.Add(new Bolt(190, 240));
            CuttingMap.Add(new Bolt(240, 290));

            TechnologyItems.Add(new TechnologyItem() { DeviceID = "0", DeviceName = "2涂", Target = 100, Ctrlline = 2, Tolerance = 4 });
            TechnologyItems.Add(new TechnologyItem() { DeviceID = "1", DeviceName = "底料", Target = 60, Ctrlline = 2, Tolerance = 4 });
            TechnologyItems.Add(new TechnologyItem() { DeviceID = "10", DeviceName = "3涂", Target = 150, Ctrlline = 2, Tolerance = 5 });
            TechnologyItems.Add(new TechnologyItem() { DeviceID = "11", DeviceName = "胶水", Target = 50, Ctrlline = 2, Tolerance = 5 });


            ScanDataItems.Add(new ScanDataItem() { DeviceID = "0", DeviceName = "2涂" });
            ScanDataItems.Add(new ScanDataItem() { DeviceID = "1", DeviceName = "底料" });
            ScanDataItems.Add(new ScanDataItem() { DeviceID = "10", DeviceName = "3涂" });
            ScanDataItems.Add(new ScanDataItem() { DeviceID = "11", DeviceName = "胶水" });

            Numbers.Add(new g_tgNumber() { cNumber = "01" });
            Numbers.Add(new g_tgNumber() { cNumber = "02" });
            Numbers.Add(new g_tgNumber() { cNumber = "03" });
        }
        public event PropertyChangedEventHandler PropertyChanged;
    }
    public class TechnologyItem
    {
        public string DeviceID { get; set; }

        public string DeviceName { get; set; }

        public double Target { get; set; } = 150;

        public double Tolerance { get; set; } = 2.3;

        public double Ctrlline { get; set; } = 1.2;
    }
}
