using System;
using System.Collections.Generic;
using System.Linq;

namespace MECF.Framework.UI.Client.TrayThickness
{
    public class TrayCoatingThickness : ICloneable
    {
        private readonly List<CoatingData> _dataList;

        #region Constructors

        public TrayCoatingThickness()
        {
            Tray = new CoatingData();
            TrayInner = new CoatingData();
            RingInner = new CoatingData();
            RingOuter = new CoatingData();

            _dataList = new List<CoatingData>(
                new []
                {
                    Tray, TrayInner, RingInner, RingOuter 
                });
        }

        public TrayCoatingThickness(string trayGuId)
        {
            TrayGuid = trayGuId; //Tary的默认数据库ID为自身编号1~8
            Tray = new CoatingData($"石墨托盘", trayGuId);
            TrayInner = new CoatingData($"内部托架", trayGuId);
            RingInner = new CoatingData($"中间托架", trayGuId);
            RingOuter = new CoatingData($"外部托架", trayGuId);

            _dataList = new List<CoatingData>(
                new[]
                {
                    Tray, TrayInner, RingInner, RingOuter
                });

        }

        //public TrayCoatingThickness(int slot) : this(slot.ToString())
        //{

        //}

        #endregion

        #region Properties

        public string TrayGuid { get; set; }

        /// <summary>
        /// Tray厚度对象
        /// </summary>
        public CoatingData Tray { get; set; }

        /// <summary>
        /// Tray内环厚度对象
        /// </summary>
        public CoatingData TrayInner { get; set; }
        /// <summary>
        /// Ring内环厚度对象
        /// </summary>
        public CoatingData RingInner { get; set; }
        /// <summary>
        /// Ring外环厚度对象
        /// </summary>
        public CoatingData RingOuter { get; set; }


        #endregion

        #region Methods

        public bool Validate(out string reason)
        {
            if (!Tray.Validate(out reason))
                return false;

            if(!TrayInner.Validate(out reason)) 
                return false;

            if(!RingInner.Validate(out reason))
                return false;

            if (!RingOuter.Validate(out reason))
                return false;

            //每个环Number数据是否存在相同，Linq语句检查
            if (_dataList.GroupBy(i => i.SerialNumber).Any(g => g.Count() > 1))
            {
                reason = $"some of components have same SN, please check.";
                return false;
            }

            return true;
        }


        public void Update(TrayCoatingThickness newData)
        {
            Tray.Update(newData.Tray);
            TrayInner.Update(newData.TrayInner);
            RingInner.Update(newData.RingInner);
            RingOuter.Update(newData.RingOuter);
        }

        public object Clone()
        {
            var newObj = new TrayCoatingThickness(TrayGuid);
            newObj.Tray = (CoatingData)Tray.Clone();
            newObj.TrayInner = (CoatingData)TrayInner.Clone();
            newObj.RingInner = (CoatingData)RingInner.Clone();
            newObj.RingOuter = (CoatingData)RingOuter.Clone();
            return newObj;
        }


        #endregion

    }
}
