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

namespace StdLabelPrint
{
    public class CAmkorSTDBarcode
    {
        public string Message { get; private set; }
        public Boolean isValid = false;
        public string SID { get;  set; }
        public string VLOT { get;  set; }
        public string RID { get;  set; }
        public string MFGDate { get; set; }
        //public string MFGDateRaw { get; set; }
        public string VENDERNAME { get; set; }
        public string PARTNO { get; set; }
        public int QTY { get; set; }
        public string RAW { get; set; }
        public Boolean DateError { get; private set; }
        public Boolean DisposalCode { get; set; }

        public Boolean NewLen15Barcode { get; set; }

        public void CopyTo(CAmkorSTDBarcode obj)
        {
            if (obj == null) obj = new CAmkorSTDBarcode();
            else obj.Clear();
            obj.Message = this.Message;
            obj.isValid = this.isValid;
            obj.SID = this.SID;
            obj.VLOT = this.VLOT;
            obj.RID = this.RID;
            obj.MFGDate = this.MFGDate;
            obj.VENDERNAME = this.VENDERNAME;
            obj.PARTNO = this.PARTNO;
            obj.QTY = this.QTY;
            obj.RAW = this.RAW;
            obj.DateError = this.DateError;
            obj.DisposalCode = this.DisposalCode;
            obj.NewLen15Barcode = this.NewLen15Barcode;
        }
        public CAmkorSTDBarcode()
        {
            Clear();
        }
        public void Clear()
        {
            this.DisposalCode = false;
            this.NewLen15Barcode = false;
            this.Message = string.Empty;
            this.isValid = false;
            this.SID = string.Empty;
            this.VLOT = string.Empty;
            this.RID = string.Empty;
            //this.MFGDateRaw = string.Empty;
            this.MFGDate = string.Empty;// new DateTime(1982, 11, 23);
            this.VENDERNAME = string.Empty;
            this.PARTNO = string.Empty;
            this.QTY = 0;
            this.RAW = string.Empty;
            DateError = false;
        }
        public CAmkorSTDBarcode(string raw)
        {
            SetBarcode(raw);
        }
        public void SetBarcodeDemo()
        {
            //SetBarcode("101410653;AG64B3W;SAMSUNG;20000;AG64B3W0031;19000101;");
            SetBarcode("101410653;AG64B3W;SAMSUNG;20000;AG64B3W0031;19000101;");
        }

        /// <summary>
        /// 현재 속성의 값을 가지고 바코드값을 반환합니다.
        /// 원본 바코드 값을 확인하려면 RAW 속성을 확인하세요
        /// </summary>
        /// <returns></returns>
        public string GetBarcode()
        {
            if (string.IsNullOrWhiteSpace(PARTNO))
                return string.Format("{0};{1};{2};{3};{4};{5}",
                    SID, VLOT, VENDERNAME, QTY, RID, MFGDate);
            else
                return string.Format("{0};{1};{2};{3};{4};{5};{6}",
              SID, VLOT, VENDERNAME, QTY, RID, MFGDate, PARTNO);
        }
        public void SetBarcode(string raw)
        {
            //101416868;01A3KX;KYOCERA;20000;RC00014A225001I;20220511;N/A


            isValid = false;

            this.RAW = raw;
            var buf = raw.Split(';');
            if (buf.Length < 5)
            {
                isValid = false;
                Message = "buffer len error : " + raw;
                return;
            }
            decimal vSID = 0;
            double vQty = 0;
            if (decimal.TryParse(buf[0], out vSID) == false) return;

            this.SID = buf[0];
            this.VLOT = buf[1];


            //101인경우에는 3번 항목이 벤더네임이다.
            //103은 partno 값이 들어간다
            if (this.SID.StartsWith("103"))
            {
                if (buf.Length == 7)    //구형바코드
                {
                    this.VENDERNAME = buf[2];
                    this.PARTNO = string.Empty;
                    Message = "폐기된 103코드 입니다";
                    DisposalCode = true;
                }
                else
                {
                    //05월 신형은 파트번호가 들어있다
                    this.PARTNO = buf[2];
                    this.VENDERNAME = string.Empty;
                    DisposalCode = false;
                }
            }
            else
            {
                this.VENDERNAME = buf[2];
                this.PARTNO = string.Empty;
                DisposalCode = false;
            }

            //일부 데이터를 변조한다  210709
            if (this.VENDERNAME == "K118165") this.VENDERNAME = "DELTA";
            else if (this.VENDERNAME == "K113986") this.VENDERNAME = "DREAM CHIP";


            if (double.TryParse(buf[3], out vQty) == false) return;
            this.QTY = (int)vQty;

            this.RID = buf[4];
            //DateTime vMFGDate;
            DateError = false;
            this.MFGDate = buf[5].Trim();

            //신형바코드는 angle 에서 제외하려고 판정한다
            if (this.RID.Length == 15 && this.RID.StartsWith("RC")) //210703
                this.NewLen15Barcode = true;
            else
                this.NewLen15Barcode = false;


            //if (buf[5].Length == 4 && this.SUPPLY == "SAMSUNG")
            //{
            //    //삼성은 주차로 한다
            //    DateError = true;
            //    Message = "Date Error(samsung)";
            //    MFGDate = new DateTime(2000+ int.Parse( buf[5].Substring(0,2) ), 1, 1);

            //    //DateTime.Now.
            //}
            //else if (buf[5].Length != 8)
            //{
            //    Message = "Date value Length Error : " + buf[5];
            //    MFGDate = new DateTime(1900, 1, 1);
            //    return;
            //}
            //else
            //{
            //    buf[5] = buf[5].Substring(0, 4) + "-" + buf[5].Substring(4, 2) + "-" + buf[5].Substring(6, 2);
            //    if (DateTime.TryParse(buf[5], out vMFGDate) == false) return;
            //    MFGDate = vMFGDate;
            //}

            //마지막 요소가 PartNo 이다. (아직 이코드는 본적 없음)
            if (this.SID.StartsWith("103") && buf.Length == 7)   //구형번호이다
            {
                this.PARTNO = buf[6];
            }

            if (buf.Length == 7 && string.IsNullOrEmpty(this.PARTNO))
            {
                this.PARTNO = buf[6];
            }
            //else this.PARTNO = string.Empty;

            isValid = true;
        }
    }
}
