using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.ComponentModel;
using System.Collections;
using Microsoft.VisualBasic.CompilerServices;

namespace FAS_LIB
{
    public class clsFASorders
    {
        private scPlants lmPlants;
        private scLevels lmPlantLevel;
        public event clsFASorders.ueUpdateProgressEventHandler ueUpdateProgress;

        public bool ImportFAS(string ImportFile)
        {
            StreamReader streamReader;
            bool flag1;
            try
            {
                streamReader = File.OpenText(ImportFile);
            }
            catch (Exception ex)
            {
                clsFASorders.scUpdProgress scUpdProgress = new clsFASorders.scUpdProgress(clsFASorders.emUpdateProgType.ResultStatus, "Error opening FAS Export file: " + ImportFile + "\r\n");
                clsFASorders.ueUpdateProgressEventHandler updateProgressEvent = this.ueUpdateProgressEvent;
                if (updateProgressEvent != null)
                    updateProgressEvent((object)this, new ProgressChangedEventArgs(10, (object)scUpdProgress));
                flag1 = false;
                goto label_115;
            }
            clsEngineCTdB clsEngineCtdB = new clsEngineCTdB();
            Hashtable hashmdsq2ccd = new clsT2Q2CdB().getHashmdsq2ccd();
            clsPlant clsPlant = new clsPlant();
            this.lmPlants = clsPlant.rtnPlantByDesin("0085");
            this.lmPlants = clsPlant.rtnPlantById(1);
            this.lmPlantLevel = this.lmPlants.PlantLevel;
            clsAssemblyLine clsAssemblyLine = new clsAssemblyLine(this.lmPlants);
            List<scAssemblyLine> scAssemblyLineList1 = new List<scAssemblyLine>();
            List<scAssemblyLine> scAssemblyLineList2 = clsAssemblyLine.rtnAssemblyLines(true);
            streamReader.ReadLine().Split('\t');
            List<clsFASorders.scOrderFas> scOrderFasList = new List<clsFASorders.scOrderFas>();
            List<string> stringList1 = new List<string>();
            List<string> stringList2 = new List<string>();
            while (!streamReader.EndOfStream)
            {
                string[] strArray = streamReader.ReadLine().Split('\t');
                clsFASorders.scOrderFas scOrderFas = new clsFASorders.scOrderFas();
                if (strArray[14].Contains("MO"))
                {
                    if (strArray[15].Contains("Parent:"))
                        scOrderFas.OrderData.FOLineSLSS = strArray[15].Replace("Parent: ", "").Trim();
                }
                else
                    scOrderFas.OrderData.FactoryOrderOE = strArray[14].Trim();
                scOrderFas.OrderData.OrderJobName = strArray[11].Trim();
                if (scOrderFas.OrderData.FOFormatValid)
                {
                    if (strArray[0].Trim().StartsWith("NW") | strArray[0].Trim().StartsWith("GM") | strArray[0].Trim().StartsWith("394") | strArray[0].Trim().StartsWith("244") | strArray[0].Trim().StartsWith("252") | strArray[0].Trim().StartsWith("311"))
                    {
                        scItemMaster scItemMaster1 = new scItemMaster();
                        scItemMaster scItemMaster2 = clsEngineCtdB.rtnByAssyPartNum(strArray[0].Trim());
                        if (scItemMaster2 != null)
                        {
                            scOrderFas.OrdFASExt.OrdCatlgNbr = scItemMaster2.Description1;
                            scOrderFas.OrdFASExt.OrdMfgPartNbr = strArray[0].Trim();
                        }
                        else
                        {
                            scOrderFas.OrdFASExt.OrdCatlgNbr = strArray[0].Trim();
                            scOrderFas.OrdFASExt.OrdMfgPartNbr = strArray[1].Trim();
                        }
                    }
                    else
                    {
                        scOrderFas.OrdFASExt.OrdCatlgNbr = strArray[0].Trim();
                        scOrderFas.OrdFASExt.OrdMfgPartNbr = strArray[1].Trim();
                    }
                    scOrderFas.OrdFASExt.FASOrdQty = Conversions.ToInteger(strArray[2].Trim());
                    scOrderFas.OrdFASExt.FASUnfilledQty = Conversions.ToInteger(strArray[3].Trim());
                    scOrderFas.OrdFASExt.FASPrgrsPnt = Conversions.ToInteger(strArray[7].Trim());
                    short integer = checked((short)Conversions.ToInteger(strArray[7].Trim()));
                    scOrderFas.OrdFASExt.OrdCdDesc = hashmdsq2ccd[(object)integer].ToString().Trim();
                    scOrderFas.OrdFASExt.FASDueDt = Conversions.ToDate(strArray[12].Trim());
                    scOrderFas.OrdFASExt.FASOrdNbr = strArray[14].Trim();
                    scOrderFas.OrdFASExt.OrdProdLn = strArray[16].Trim();
                    scOrderFas.OrdFASExt.FASCmplQty = Conversions.ToInteger(strArray[17].Trim());
                    scOrderFas.OrdFASExt.FASAsmblLn = strArray[18].Trim();
                    scOrderFas.OrdFASExt.FASWorkCtr = strArray[19].Trim();
                    scOrderFasList.Add(scOrderFas);

                    //EXTRA CODE FOR TESTING
                    //if (Conversions.ToInteger(strArray[7].Trim()) >= 240)
                    //{
                    // int x =   Conversions.ToInteger(strArray[7].Trim()) ;
                    // DateTime dt = Conversions.ToDate(strArray[4].Trim());
                    // string s = scOrderFas.OrdFASExt.FASOrdNbr;
                    // int a = clsEngineCtdB.rtnOrderLineId(scOrderFas.OrderData.FOLineSLSS);
                    //}

                    //DOUBT IS HERE ON clsEngineCtdB.rtnOrderLineId(scOrderFas.OrderData.FOLineSLSS)
                    if (Conversions.ToInteger(strArray[7].Trim()) >= 240 & DateTime.Compare(Conversions.ToDate(strArray[4].Trim()), Conversions.ToDate("12/31/2039")) < 0 & !scOrderFas.OrdFASExt.FASOrdNbr.StartsWith("MO") && clsEngineCtdB.rtnOrderLineId(scOrderFas.OrderData.FOLineSLSS) == 0)
                    {
                        //EXTRA CODE
                        //int k = clsEngineCtdB.rtnOrderLineId(scOrderFas.OrderData.FOLineSLSS);
                        scOrderImportInfo BackOrders = new scOrderImportInfo();
                        BackOrders.SEPlant = this.lmPlants;
                        string str = "";
                        string fasAsmblLn = scOrderFas.OrdFASExt.FASAsmblLn;
                        if (Microsoft.VisualBasic.CompilerServices.Operators.CompareString(fasAsmblLn, "221", false) == 0)
                            str = "NW221C";
                        else if (Microsoft.VisualBasic.CompilerServices.Operators.CompareString(fasAsmblLn, "221B", false) == 0)
                            str = "NW221B";
                        else if (Microsoft.VisualBasic.CompilerServices.Operators.CompareString(fasAsmblLn, "CUKIT", false) == 0)
                            str = "221KITS";
                        else if (Microsoft.VisualBasic.CompilerServices.Operators.CompareString(fasAsmblLn, "FAB", false) == 0)
                        {
                            if (scOrderFas.OrdFASExt.FASWorkCtr.Contains("221PKKIT"))
                                str = "221KITS";
                            if (scOrderFas.OrdFASExt.FASWorkCtr.Contains("221ASSEM") | scOrderFas.OrdFASExt.FASWorkCtr.Contains("221PACK"))
                                str = "221CT";
                        }
                        bool flag2 = false;
                        List<scAssemblyLine>.Enumerator enumerator;
                        if (str.Length > 0)
                        {
                            try
                            {
                                enumerator = scAssemblyLineList2.GetEnumerator();
                                while (enumerator.MoveNext())
                                {
                                    scLevels current = (scLevels)enumerator.Current;
                                    if (current.LevelAbbrv.Contains(str))
                                    {
                                        BackOrders.AssemblyLine = current;
                                        flag2 = true;
                                        break;
                                    }
                                }
                            }
                            finally
                            {
                                //enumerator.Dispose();
                            }
                        }
                        if (flag2)
                        {
                            BackOrders.OrderFO = scOrderFas.OrderData.OrderFO;
                            BackOrders.OrderLineNum = scOrderFas.OrderData.OrderLineNum;
                            BackOrders.OrderLineSL = scOrderFas.OrderData.OrderLineSL;
                            BackOrders.OrderLineSS = scOrderFas.OrderData.OrderLineSS;
                            BackOrders.OrderLineShipDt = Conversions.ToDate(strArray[4].Trim());
                            BackOrders.OrderLineTargetDt = Conversions.ToDate(strArray[4].Trim());
                            BackOrders.OrderPurchaser = scOrderFas.OrderData.OrderJobName;
                            BackOrders.OrderLineNB4Dt = Conversions.ToDate(strArray[6].Trim());
                            BackOrders.OrderLineOrgShipDt = Conversions.ToDate(strArray[5].Trim());
                            BackOrders.OrderLineQty = Conversions.ToInteger(strArray[2].Trim());
                            new clsBackorders(this.lmPlantLevel, BackOrders.AssemblyLine).InsertBackOrderLines(BackOrders);
                            clsFASorders.scUpdProgress scUpdProgress = new clsFASorders.scUpdProgress(clsFASorders.emUpdateProgType.ResultStatus, scOrderFas.OrderData.FactoryOrderOE + " was created in VMS\r\n");
                            clsFASorders.ueUpdateProgressEventHandler updateProgressEvent = this.ueUpdateProgressEvent;
                            //if (updateProgressEvent != null)
                            //    updateProgressEvent((object)this, new ProgressChangedEventArgs(10, (object)scUpdProgress));
                        }
                    }
                }
            }

            label_115:
            return true;
        }

