using MECF.Framework.Common.Equipment;
using MECF.Framework.Common.OperationCenter;
using MECF.Framework.UI.Client.ClientBase;
using OpenSEMI.ClientBase;
using OpenSEMI.Ctrlib.Controls;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;

namespace MECF.Framework.UI.Client.TrayThickness
{
    public class TrayDataViewManager
    {
        TrayCoatingThickness TrayData;
        TrayDataViewSqlHelp trayDataHelp = new TrayDataViewSqlHelp();
        private string TrayID;
        public TrayDataViewManager(TrayCoatingThickness trayData, string trayID)
        {
            TrayData = trayData;
            TrayID = trayID; 
        }
        public void UpTrayCoatingThickness()
        {
            TrayCoatingThickness _trayCoatingThickness = trayDataHelp.GetTrayCoatingThickness(TrayID);
            TrayData.Tray.Update(_trayCoatingThickness.Tray);
            TrayData.TrayInner.Update(_trayCoatingThickness.TrayInner);
            TrayData.RingInner.Update(_trayCoatingThickness.RingInner);
            TrayData.RingOuter.Update(_trayCoatingThickness.RingOuter);
            
        }
        /// <summary>
        /// 界面数据设置到数据库
        /// </summary>
        public bool SET(out string reason)
        {
            reason = "";

            // 校验托盘组件数据
            if (!TrayData.Validate(out reason))
                return false;

            List<CoatingData> coatingDataList = new List<CoatingData>()
            {
                TrayData.Tray,
                TrayData.TrayInner,
                TrayData.RingInner,
                TrayData.RingOuter
            };

            //检查单个环对象是否存在空数据
            foreach (var item in coatingDataList)
            {

                if (item.GetType().GetProperties()
                          .Where(pi => pi.PropertyType == typeof(string))
                          .Select(pi => (string)pi.GetValue(item))
                          .Any(string.IsNullOrEmpty))
                {
                    reason = $"Please check empty value";
                    return false;
                }
            }
            var ret = trayDataHelp.UpTrayNumberData(TrayData, TrayID, coatingDataList);
            if (!ret)
                reason = "Failed to write tray Info. to database.";

            if(ret)
            {
                foreach (CoatingData coatingData in coatingDataList)
                {
                    string name = coatingData.SerialNumber;
                    string coating = coatingData.Current;
                    string coatingMax = coatingData.MAX;

                    InvokeClient.Instance.Service.DoOperation("GEM_UpdateCoating",
                        name,coating, coatingMax);
                }
            }

            return ret;
        }

        /// <summary>
        /// 用户输入数据时，去数据库查询更新到界面
        /// </summary>
        /// <param name="part"></param>
        public void LoadCoatingData(string part)
        {

            switch (part)
            {
                case "Tray":
                    ForUpCoatingData(TrayData.Tray.SerialNumber, TrayData.Tray);
                    break;
                case "TrayInner":
                    ForUpCoatingData(TrayData.TrayInner.SerialNumber, TrayData.TrayInner);
                    break;
                case "RingInner":
                    ForUpCoatingData(TrayData.RingInner.SerialNumber, TrayData.RingInner);
                    break;
                case "RingOuter":
                    ForUpCoatingData(TrayData.RingOuter.SerialNumber, TrayData.RingOuter);
                    break;
                default:
                    break;
            }
            
        }
        
        private void ForUpCoatingData(string number, CoatingData data)
        {
            var coatingData = trayDataHelp.GetCoatingData(number, out var isExistedInDb);
            if (isExistedInDb)
            {
                data.Update(coatingData);
            }
        }
    }
}
