using Microsoft.VisualBasic;
using Microsoft.VisualBasic.CompilerServices;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text;

namespace FAS_LIB
{
    public class clsBackorders
    {
        private scLevels _Plant;
        private scLevels _AssemblyLine;
        private clsLogWriter lcLogWriter;

        public clsBackorders(scLevels Plant, scLevels AssemblyLine)
        {
            this._Plant = Plant;
            this._AssemblyLine = AssemblyLine;
            this.lcLogWriter = new clsLogWriter("C:\\LogFiles\\BOUImport.log");
            //this.lcBWT2 = new ecDataInterface();
            //clsDBC clsDbc = new clsDBC();
            //clsDbc.GetServer((object)"SE0085CTEntities1");
            //this._CTConnection = Conversions.ToString(clsDbc.ConnectionString);
        }

        public void InsertBackOrderLines(scOrderImportInfo BackOrders)
        {
            string str1 = "";
            clsEngineMES clsEngineMes = new clsEngineMES(this._Plant, this._AssemblyLine);
            scOrderImportInfo ldBackOrder = new scOrderImportInfo();
            ldBackOrder = BackOrders;

            using (SE0085CTEntities1 se0085CtEntities1 = new SE0085CTEntities1())
            {
                //ParameterExpression parameterExpression1;                
                //IQueryable<AssemblyLine> source1 = (IQueryable<AssemblyLine>)Queryable.Select(Queryable.Where((IQueryable<M0>)Queryable.Join((IQueryable<M0>)se0085CtEntities1.SEPlants, (IEnumerable<M1>)se0085CtEntities1.AssemblyLines, (Expression<Func<M0, M2>>)(p => p.SEPlantId), (Expression<Func<M1, M2>>)(a => a.SEPlant.SEPlantId), (Expression<Func<M0, M1, M3>>)((p, a) => new { p = p, a = a })), (Expression<Func<M0, bool>>)Expression.Lambda<Func<AnonymousType_5<SEPlant, AssemblyLine>, bool>>((Expression)Expression.Equal(It.a.AssemLineId, ldBackOrder.AssemblyLine.LevelId, true, (MethodInfo)null), new ParameterExpression[1]
                //{
                //  parameterExpression1
                //})), (Expression<Func<M0, M1>>)(data => data.a));

                IQueryable<AssemblyLine> source1 = (from sp in se0085CtEntities1.SEPlants
                                                    join al in se0085CtEntities1.AssemblyLines
                                                    on sp.SEPlantId equals al.SEPlantId
                                                    where int.Equals(sp.SEPlantActive, 1)
                                                    select new AssemblyLine
                                                    {
                                                        AssemLineId = al.AssemLineId,
                                                        SEPlantId = sp.SEPlantId,
                                                        AssemLineAbbrv = al.AssemLineAbbrv,
                                                        AssemLineName = al.AssemLineName,
                                                        AssemLineValue = al.AssemLineValue,
                                                        AssemLineActive = al.AssemLineActive
                                                    }).AsQueryable<AssemblyLine>();

                if (source1.Count<AssemblyLine>() == 0)
                {
                  int num1 = (int) Interaction.MsgBox((object) ("Error loading Assembly Line data, aborting MES import for AssemblyLineId; " + Conversions.ToString(ldBackOrder.AssemblyLine.LevelId)), MsgBoxStyle.Information, (object) "ONE QMS MES Import Error - E0001");
                }
                else if (source1.Count<AssemblyLine>() == 1)
                {
                    AssemblyLine assemblyLine = source1.First<AssemblyLine>();                    
                    //IQueryable<SEOrder> source2 = (IQueryable<SEOrder>)Queryable.Select<SEOrder, SEOrder>(Queryable.Where<SEOrder>((IQueryable<M0>)se0085CtEntities1.SEOrders, (Expression<Func<M0, bool>>)(o => Expression.Call((Expression)null, (MethodInfo)MethodBase.GetMethodFromHandle(__methodref(Operators.CompareString)), o.OrderFO, closure95.ldBackOrder.OrderFO, false) == 0)), (Expression<Func<M0, M1>>)(o => o));
                    IQueryable<SEOrder> source2 = (from s in se0085CtEntities1.SEOrders
                                                   where string.Equals(s.OrderFO, ldBackOrder.OrderFO)
                                                   select new SEOrder
                                                   {
                                                       OrderId = s.OrderId,
                                                       OrderFO = s.OrderFO,
                                                       OrderJobName = s.OrderJobName,
                                                       OrderPurchaser = s.OrderPurchaser
                                                   }).AsQueryable<SEOrder>();
                    SEOrder entity;
                    if (source2.Count<SEOrder>() == 0)
                    {
                        if (DateTime.Compare(ldBackOrder.OrderLineShipDt, Conversions.ToDate("12/31/2039")) == 0)
                            return;
                        entity = new SEOrder();
                        entity.OrderFO = ldBackOrder.OrderFO;
                        entity.OrderJobName = ldBackOrder.OrderJobName;
                        entity.OrderPurchaser = ldBackOrder.OrderPurchaser;
                        se0085CtEntities1.SEOrders.Add(entity);
                        se0085CtEntities1.SaveChanges();
                    }
                    else if (source2.Count<SEOrder>() == 1)
                    {
                        entity = source2.First<SEOrder>();
                        if (!(Operators.CompareString(entity.OrderJobName, ldBackOrder.OrderJobName, false) == 0 & Operators.CompareString(entity.OrderPurchaser, ldBackOrder.OrderPurchaser, false) == 0))
                        {
                            if (!ldBackOrder.OrderJobName.Contains("Part"))
                            {
                                entity.OrderJobName = ldBackOrder.OrderJobName;
                            }
                            entity.OrderPurchaser = ldBackOrder.OrderPurchaser;
                            se0085CtEntities1.SaveChanges();
                        }
                    }
                    else
                    {
                        int num2 = (int)Interaction.MsgBox((object)("Multiple Order numbers found, aborting MES import for Order; " + ldBackOrder.OrderFO), MsgBoxStyle.Information, (object)"ONE QMS MES Import Error - E0001");
                        return;
                    }
                }
                else
                {
                    int num3 = (int)Interaction.MsgBox((object)("Multiple AssemblyLine Ids found, aborting MES import for AssemblyLineId; " + Conversions.ToString(ldBackOrder.AssemblyLine.LevelId)), MsgBoxStyle.Information, (object)"ONE QMS MES Import Error - E0001");
                }
            }
        }

    }
}
