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

namespace Project.Class
{
    public class CHistorySIDRef : INotifyPropertyChanged
    {
        public event PropertyChangedEventHandler PropertyChanged;
        public Boolean JobSIDRecvError
        {
            get
            {
                //아이템이있어야 정상이다
                if (_items == null || _items.Count < 1) return true;
                else if (JobSIDRecvTime.Year == 1982) return true;
                else return false;
            }
        }
        public DateTime JobSIDRecvTime { get; set; }
        public string JobSIDRecvMessage { get; set; }
        List<SIDDataRef> _items;

        public CHistorySIDRef()
        {
            Clear();
        }
        public void SetItems(List<SIDDataRef> value)
        {
            this._items = value;
            OnPropertyChanged("REFRESH");
        }

        public List<SIDDataRef> Items { get { return _items; } }
        public int Count
        {
            get
            {
                return _items.Count;
            }
        }
        public SIDDataRef Get(string sid_)
        {
            return _items.Where(t => t.sid == sid_).FirstOrDefault();
        }
        public void Set(SIDDataRef data)
        {
            var item = Get(data.sid);
            if (item == null) throw new Exception("No data sid:" + data.sid.ToString());
            else
            {
                item.kpc = data.kpc;
                item.unit = data.unit;
                OnPropertyChanged("Set:" + data.sid);
            }
        }

        public void Set(string sid, int kpc, string unit)
        {
            var item = Get(sid);
            if (item == null)
            {
                Add(sid, kpc, unit);  //없다면 추가해준다
            }
            else
            {
                item.kpc = kpc;
                item.unit = unit;
                OnPropertyChanged("Set:" + sid);
            }
        }

        public void Add(SIDDataRef data)
        {
            _items.Add(data);
            OnPropertyChanged("Add:" + data.sid);
        }


        public void Add(string sid, int kpc_, string unit)
        {
            if (string.IsNullOrEmpty(sid))
            {
                PUB.log.AddAT("SID addition failed - SID value not entered");
                return;
            }
            //if (JobSidList.ContainsKey(sid) == false)
            _items.Add(new SIDDataRef(sid, kpc_, unit));
            OnPropertyChanged("Add:" + sid);
            //else
            //{
            //이미 데이터가 있다. 중복이므로 누적한다
            //JobSidList.TryGetValue()
            //}
        }
        public void Clear()
        {
            //JobSIDRecvError = false;
            JobSIDRecvMessage = string.Empty;
            JobSIDRecvTime = DateTime.Parse("1982-11-23");
            if (this._items == null) this._items = new List<SIDDataRef>();
            else this._items.Clear();
            OnPropertyChanged("Clear");
        }

        protected void OnPropertyChanged([CallerMemberName] string name = null)
        {
            if (PropertyChanged != null)
                PropertyChanged.Invoke(this, new PropertyChangedEventArgs(name));
        }
    }

    [Serializable]
    public class SIDDataRef
    {
        public string guid { get; set; }
        public string sid { get; set; }
        public string unit { get; set; }
        public int kpc { get; set; }

        public SIDDataRef(string sid_, int kpc_, string unit_)
        {

            guid = Guid.NewGuid().ToString();
            sid = sid_;
            unit = unit_;
            kpc = kpc_;
        }
        public SIDDataRef() : this(string.Empty, 0, string.Empty) { }
    }
}
