using Aitex.Core.RT.OperationCenter;

using MECF.Framework.UI.Client.ClientBase;
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Media;

namespace MECF.Framework.UI.Client.TrayThickness.HistoryData
{
    public class HistoryDataViewModel : UiViewModelBase, ISupportMultipleSystem
    {
        /// <summary>
        /// 返回ViewModel绑定的视图对象。
        /// </summary>
        public UserControl View { get; private set; }
        public DateTime StartDateTime
        {
            get => ((HistoryDataView)View).wfTimeFrom.Value;
            set
            {
                ((HistoryDataView)View).wfTimeFrom.Value = value;
                NotifyOfPropertyChange(nameof(StartDateTime));
            }
        }

        public DateTime EndDateTime
        {
            get => ((HistoryDataView)View).wfTimeTo.Value;
            set
            {
                ((HistoryDataView)View).wfTimeTo.Value = value;
                NotifyOfPropertyChange(nameof(EndDateTime));
            }
        }

        private bool isQueryTray = true;
        //勾选查询Tray
        public bool IsQueryTray
        {
            get => isQueryTray;
            set 
            {
                isQueryTray = value;
                Refresh();
            }
        }

        private bool isQueryPM;
        //勾选查询PM
        public bool IsQueryPM
        {
            get => isQueryPM;
            set
            {
                isQueryPM = value;
                Refresh();
            }
        }

        public bool TimeChecked { get; set; }

        public bool Unit { get; set; } = false;

        public bool IsNotUnit => !Unit;

        public bool Entirety { get; set; } = true;

        //数据库查询对象实例
        private HistoryCoatingSqlHelp HistoryCoatingSqlHelp = new HistoryCoatingSqlHelp();


        //表示单行数据集合,Tray专用
        public List<CoatingData> CoatingData { get; set; } = new List<CoatingData>();

        //整个Tray对象数据集合，内含4个环
        public List<TrayCoatingThickness> TrayCoatingList { get; set; } = new List<TrayCoatingThickness>();

        //PM对象数据集合，内含大小周期
        public List<PMCoatingThickness> PMCoatingList { get; set; } = new List<PMCoatingThickness>();

        protected override void OnViewLoaded(object _view)
        {
            base.OnViewLoaded(_view);
            View = (UserControl)_view;
            StartDateTime = DateTime.Now.Date;
            EndDateTime = DateTime.Now.Date.AddDays(1).AddTicks(-1);

        }
        public void QueryTime()
        {
            if (StartDateTime > EndDateTime)
            {
                MessageBox.Show("time range invalid, start time should be early than end time.", "Error",
                    MessageBoxButton.OK,
                    MessageBoxImage.Error);
                return;
            }
            if (IsQueryTray)
                TrayCoatingList = HistoryCoatingSqlHelp.QueryUpdateTime_Tray(GetTime(StartDateTime), GetTime(EndDateTime));
            if (IsQueryPM)
                PMCoatingList = HistoryCoatingSqlHelp.QueryUpdateTime_PM(GetTime(StartDateTime), GetTime(EndDateTime));
            Refresh();
        }
        public void QueryAll()
        {
            if (IsQueryTray)
                TrayCoatingList = HistoryCoatingSqlHelp.QueryUpdateAll_Tray();
            if (IsQueryPM)
                PMCoatingList = HistoryCoatingSqlHelp.QueryUpdateAll_PM();
            Refresh();
        }

        public void QueryName(string name)
        {
            if (name == null || name.Length == 0)
            {
                MessageBox.Show("输入名称为空");
                return;
            }
            if (TimeChecked)
            {
                if (StartDateTime > EndDateTime)
                {
                    MessageBox.Show("time range invalid, start time should be early than end time.", "Error",
                        MessageBoxButton.OK,
                        MessageBoxImage.Error);
                    return;
                }
            }
            if (IsQueryTray)
            {
                if (Unit)
                    CoatingData = HistoryCoatingSqlHelp.QueryUnitName_Tray(name, GetTime(StartDateTime), GetTime(EndDateTime));
                else if (Entirety)
                    TrayCoatingList = HistoryCoatingSqlHelp.QueryEntiretyTray(name, GetTime(StartDateTime), GetTime(EndDateTime));
                else
                    CoatingData = HistoryCoatingSqlHelp.QueryUnitName_Tray(name, GetTime(StartDateTime), GetTime(EndDateTime));
            }
            if (IsQueryPM)
                PMCoatingList = HistoryCoatingSqlHelp.QueryUnitName_PM(name, GetTime(StartDateTime), GetTime(EndDateTime));

            Refresh();
        }

        private string GetTime(DateTime dt)
        {
           return dt.ToString("yyyy-MM-dd HH:mm:ss");
        }
    }




    /// <summary>
    /// 检测厚度是否大于最大值
    /// </summary>
    class ColorConverter : IMultiValueConverter
    {
        public object Convert(object[] value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            try
            {
                if (value[0] == null || value[1] == null)
                {
                    return new SolidColorBrush(Colors.Black);
                }
                double _current;
                if (!double.TryParse(value[0].ToString(), out _current))
                    return new SolidColorBrush(Colors.Red);
                double _max;
                if (!double.TryParse(value[1].ToString(), out _max))
                    return new SolidColorBrush(Colors.Black);

                if (_current > _max)
                {
                    return new SolidColorBrush(Colors.Red);
                }
                else
                {
                    return new SolidColorBrush(Colors.Black);
                }
            }
            catch (Exception)
            {
                return new SolidColorBrush(Colors.Red);
            }

        }

        public object[] ConvertBack(object value, Type[] targetTypes, object parameter, CultureInfo culture)
        {
            return null;
        }
    }

    class BoolToVisibilityConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            if ((bool)value)
                return Visibility.Visible;
            return Visibility.Collapsed;  
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }
}
