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>
    /// PageSelect.xaml 的交互逻辑
    /// </summary>
    public partial class PgSelect : Page
    {
        PgSelectViewModel viewModel;
        public PgSelect()
        {
            InitializeComponent();
        }

        public void Init()
        {
            viewModel = new PgSelectViewModel();
            viewModel.Init();
            this.DataContext = viewModel;
        }

        private async void ButtonOK_Click(object sender, RoutedEventArgs e)
        {
            if (dgridProcesses.SelectedItems.Count == 0)
            {
                await App.MetroWindow.ShowMessageAsync("加载异常",
                    $"没有选择任何一个记录,也许忘记按 【搜索】 了！！");
                return;
            }


            //开始加载
            var controller = await App.MetroWindow.ShowProgressAsync("请等待...", "开始加载数据",
                settings: new MetroDialogSettings()
                {
                    NegativeButtonText = "停止",
                    AnimateShow = false,
                    AnimateHide = false,
                });
            //controller.SetCancelable(true);
            controller.SetIndeterminate();


            DBViewerModel.Instance.CurrProcess = null;

            DBViewerModel.Instance.CurrProcess = dgridProcesses.SelectedItem as Process;
            bool ret = DBViewerModel.Instance.GetProcessPacks();
            if (!ret)
            {
                //获取出错
                await controller.CloseAsync();
                await App.MetroWindow.ShowMessageAsync("加载出错", "数据异常，请检测数据库");
                return;
            }

            await controller.CloseAsync();
            string msg = "";
            if (DBViewerModel.Instance.CurrProcess.HasTGData)
            {
                int cnt = DBViewerModel.Instance.ScanDataItems.Sum(item => item.MixValues.Count());

                msg += $"厚度数据 {cnt}幅";
            }

            if (DBViewerModel.Instance.CurrProcess.HasSBData)
            {
                int cnt = DBViewerModel.Instance.SBProcessPack.Records.Count();
                msg += $"温控数据 {cnt}个点";
            }

            //加载成功
            await App.MetroWindow.ShowMessageAsync("加载成功", msg);


            this.NavigationService.GoBack();
            this.NavigationService.RemoveBackEntry();
        }

        private void ButtonBack_Click(object sender, RoutedEventArgs e)
        {
            this.NavigationService.GoBack();
            this.NavigationService.RemoveBackEntry();
        }
    }

    public class PgSelectViewModel : INotifyPropertyChanged
    {
        public event PropertyChangedEventHandler PropertyChanged;

        /// <summary>
        /// 数据库加载结果
        /// </summary>
        public string DBInitResult { get; set; }

        /// <summary>
        /// 机台号
        /// </summary>
        public int MachineNo { get; set; }

        /// <summary>
        /// 工序号
        /// </summary>
        public int StepNo { get; set; }

        /// <summary>
        /// 开始日期
        /// </summary>
        public DateTime BeginDate { get; set; }
        /// <summary>
        /// 结束日期
        /// </summary>
        public DateTime EndDate { get; set; }
        /// <summary>
        /// 订单号过滤
        /// </summary>
        public string OrderNoFilter { get; set; }

        /// <summary>
        /// 生产列表
        /// </summary>
        public ObservableCollection<Process> Processes { get; } = new ObservableCollection<Process>();

        public PgSelectViewModel()
        {
        }
        public void Init()
        {

            BeginDate = DateTime.Now.AddDays(-1);
            EndDate = DateTime.Now;

            StepNo = DBViewerModel.Instance.StepNo;
            MachineNo = DBViewerModel.Instance.MachineNo;
            DBInit(); 
        }

        void DBInit()
        {

            if (!DBHelper.Instance.Init(
                DBViewerModel.Instance.ServerIP,
                DBViewerModel.Instance.ServerUser,
                DBViewerModel.Instance.ServerPw))
            {
                DBInitResult = DBHelper.Instance.ErrorMsg;
            }
            else
            {
                DBInitResult = "数据库加载成功";
            }

        }



        private ICommand buttonSearch;

        public ICommand ButtonSearch
        {
            get
            {
                return this.buttonSearch ?? (this.buttonSearch = new ClickCommand( async ()=> {
                    var list = DBHelper.Instance.GetProcessList(StepNo, MachineNo, BeginDate, EndDate, OrderNoFilter);


                    Processes.Clear();
                    foreach (var p in list)
                    {
                        Processes.Add(p);
                    }

                    if (list.Count() > 0)
                    {
                        if (StepNo != DBViewerModel.Instance.StepNo || MachineNo != DBViewerModel.Instance.MachineNo)
                        {
                            DBViewerModel.Instance.StepNo = StepNo;
                            DBViewerModel.Instance.MachineNo = MachineNo;
                            DBViewerModel.Instance.Save();
                        }
                    }
                    if (Processes.Count > 0)
                    {
                        //加载成功
                        await App.MetroWindow.ShowMessageAsync("加载成功", $"{Processes.Count()} 条记录被加载");
                    }
                    else
                    {
                        await App.MetroWindow.ShowMessageAsync("加载失败", $"不能找到任何记录");
                    }
                }));
            }
        }

    }
    public class PgSelectViewModel_UnitTest : INotifyPropertyChanged
    {
        public event PropertyChangedEventHandler PropertyChanged;

        /// <summary>
        /// 机台号
        /// </summary>
        public int MachineNo { get; set; }

        /// <summary>
        /// 工序号
        /// </summary>
        public int StepNo { get; set; }

        /// <summary>
        /// 开始日期
        /// </summary>
        public DateTime BeginDate { get; set; }

        /// <summary>
        /// 结束日期
        /// </summary>
        public DateTime EndDate { get; set; }

        /// <summary>
        /// 订单号过滤
        /// </summary>
        public string OrderNoFilter { get; set; }

        public ObservableCollection<g_tgProcess> Processes { get; } = new ObservableCollection<g_tgProcess>();

        public PgSelectViewModel_UnitTest()
        {
            BeginDate = DateTime.Now.AddDays(-1);
            EndDate = DateTime.Now;

        }
    }
}
