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

namespace ITC81DB_0H
{
    #region Enum
    public enum BarcodeStatus
    {
        None,
        Normal,
        NoRead,
        NoMatch,
    }
 
    #endregion

    public class BarcodeShortCut
    {
        public static readonly string MinAngle = "5362";
        public static readonly string MaxAngle = "5363";
        public static readonly string Frequency = "5364";
        public static readonly string ReadingPhaseTimeout = "78";
        public static readonly string Timeout = "79";
        public static readonly string ReadingConditions = "5250";
    }

    public class Barcode
    {
        #region Field
        public static byte[] EnterHostMode = { 0x1B, 0x5B, 0x43 };
        public static byte[] EnterTerminalMode = { 0x1B, 0x5D, 0x42 };
        public static byte[] EnterProgrammingMode = { 0x1B, 0x63, 0x4D, 0xB0, 0x30 };
        public static byte[] ExitProgrammingMode = { 0x1B, 0x64, 0x4D, 0xB0, 0x30 };
        public static byte[] ExitTerminalMode = { 0x1B, 0x49, 0x41, 0x20 };
        public static byte[] ExitHostMode = { 0x1B, 0x5B, 0x41 };
        public static byte[] AutoExitCommandMode = { 0x1B, 0x5B, 0x41 };
        public static byte[] SavePermanently = { 0x45, 0x20, 0x50, 0x0D, 0x0A };

        private bool m_IsCommandMode; // 현재 커맨드 모드인지 확인
        private int m_SaveValue; // 설정한 값 저장 변수
        private string m_SaveGettingShortCut; // 설정한 Getting ShortCut 변수
        private string m_SaveSettingShortCut; // 설정한 Setting ShortCut 변수

        private Queue<string> QueueBarcode;
        private Collection<string> CollectionLotNo;
        #endregion

        #region Constructor
        public Barcode(Collection<ProductItem> items, int productCount)
        {
            this.DefaultSetting(productCount);

            for (int i = 0; i < productCount; i++)
                this.CollectionLotNo[i] = items[i].LotNo;
        }
        #endregion

        #region Property
        public bool IsCommandMode
        {
            get { return this.m_IsCommandMode; }
            set { this.m_IsCommandMode = value; }
        }
        public int SaveValue
        {
            get { return this.m_SaveValue; }
            set { this.m_SaveValue = value; }
        }
        public string SaveGettingShortCut
        {
            get { return this.m_SaveGettingShortCut; }
            set { this.m_SaveGettingShortCut = value; }
        }
        public string SaveSettingShortCut
        {
            get { return this.m_SaveSettingShortCut; }
            set { this.m_SaveSettingShortCut = value; }
        }

        public int GetQueueCount
        {
            get
            {
                return this.QueueBarcode.Count;
            }
        }
        #endregion

        #region Method
        private void DefaultSetting(int barcodeCount)
        {
            this.IsCommandMode = false;
            this.QueueBarcode = new Queue<string>();

            this.CollectionLotNo = new Collection<string>();
            for (int i = 0; i < barcodeCount; i++)
                this.CollectionLotNo.Add("");
        }

        public void SetLotNo(Collection<ProductItem> items, int productCount)
        {
            for (int i = 0; i < productCount; i++)
                this.CollectionLotNo[i] = items[i].LotNo;
        }
        public void SetData(string data)
        {
            string value = "";

            value = this.DecisionLength(data);
            this.QueueBarcode.Enqueue(value);
        }
        public BarcodeStatus GetData(ref int productNo)
        {
            string value = "";
            BarcodeStatus ret = BarcodeStatus.None;

            if (this.QueueBarcode.Count > 0)
            {
                value = this.QueueBarcode.Peek();

                if (value == "NoRead")
                {
                    productNo = 1;
                    return ret = BarcodeStatus.NoRead;
                }
                else
                {
                    for (int i = 0; i < this.CollectionLotNo.Count; i++)
                    {
                        if (this.CollectionLotNo[i] == value)
                        {
                            productNo = i + 1;
                            return ret = BarcodeStatus.Normal;
                        }
                    }

                    productNo = 2;
                    return ret = BarcodeStatus.NoMatch;
                }
            }
         
            productNo = 0;
            return ret = BarcodeStatus.None;
        }

        /// <summary>
        /// 바코드 길이를 14 이하로 결정
        /// </summary>
        /// <param name="barcode"></param>
        /// <returns>string</returns>
        private string DecisionLength(string barcode)
        {
            if (barcode.Length > 14)
                return barcode.Substring(barcode.Length - 14, 14);
            else
                return barcode;
        }

        public void BarcodeDequeue()
        {
            if(this.QueueBarcode.Count > 0)
                this.QueueBarcode.Dequeue();
        }
        public void Clear()
        {
            this.QueueBarcode.Clear();
        }
        #endregion
    }
}