using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Project.Dialog
{
    public partial class fDataBufferSIDRef : Form
    {
        Boolean useMonitor = false;
        public fDataBufferSIDRef()
        {
            InitializeComponent();
            this.lvSID.Items.Clear();
            var list = PUB.Result.SIDReference.Items;
            foreach (var item in list)
            {
                var lv = this.lvSID.Items.Add(item.sid, string.Format("[{0}] {1}", lvSID.Items.Count + 1, item.sid), 0);    //sid
                lv.SubItems.Add(item.kpc.ToString()); //count
                lv.SubItems.Add(item.unit);
            }
            PUB.Result.SIDReference.PropertyChanged += sidreflist_PropertyChanged;
            useMonitor = true;
        }

        public fDataBufferSIDRef(List<Class.SIDDataRef> items)
        {
            InitializeComponent();
            this.lvSID.Items.Clear();
            foreach (var item in items)
            {
                var lv = this.lvSID.Items.Add(item.sid, string.Format("[{0}] {1}", lvSID.Items.Count + 1, item.sid), 0);    //sid
                lv.SubItems.Add(item.kpc.ToString()); //count
                lv.SubItems.Add(item.unit);
            }
        }
        void fDataBufferSID_FormClosed(object sender, FormClosedEventArgs e)
        {
            if (useMonitor)
                PUB.Result.SIDReference.PropertyChanged -= sidreflist_PropertyChanged;
        }

        private void fDataBuffer_Load(object sender, EventArgs e)
        {
            this.FormClosed += fDataBufferSID_FormClosed;

        }

        void sidreflist_PropertyChanged(object sender, PropertyChangedEventArgs e)
        {
            if (this.InvokeRequired)
            {
                var p = new PropertyChangedEventArgs(e.PropertyName);
                this.BeginInvoke(new PropertyChangedEventHandler(sidreflist_PropertyChanged), new object[] { sender, p });
                return;
            }
            var pname = e.PropertyName.Split(':');
            if (pname[0].ToLower() == "add")
            {
                var sid = pname[1];
                var item = PUB.Result.SIDReference.Get(pname[1]);
                var lv = this.lvSID.Items.Add(sid, string.Format("[{0}] {1}", lvSID.Items.Count + 1, sid), 0);
                lv.SubItems.Add(item.kpc.ToString());
                lv.SubItems.Add(item.unit);//
                lv.EnsureVisible();
            }
            else if (pname[0].ToLower() == "clear")
            {
                this.lvSID.Items.Clear();
            }
            else if (pname[0].ToLower() == "set")
            {
                var sid = pname[1];
                var cnt = PUB.Result.SIDReference.Get(sid);
                var items = this.lvSID.Items.Find(sid, false);
                if (items != null && items.Length > 0)
                {
                    foreach (var item in items)
                    {
                        item.SubItems[1].Text = cnt.ToString();
                    }
                }
            }
        }

    }
}
