using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel;
using System.Collections.ObjectModel;

namespace FLY.Thick.Base.Common
{
    public class CoatingSegmentPart : INotifyPropertyChanged
    {
        private double total;
        //涂长+留白单位mm
        public double Total
        {
            get { return total; }
            set
            {
                if (total != value)
                {
                    total = value;
                    NotifyPropertyChanged("Total");
                }
            }
        }
        private double distance;
        public double Distance//留白单位mm
        {
            get { return distance; }
            set
            {
                if (distance != value)
                {
                    distance = value;
                    NotifyPropertyChanged("Distance");
                }
            }
        }
        public CoatingSegmentPart Clone() 
        {
            CoatingSegmentPart p = new CoatingSegmentPart();
            p.Total = Total;
            p.Distance = Distance;
            return p;
        }
        public byte[] ToByte()
        {
            List<byte> buf = new List<byte>();
            buf.AddRange(BitConverter.GetBytes(Total));
            buf.AddRange(BitConverter.GetBytes(Distance));

            return buf.ToArray();
        }
        /// <summary>
        /// 16个字节转换
        /// </summary>
        /// <param name="value"></param>
        /// <param name="offset"></param>
        /// <returns></returns>
        public bool TryParse(byte[] value, int offset)
        {
            if (value.Length - offset < 16)
                return false;

            Total = BitConverter.ToDouble(value, offset + 0);
            Distance = BitConverter.ToDouble(value, offset + 8);
            return true;
        }


        protected void NotifyPropertyChanged(string propertyName)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged.Invoke(this, new PropertyChangedEventArgs(propertyName));
            }
        }


        public event PropertyChangedEventHandler PropertyChanged;
    }
    public class CoatingSegmentProfile : INotifyPropertyChanged
    {

        string productname;
        /// <summary>
        /// 产品名称
        /// </summary>
        public string ProductName
        {
            get { return productname; }
            set
            {
                if (productname != value)
                {
                    productname = value;
                    NotifyPropertyChanged("ProductName");
                }
            }
        }


        ObservableCollection<CoatingSegmentPart> coatingpartlist = new ObservableCollection<CoatingSegmentPart>();
        /// <summary>
        /// 涂布段长 &amp; 留白
        /// </summary>
        public ObservableCollection<CoatingSegmentPart> CoatingPartList
        {
            get { return coatingpartlist; }
        }



        public byte[] ToBytes()
        {
            List<byte> buf = new List<byte>();
            byte[] name_bytes = Misc.Converter.StringToBytes(ProductName);
            if (name_bytes == null)
            {
                buf.AddRange(BitConverter.GetBytes(0));
            }
            else{
                buf.AddRange(BitConverter.GetBytes((int)name_bytes.Length));
                buf.AddRange(name_bytes);
            }
            
            buf.AddRange(BitConverter.GetBytes(CoatingPartList.Count()));
            for (int i = 0; i < CoatingPartList.Count(); i++)
                buf.AddRange(CoatingPartList[i].ToByte());
            
            return buf.ToArray();
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="value"></param>
        /// <param name="offset"></param>
        /// <param name="cnt"></param>
        /// <returns></returns>
        public bool TryParse(byte[] value, int offset, out int cnt)
        {
            cnt = (4 + 4);
            if (value.Length - offset < cnt)
                return false;
            int idx = offset;
            int len = BitConverter.ToInt32(value, idx);
            idx += 4;
            cnt += len;
            if (value.Length < cnt)
                return false;

            byte[] bytes = new byte[len];
            Array.Copy(value, idx, bytes, 0, len);
            idx += len;
            string name = Misc.Converter.BytesToString(bytes);

            int len2 = BitConverter.ToInt32(value, idx);
            idx += 4;
            cnt += len2 * 16;
            if (value.Length < cnt)
                return false;

            CoatingPartList.Clear();

            ProductName = name;
            for (int i = 0; i < len2; i++)
            {
                CoatingSegmentPart coatingPart = new CoatingSegmentPart();
                coatingPart.TryParse(value, idx);
                idx += 16;
                CoatingPartList.Add(coatingPart);
            }
            return true;
        }




        protected void NotifyPropertyChanged(string propertyName)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged.Invoke(this, new PropertyChangedEventArgs(propertyName));
            }
        }

        public event PropertyChangedEventHandler PropertyChanged;
    }
}
