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

namespace ITC81DB
{
    #region Enum
    public class RFIDDataStore
    {
        public enum VerifyCode
        {
            Empty,
            Fail_Weight,
            Fail_Filter,
            Fail_StandardCode,
            Pass,
        }
    }
    #endregion

    public class RFID_Impinj_Speedway_R420
    {
        #region Field
        private List<string> ListTag;

        private string m_BARCD_Binary;
        private string m_BARCD_EPC_Filter;
        #endregion

        #region Constructor
        public RFID_Impinj_Speedway_R420(string BARCD, string Epc_Filter)
        {
            this.ListTag = new List<string>();

            this.ChangeBARCDAndEPCFilter(BARCD, Epc_Filter);
        }
        #endregion

        #region Property
        public string BARCD_Binary
        {
            get { return this.m_BARCD_Binary; }
            set { this.m_BARCD_Binary = value; }
        }
        public string BARCD_EPC_Filter
        {
            get { return this.m_BARCD_EPC_Filter; }
            set { this.m_BARCD_EPC_Filter = value; }
        }
        #endregion

        #region Method
        public void ChangeBARCDAndEPCFilter(string BARCD, string Epc_Filter)
        {
            this.BARCD_Binary = Helper.DecToBin(BARCD.PadLeft(44, '0'));
            this.BARCD_EPC_Filter = Helper.DecToBin(Epc_Filter).PadLeft(3, '0');
        }

        /// <summary>
        /// Tag에서 물류코드 추출
        /// </summary>
        /// <param name="tag">태그값</param>
        /// <returns>물류코드 13자리</returns>
        public string VerifyBARCD(string tag)
        {
            string barcd = "";

            barcd = Helper.HexToBin(tag.Substring(0, 15)).PadLeft(60, '0');
            barcd = barcd.Substring(14, 44);
            barcd = Helper.BinToDec(barcd).PadLeft(13, '0');

            return barcd;
        }

        public RFIDDataStore.VerifyCode CompareToBARCD(string tag)
        {
            string temp = "";
            temp = Helper.HexToBin(tag.Substring(0, 15)).PadLeft(60, '0');

            // Filter 비교
            if (this.BARCD_EPC_Filter != temp.Substring(8, 3))
            {
                return RFIDDataStore.VerifyCode.Fail_Filter;
            }
            else
            {
                // TAG와 품목번호 비교
                if (this.BARCD_Binary == temp.Substring(14, 44))
                    return RFIDDataStore.VerifyCode.Pass;
                else
                    return RFIDDataStore.VerifyCode.Fail_StandardCode;
            }
        }
        public RFIDDataStore.VerifyCode DefineCode(string tag, DataStore.JudgmentStatus status)
        {
            if (tag == "")
                return RFIDDataStore.VerifyCode.Empty;

            if(tag == "000000000000000000000000")
                return RFIDDataStore.VerifyCode.Empty;

            if (status != DataStore.JudgmentStatus.Pass)
                return RFIDDataStore.VerifyCode.Fail_Weight;

            return this.CompareToBARCD(tag);
        }

        public bool IsTagInList(string tag)
        {
            for (int i = 0; i < this.ListTag.Count; i++)
            {
                if (this.ListTag[i] == tag) // 동일한 Tag가 있음
                    return true;
            }
            return false;
        }       
        public string ReturnTag(int index)
        {
            if (this.ListTag.Count > index && index >= 0)
                return this.ListTag[index];
            else
                return "0";
        }
        public void RemoveIndex(int index)
        {
            if (this.ListTag.Count > 0)
                this.ListTag.RemoveAt(index);
        }

        public bool Add(string sValue, int index)
        {
            bool ret = false;

            if (this.ListTag.Count > 0 && this.ListTag[index] == "")
            {
                this.ListTag[index] = sValue;
                ret = true;
            }
            else
                ret = false;

            return ret;
        }
        public void Add(string sValue)
        {
            this.ListTag.Add(sValue);
        }
        public int Count()
        {
            return this.ListTag.Count;
        }
        public void Clear()
        {
            this.ListTag.Clear();
        }
        #endregion
    }
}