        private void ueUpdateProgressEvent(object sender, ProgressChangedEventArgs e)
        {
            throw new NotImplementedException();
        }

        public delegate void ueUpdateProgressEventHandler(object sender, ProgressChangedEventArgs e);

        public class scOrderFas
        {
            private scOrderData _OrderData;
            private scOrdFASExt _OrdFASExt;
            private scOrdFASExtDtl _OrdFASExtDtl;

            public scOrderData OrderData
            {
                get
                {
                    return this._OrderData;
                }
                set
                {
                    this._OrderData = value;
                }
            }

            public scOrdFASExt OrdFASExt
            {
                get
                {
                    return this._OrdFASExt;
                }
                set
                {
                    this._OrdFASExt = value;
                }
            }

            public scOrdFASExtDtl OrdFASExtDtl
            {
                get
                {
                    return this._OrdFASExtDtl;
                }
                set
                {
                    this._OrdFASExtDtl = value;
                }
            }

            public scOrderFas()
            {
                this._OrderData = new scOrderData();
                this._OrdFASExt = new scOrdFASExt();
                this._OrdFASExtDtl = new scOrdFASExtDtl();
            }
        }

        public class scUpdProgress
        {
            private clsFASorders.emUpdateProgType _UpdateType;
            private string _UpdateStatus;

            public clsFASorders.emUpdateProgType UpdateType
            {
                get
                {
                    return this._UpdateType;
                }
                set
                {
                    this._UpdateType = value;
                }
            }

            public string UpdateStatus
            {
                get
                {
                    return this._UpdateStatus;
                }
                set
                {
                    this._UpdateStatus = value;
                }
            }

            public scUpdProgress()
            {
                this._UpdateType = clsFASorders.emUpdateProgType.OrderInfo;
                this._UpdateStatus = "";
            }

            public scUpdProgress(clsFASorders.emUpdateProgType UpdType, string UpdStatus)
            {
                this._UpdateType = UpdType;
                this._UpdateStatus = UpdStatus;
            }
        }

        public enum emUpdateProgType
        {
            OrderInfo,
            ResultStatus,
        }
    }
    
}
