using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Text;

namespace Project.Class
{

    public class CHistoryJOB : INotifyPropertyChanged
    {
        public event PropertyChangedEventHandler PropertyChanged;
        List<JobData> _items;
        public CHistoryJOB()
        {
            _items = new List<JobData>();
        }
        public void Clear()
        {
            lock (_items)
            {
                _items.Clear();
            }

            OnPropertyChanged("Clear");
        }
        public int Count
        {
            get
            {
                lock (_items)
                {
                    return _items.Count;
                }
            }
        }
        public void SetItems(List<JobData> value)
        {
            lock (_items)
            {
                this._items = value;
            }
            OnPropertyChanged("REFRESH");
        }

        public List<JobData> Items { get { return _items; } }
        public void Add(JobData data)
        {
            lock (_items)
            {
                data.No = this._items.Count + 1;
                _items.Add(data);
            }

            OnPropertyChanged("Add:" + data.guid);
        }

        public void Remove(JobData data)
        {
            lock (_items)
            {
                _items.Remove(data);
            }

            OnPropertyChanged("Remove:" + data.guid);
        }
        public void Remove(string guid)
        {
            lock (_items)
            {
                var data = Get(guid);
                _items.Remove(data);
            }
            OnPropertyChanged("Remove:" + guid);
        }
        public JobData Get(string guid)
        {
            lock (_items)
            {
                return _items.Where(t => t.guid == guid).FirstOrDefault();
            }
        }
        public void Set(JobData data)
        {
            var item = Get(data.guid);
            if (item == null) throw new Exception("No data guid:" + data.guid.ToString());
            else
            {
                //item.No = data.No;
                //item.JobStart = data.JobStart;
                //item.JobEnd = data.JobEnd;
                //item.VisionData = data.VisionData;
                //item.error = data.error;
                //item.Message = data.Message;
                OnPropertyChanged("Set:" + data.guid);
            }
        }
        public void RaiseSetEvent(string guid)
        {
            OnPropertyChanged("Set:" + guid);
        }

        protected void OnPropertyChanged([CallerMemberName] string name = null)
        {
            if (PropertyChanged != null)
                PropertyChanged.Invoke(this, new PropertyChangedEventArgs(name));
        }
    }
    //[Serializable]
    //public class JobData
    //{
    //    //고유식별자
    //    public string guid { get; private set; }

    //    //비젼처리값
    //    public Class.VisionData VisionData { get; set; }

    //    //언로더포트(L/R)
    //    public string PortPos { get; set; }

    //    //프린트위치(U/LO)
    //    public string PrintPos { get; set; }

    //    //작업시작시간
    //    public DateTime JobStart { get; set; }

    //    //작업종료시간
    //    public DateTime JobEnd { get; set; }

    //    /// <summary>
    //    /// 이전 출고되는 시점과의 시간차 값
    //    /// </summary>
    //    public double TackTime { get { return (JobEnd - JobStart).TotalSeconds; } }

    //    //작업순서
    //    public int No { get; set; }

    //    //오류상태
    //    public eJobResult error { get; set; }

    //    //메세지
    //    public string message { get; set; }


    //    public TimeSpan JobRun
    //    {
    //        get
    //        {
    //            if (JobEnd.Year == 1982) return new TimeSpan(0);
    //            else return this.JobEnd - this.JobStart;
    //        }
    //    }
    //    public JobData()
    //    {
    //        this.No = 0;
    //        PortPos = string.Empty;
    //        PrintPos = string.Empty;
    //        guid = Guid.NewGuid().ToString();
    //        VisionData = new VisionData();
    //        this.JobStart = new DateTime(1982, 11, 23); // DateTime.Parse("1982-11-23");
    //        this.JobEnd = new DateTime(1982, 11, 23); // DateTime.Parse("1982-11-23");
    //        error = eJobResult.None;
    //        message = string.Empty;
    //    }

    //}
}
