using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using FLY.Thick.Base.IService;
using System.ComponentModel;
using FLY.Thick.Base.Common;
using Misc;

namespace FLY.Thick.Base.Server
{
    /// <summary>
    /// 给涂布用的，简易分区表。 只能以1cm一个分区设定
    /// </summary>
    public class BoltMapEasy:IBoltMapEasy,Misc.ISaveToXml,IBoltMapBase
    {
        #region IBoltMapEasy 属性

        private int boltwidth=100;
        /// <summary>
        /// 平均分区宽度
        /// </summary>
        public int BoltWidth
        {
            get
            {
                return boltwidth;
            }
            set
            {
                if (boltwidth != value)
                {
                    boltwidth = value;
                    NotifyPropertyChanged("BoltWidth");
                }
            }
        }
        
        private int boltcnt=90;
        /// <summary>
        /// 分区总数
        /// </summary>
        public int BoltCnt
        {
            get
            {
                return boltcnt;
            }
            set
            {
                if (boltcnt != value)
                {
                    boltcnt = value;
                    NotifyPropertyChanged("BoltCnt");
                }
            }
        }

        private int smooth=0;
        public int Smooth
        {
            get { return smooth; }
            set
            {
                if (smooth != value)
                {
                    smooth = value;
                    NotifyPropertyChanged("Smooth");
                }
            }
        }

        private int borderboltno_b=0;
        /// <summary>
        /// 开始边界分区号 
        /// </summary>
        public int BorderBoltNo_B
        {
            get { return borderboltno_b; }
            set
            {
                if (borderboltno_b != value)
                {
                    borderboltno_b = value;
                    NotifyPropertyChanged("BorderBoltNo_B");
                }
            }
        }

        private BORDER_TYPE bordertype;
        /// <summary>
        /// 找边界,边界类型
        /// </summary>
        public BORDER_TYPE BorderType
        {
            get { return bordertype; }
            set
            {
                if (bordertype != value)
                {
                    bordertype = value;
                    NotifyPropertyChanged("BorderType");
                }
            }
        }
        #endregion

        private Range[] bolts;
        private DynBoltAD dyn;
        private BoltMapHelper helper;
        public event ActiveEventHandler ActiveEvent;


        private string param_path = "boltmap_easy.xml";
        public BoltMapEasy() 
        {
            dyn = new DynBoltAD(this);
            helper = new BoltMapHelper(this);
            create_bolts();

        }
        public BoltMapEasy(string param_path) 
        {
            if (!string.IsNullOrEmpty(param_path))
                this.param_path = param_path;
            dyn = new DynBoltAD(this);
            helper = new BoltMapHelper(this);
            
            Load();
            create_bolts();
        }
        void create_bolts() 
        {
            bolts = new Range[BoltCnt];

            for (int i = 0; i < BoltCnt; i++) 
            {
                bolts[i] = new Range() { Begin = i * BoltWidth, End = (i+1) * BoltWidth};
            }
        }
        public void Apply() 
        {
            create_bolts();
            Save();

            if (ActiveEvent != null)
                ActiveEvent(this);
        }

        #region IBoltMapBase
        public int FirstBoltNo { get { return 0; } }
        public int NBolts { get { return BoltCnt; } }

        /// <summary>
        /// 获取当前使用分区表
        /// </summary>
        /// <param name="boltno1st"></param>
        /// <param name="bolts"></param>
        public void GetBolts(out int boltno1st, out IEnumerable<Range> bolts) 
        {
            boltno1st = FirstBoltNo;
            bolts = this.bolts;
        }
        public void GetScanRange(Range boltno, out int posBegin, out int posEnd) 
        {
            int index = boltno.Begin;
            if (index < 0)
                index = 0;
            else if (index >= BoltCnt)
                index = BoltCnt - 1;
            posBegin = index * BoltWidth;

            index = boltno.End;
            if (index < 0)
                index = 0;
            else if (index >= BoltCnt)
                index = BoltCnt - 1;
            posEnd = index * BoltWidth + BoltWidth;

        }
        public int GetBoltIndex_fromPos(int pos) 
        {
            return pos / BoltWidth;
        }
        #region 用于动态生成分区数据
        public void InitBoltAD()
        {
            dyn.Init();
        }
        public bool UpdateBoltAD(int[] data, int start_pos, int posOfGrid, Misc.DIRECTION direction, out int start_boltIndex, out int[] boltAD)
        {
            return dyn.UpdateBoltAD(data, start_pos, posOfGrid, direction, out start_boltIndex, out boltAD);
        }
        #endregion


        #region 根据边界调整分区
        public void InitExChange()
        {
            this.helper.InitExChange();
        }
        public void CalExChange(Misc.DIRECTION direction, Range bolt_r)
        {
            int borderPosBegin = bolt_r.Begin;
            int idx = 0;
            if (direction == Misc.DIRECTION.BACKWARD)
                idx = 1;
            FLY.Thick.Base.Server.BoltMapHelper.ExChange exchange = helper.mExChange[idx];
            exchange.Init();

            int org_borderPosBegin;

            int borderboltindex_b = BorderBoltNo_B;

            if (borderboltindex_b < 0)
                borderboltindex_b = 0;
            else if (borderboltindex_b >= BoltCnt)
                borderboltindex_b = 0;

            switch (BorderType)
            {
                case BORDER_TYPE.Begin:
                    {
                        org_borderPosBegin = BoltWidth * borderboltindex_b;

                        exchange.Offset = (int)(borderPosBegin - org_borderPosBegin);
                    } break;
            }
        }
        public int PositionExchange(Misc.DIRECTION direction, int position)
        {
            return helper.PositionExchange(direction, position);
        }
        public int BoltExchange(Misc.DIRECTION direction, int bolt_pos)
        {
            return helper.BoltExchange(direction, bolt_pos);
        }
        #endregion

        public int[] Map(Misc.DIRECTION direction, int posBegin, int posOfGrid, int[] dat)
        {
            return helper.Map(direction, posBegin, posOfGrid, dat);
        }

        #endregion

        #region INotifyPropertyChanged 成员
        protected void NotifyPropertyChanged(string propertyName)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged.Invoke(this, new PropertyChangedEventArgs(propertyName));
            }
        }
        public event PropertyChangedEventHandler PropertyChanged;

        #endregion

        #region ISaveToXml
        public bool Load()
        {
            return Misc.SaveToXmlHepler.Load(param_path, this);
        }
        public bool Save()
        {
            return Misc.SaveToXmlHepler.Save(param_path, this);
        }
        public string[] GetSavePropertyNames()
        {
            return new string[]{
                "BoltWidth",
                "BoltCnt",
                "BorderType",
                "BorderBoltNo_B",
                "Smooth"
            };
        }
        #endregion
    }
}
