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

namespace FAS_LIB.COLA_DMS
{
    class EntityData
    {
        public static void fasFU(string AssyLine)
        {
            clsEngineCTdB clsEngineCtdB = new clsEngineCTdB();
            List<scOrderData> scOrderDataList1 = new List<scOrderData>();
            // Ship date range - Order list used to update Sql server from MES
            DateTime ShipDateStart = DateTime.Today.AddDays(-30);
            DateTime ShipDateEnd = DateTime.Today.AddDays(60);
            List<scOrderData> scOrderDataList2 = clsEngineCtdB.rtnOVDataAllOpen(AssyLine, ShipDateStart, ShipDateEnd);
            clsPlant clsPlant = new clsPlant();
            scPlants scPlants1 = new scPlants();
            scPlants scPlants2 = clsPlant.rtnPlantById(1);
            clsAssemblyLine clsAssemblyLine = new clsAssemblyLine(scPlants2);
            scAssemblyLine scAssemblyLine1 = new scAssemblyLine();
            scAssemblyLine scAssemblyLine2 = clsAssemblyLine.rtnAssemblyLineByAbbrv(scPlants2.PlantId, AssyLine);
            clsEngineMES clsEngineMes = new clsEngineMES(scPlants2.PlantLevel, scAssemblyLine2.AssemLevel);
            List<scMESUpdateFull> scMesUpdateFullList = new List<scMESUpdateFull>();
            foreach (scOrderData scOrderData in scOrderDataList2)
            {
                string orderFo = scOrderData.OrderFO;
                string orderLineNum = scOrderData.OrderLineNum;
                string orderLineSl = scOrderData.OrderLineSL;
                string orderLineSs = scOrderData.OrderLineSS;
                List<scMESUpdateFull> OrderList = clsEngineMes.LoadMESOrders(scAssemblyLine2.LevelAbbrv, orderFo, orderLineNum, orderLineSl, orderLineSs);


                if (OrderList != null && OrderList.Count > 0)
                {
                    // Add Order information to string
                    string OrderInfo = OrderList[0].hd_q2c_nbr + @" " + OrderList[0].od_ln_dsply_seq_nbr + @" " + DateTime.Now;
                    try
                    {
                        clsEngineMes.ScheduleOrder(ref OrderList);
                        clsEngineMes.MESLoad(scPlants2, (scLevels)scAssemblyLine2, OrderList, true, false);

                    }
                    // Capture any Exceptions and print the Order inforation for the current order
                    catch
                    {

                    }
                }
            }
            scOrderDataList1 = (List<scOrderData>)null;
            scPlants1 = (scPlants)null;
            scAssemblyLine1 = (scAssemblyLine)null;
            scMesUpdateFullList = (List<scMESUpdateFull>)null;
        }

        public static void LoadMESdata(string AssyLineAbbrv, emLoadOrdEngr LoadByDt = emLoadOrdEngr.Act)
        {
            string str = AssyLineAbbrv;
            clsEngineCTdB clsEngineCtdB = new clsEngineCTdB();
            List<scOrderImportInfo> scOrderImportInfoList1 = new List<scOrderImportInfo>();
            DateTime ReleaseDtStart = DateTime.Today.AddDays(-30);
            DateTime today = DateTime.Today;
            List<scOrderImportInfo> scOrderImportInfoList2;
            switch (LoadByDt)
            {
                case emLoadOrdEngr.Org:
                    scOrderImportInfoList2 = clsEngineCtdB.rtnEngrOrderStatus(str, ReleaseDtStart, today);
                    break;
                case emLoadOrdEngr.Act:
                    scOrderImportInfoList2 = clsEngineCtdB.rtnEngrActRelease(str, ReleaseDtStart, today);
                    break;
                default:
                    scOrderImportInfoList2 = clsEngineCtdB.rtnEngrActRelease(str, ReleaseDtStart, today);
                    break;
            }
            clsPlant clsPlant = new clsPlant();
            scPlants scPlants1 = new scPlants();
            scPlants scPlants2 = clsPlant.rtnPlantById(1);
            clsAssemblyLine clsAssemblyLine = new clsAssemblyLine(scPlants2);
            scAssemblyLine scAssemblyLine1 = new scAssemblyLine();
            scAssemblyLine scAssemblyLine2 = clsAssemblyLine.rtnAssemblyLineByAbbrv(scPlants2.PlantId, str);
            clsEngineMES clsEngineMes = new clsEngineMES(scPlants2.PlantLevel, scAssemblyLine2.AssemLevel);
            List<scMESUpdateFull> scMesUpdateFullList = new List<scMESUpdateFull>();
            foreach (scOrderImportInfo scOrderImportInfo in scOrderImportInfoList2)
            {
                string orderFo = scOrderImportInfo.OrderFO;
                string orderLineNum = scOrderImportInfo.OrderLineNum;
                string orderLineSl = scOrderImportInfo.OrderLineSL;
                string orderLineSs = scOrderImportInfo.OrderLineSS;
                List<scMESUpdateFull> OrderList = clsEngineMes.LoadMESOrders(scAssemblyLine2.LevelAbbrv, orderFo, orderLineNum, orderLineSl, orderLineSs);



                if (OrderList != null && OrderList.Count > 0)
                {
                    // Add Order information to string
                    string OrderInfo = OrderList[0].hd_q2c_nbr + @" " + OrderList[0].od_ln_dsply_seq_nbr + @" " + OrderList[0].hd_sub_ln_itm_id
                    + @" " + DateTime.Now;
                    try
                    {

                        clsEngineMes.ScheduleOrder(ref OrderList);
                        clsEngineMes.MESLoad(scPlants2, (scLevels)scAssemblyLine2, OrderList, true, false);

                    }
                    // Capture any Exceptions and print the Order inforation for the current order
                    catch
                    {

                        Console.WriteLine(OrderList[0].hd_q2c_nbr + " " + OrderList[0].od_ln_dsply_seq_nbr);
                    }
                }
            }
            scOrderImportInfoList1 = (List<scOrderImportInfo>)null;
            scPlants1 = (scPlants)null;
            scAssemblyLine1 = (scAssemblyLine)null;
            scMesUpdateFullList = (List<scMESUpdateFull>)null;
        }

        public enum emLoadOrdEngr
        {
            Org,
            Act,
        }

    }
}
