private string GetSeatSummaryDescription(string itemNumber, int lineId)
        {
            var items = ItemMasterCacheManager.ItemNumberDescriptions.Where(i => i.ItemNumber.Trim() == itemNumber).ToList();
            string description = "";
            ItemDetailsFactory model = new ItemDetailsFactory();
            var item = model.GetItemInfo(itemNumber);
            if (items.Count() > 0)
            {
                if (lineId == 1)
                {
                    var first = item.ItemInfos.Where(s => s.PartIndex == 1).FirstOrDefault();
                    var second = item.ItemInfos.Where(s => s.PartIndex == 2).FirstOrDefault();
                    if (first != null)
                    {
                        description = ItemDetailsFactory.GetOptionValue(first, 100) + " " + ItemDetailsFactory.GetOptionValue(first, 101) + " " 
                            + ItemDetailsFactory.GetOptionValue(first, 3) + " " + ItemDetailsFactory.GetOptionValue(first, 10);
                    }
                }
                else if (lineId == 2)
                {
                    var first = item.ItemInfos.Where(s => s.PartIndex == 1).FirstOrDefault();
                    var second = item.ItemInfos.Where(s => s.PartIndex == 2).FirstOrDefault();
                    if (first != null)
                    {
                        if (first.ItemType == "2B")
                            description = "BENCH";
                        else
                        {
                            description = ItemDetailsFactory.GetOptionValue(first, 100) + " " + ItemDetailsFactory.GetOptionValue(first, 101) + " "
                                + ItemDetailsFactory.GetOptionValue(first, 10) + " " + ItemDetailsFactory.GetOptionValue(first, 9) + " "
                                + ItemDetailsFactory.GetOptionValue(first, 13) + " " + ItemDetailsFactory.GetOptionValue(first, 11);
                        }

                    }
                }
                else if (lineId == 3)
                {
                    var third = item.ItemInfos.Where(s => s.PartIndex == 3).FirstOrDefault();
                    var fourth = item.ItemInfos.Where(s => s.PartIndex == 4).FirstOrDefault();
                    if (third != null)
                    {
                        description = ItemDetailsFactory.GetOptionValue(third, 100) + " " + ItemDetailsFactory.GetOptionValue(third, 101) + " "
                            + ItemDetailsFactory.GetOptionValue(third, 3) + " " + ItemDetailsFactory.GetOptionValue(third, 7) + " " 
                            + ItemDetailsFactory.GetOptionValue(third, 9) + " " + ItemDetailsFactory.GetOptionValue(third, 10);
                    }
                }

                return System.Text.RegularExpressions.Regex.Replace(description, @"\s+", " ");

            }

            return "*Unknown Sku* (" + itemNumber + ")";
        }
        public TrimScheduleInfo GetTrimScheduleHistoryDetail(int locationId, int scheduleNum)
        {
            PartSequencingDataContext dbSeq = new PartSequencingDataContext();
            ItemDetailsFactory itemDetailsModel = new ItemDetailsFactory();
            SequencingFactory factory = new SequencingFactory();
            var seqMessageProcessor = new SeqMessageProcessor();

            var seqStation = dbSeq.SequencingStationSettings.Where(i => i.SequencingLocationId == locationId).FirstOrDefault();
            var tokens = MessageTokensCacheManager.MessageTokens.Where(s => s.LineId == seqStation.LineId || s.LineId == 4).ToList();
            var station = factory.UpdateNextBoxBySequencingId(seqStation.Id);

            TrimScheduleInfo trimHistorySchedule = null;

            if (station != null)
            {
                SequencingTrackingItem lastItem = null;
                var tracking = factory.GetSequencingHistory(station);

                if (tracking.Any())
                {
                    lastItem = tracking.OrderByDescending(i => i.OrderId).First();
                    if (lastItem != null)
                    {
                        var historyjobs = (from t in dbSeq.IWS_GetSequencingDetailHistoryBySeqSettingsIdAndScheduleNum(station.Id, scheduleNum)
                                          select new Seat
                                          {
                                            ScheduleNum = t.ScheduleNum,
                                            BoxNum = t.BoxNum,
                                            Complete = false,
                                            TrimStyle = GetOptionValue(t.ItemNumber.Trim(), 100),
                                            Colour = GetOptionValue(t.ItemNumber.Trim(), 101),
                                            ItemNumber = t.ItemNumber.Trim(),
                                            ItemType = t.ItemType.Trim(),
                                            DescLine1 = t.ItemNumber.Trim(),
                                            DescLine2 = seqMessageProcessor.GetSequencingMessage(station, t.ItemNumber.Trim(), tokens),
                                            OrderId = t.OrderId,
                                            Item = itemDetailsModel.GetItemInfoByLineId(t.ItemNumber.Trim(), station.Line.Id)
                                          }).ToList();

                        var firstItem = historyjobs.FirstOrDefault();

                        trimHistorySchedule = new TrimScheduleInfo
                        {
                            Complete = true,
                            ScheduleStatus = new TrimScheduleStatus
                            {
                                CurrentBoxNumber = firstItem == null ? 0 : firstItem.BoxNum,
                                CurrentScheduleNumber = firstItem == null ? 0 : firstItem.ScheduleNum
                            },
                            SeatSchedule = historyjobs,
                            SummaryList = GetSummaryHeaderItems(historyjobs, station.Line.Id),
                        };
                        return trimHistorySchedule;
                    }

                }

            }
            return trimHistorySchedule;
        }
        public string GetOptionValue(string itemNumber, int optionNum)
        {
            ItemDetailsFactory itemDetailsModel = new ItemDetailsFactory();
            var item = itemDetailsModel.GetItemInfo(itemNumber);
            string optionString = "";

            if (item == null)
                return optionString;

            if (item.ItemInfos.Count() > 0)
            {
                var firstpart = item.ItemInfos.FirstOrDefault();
                Option option = firstpart.Options.Where(i => i.OptionNumber == optionNum).FirstOrDefault();
                if (option != null)
                {
                    if (option.OptionValue != null)
                        return option.OptionValue.Trim();
                }
            }

            return optionString;
        }
        public TrimSchedule GetTrimSchedule(int id)
        {
            PartSequencingDataContext dbSeq = new PartSequencingDataContext();
            SequencingFactory factory = new SequencingFactory();
            HP_MESDataContext dbMes = new HP_MESDataContext();
            ItemDetailsFactory itemDetailsModel = new ItemDetailsFactory();
            TrimSchedule trimSchedule = null;
            List<Seat> offsetSeats = null;
            List<Seat> currentjobs = null;
            var seqMessageProcessor = new SeqMessageProcessor();

            var seqStations = dbSeq.SequencingStationSettings.Where(i => i.SequencingLocationId == id).ToList();
            if (seqStations.Count() > 0)
            {
                var seqStation = seqStations.FirstOrDefault();
                if (seqStation != null)
                {
                    var station = factory.UpdateNextBoxBySequencingId(seqStation.Id);
                    var scheduleStatus = dbSeq.SequencingStationLookups.Where(s => s.SequenceStationSettingsId == station.Id).FirstOrDefault();
                    var tokens = MessageTokensCacheManager.MessageTokens.Where(s => s.LineId == station.Line.Id || s.LineId == 4).ToList();

                    var lineBuffer = factory.GetSequencingBuffer(station);

                    if (lineBuffer != null)
                    {
                        var itemsinbox = station.PartsPerBox;
                        var boxperschedule = station.BoxPerSchedule;
                        var itemsinschedule = itemsinbox * boxperschedule;
                        var boxqty = station.BoxQty;

                        trimSchedule = new TrimSchedule();

                        var tracking = factory.GetSequencingHistory(station);
                        if (tracking.Any()) 
                        {
                            var lastTrackItem = tracking.LastOrDefault();
                            if (lastTrackItem != null)
                            {
                                if (lastTrackItem.Type == 2)
                                {
                                    var lastRealJob = tracking.Where(o => o.OrderId < lastTrackItem.OrderId).OrderByDescending(f => f.OrderId).Where(x => x.ScheduleNum > 0).FirstOrDefault();

                                    if (lastRealJob != null)
                                    {
                                        var unassignedBuffer = tracking.Where(o => o.OrderId <= lastTrackItem.OrderId && o.OrderId >lastRealJob.OrderId).OrderBy(f => f.OrderId).ToList();
                                        if (unassignedBuffer.Count() > 0)
                                        {
                                            var offsetjobs = (from j in unassignedBuffer
                                                              select new Seat
                                                              {
                                                                  OrderStatus = (int)j.OrderStatus,
                                                                  Complete = false,
                                                                  TrimStyle = GetOptionValue(j.ItemNumber.Trim(), 100),
                                                                  Colour = GetOptionValue(j.ItemNumber.Trim(), 101),
                                                                  OrderNumber = j.OrderNumber == null ? "" : j.OrderNumber.Trim(),
                                                                  ItemNumber = j.ItemNumber.Trim(),
                                                                  ItemType = j.ItemType.Trim(),
                                                                  DescLine1 = j.ItemNumber.Trim(),
                                                                  DescLine2 = seqMessageProcessor.GetSequencingMessage(station, j.ItemNumber.Trim(), tokens),
                                                                  JobId = (int)j.JobId,
                                                                  OrderId = j.OrderId,
                                                                  Item = itemDetailsModel.GetItemInfoByLineId(j.ItemNumber.Trim(), station.Line.Id)
                                                              }).ToList();

                                            offsetSeats = offsetjobs;
                                        }
                                    }

                                }
                            }


                        }


                        if (station.SequencingLocation.ParentId == null)
                        {
                            currentjobs = (from j in lineBuffer.Where(i => i.OrderId >= station.NextBox.OrderId).Take(itemsinschedule).ToList()
                                           select new Seat
                                           {
                                               OrderStatus = (int)j.OrderStatus,
                                               Complete = false,
                                               TrimStyle = GetOptionValue(j.ItemNumber.Trim(), 100),
                                               Colour = GetOptionValue(j.ItemNumber.Trim(), 101),
                                               OrderNumber = j.OrderNumber == null ? "" : j.OrderNumber.Trim(),
                                               ItemNumber = j.ItemNumber.Trim(),
                                               ItemType = j.ItemType.Trim(),
                                               DescLine1 = j.ItemNumber.Trim(),
                                               DescLine2 = seqMessageProcessor.GetSequencingMessage(station, j.ItemNumber.Trim(), tokens),
                                               JobId = (int)j.JobId,
                                               OrderId = j.OrderId,
                                               Item = itemDetailsModel.GetItemInfoByLineId(j.ItemNumber.Trim(), station.Line.Id),
                                           }).OrderBy(s => s.OrderId).ToList();
                        }
                        else //from queue
                        {
                            currentjobs = (from j in lineBuffer.Where(i => i.OrderId >= station.NextBox.OrderId).Take(itemsinschedule).ToList()
                                            select new Seat
                                            {
                                                ScheduleNum = j.ScheduleNum,
                                                BoxNum = j.BoxNum,
                                                OrderStatus = (int)j.OrderStatus,
                                                Complete = false,
                                                TrimStyle = GetOptionValue(j.ItemNumber.Trim(), 100),
                                                Colour = GetOptionValue(j.ItemNumber.Trim(), 101),
                                                OrderNumber = j.OrderNumber == null ? "" : j.OrderNumber.Trim(),
                                                ItemNumber = j.ItemNumber.Trim(),
                                                ItemType = j.ItemType.Trim(),
                                                DescLine1 = j.ItemNumber.Trim(),
                                                DescLine2 = seqMessageProcessor.GetSequencingMessage(station, j.ItemNumber.Trim(), tokens),
                                                JobId = (int)j.JobId,
                                                OrderId = j.OrderId,
                                                Item = itemDetailsModel.GetItemInfoByLineId(j.ItemNumber.Trim(), station.Line.Id),
                                            }).OrderBy(s => s.OrderId).ToList();
                        }


                        if (currentjobs.Count() != itemsinschedule)
                            return null;

                        for(int i=0; i<currentjobs.Count(); i++)
                            currentjobs[i].Index = i;

                        trimSchedule.CurrentSchedule = new TrimScheduleInfo
                        {
                            Complete = false,
                            SeatSchedule = currentjobs,
                            SummaryList = GetSummaryHeaderItems(currentjobs, station.Line.Id),
                            ScheduleStatus = GetTrimScheduleStatus(station, scheduleStatus, currentjobs),
                            OffsetSeats = offsetSeats
                        };

                        return trimSchedule;

                    }
                }
            }
            return null;
        }
        public TrimScheduleInfo GetNextTrimSchedule(int locationId)
        {
            PartSequencingDataContext dbSeq = new PartSequencingDataContext();
            SequencingFactory factory = new SequencingFactory();
            HP_MESDataContext dbMes = new HP_MESDataContext();
            ItemDetailsFactory itemDetailsModel = new ItemDetailsFactory();
            TrimScheduleInfo nextTrimScheduleInfo = null;

            var seqMessageProcessor = new SeqMessageProcessor();

            var seqStations = dbSeq.SequencingStationSettings.Where(i => i.SequencingLocationId == locationId).ToList();
            if (seqStations.Count() > 0)
            {
                var seqStation = seqStations.FirstOrDefault();
                if (seqStation != null)
                {
                    var station = factory.UpdateNextBoxBySequencingId(seqStation.Id);
                    var scheduleStatus = dbSeq.SequencingStationLookups.Where(s => s.SequenceStationSettingsId == station.Id).FirstOrDefault();
                    var tokens = MessageTokensCacheManager.MessageTokens.Where(s => s.LineId == station.Line.Id || s.LineId == 4).ToList();

                    var lineBuffer = factory.GetSequencingBuffer(station);

                    if (lineBuffer != null)
                    {
                        var itemsinbox = station.PartsPerBox;
                        var boxperschedule = station.BoxPerSchedule;
                        var itemsinschedule = itemsinbox * boxperschedule;
                        var boxqty = station.BoxQty;

                        var currentjobs = (from j in lineBuffer.Where(i => i.OrderId >= station.NextBox.OrderId).Take(itemsinschedule).ToList()
                                           select new Seat
                                           {
                                               OrderStatus = (int)j.OrderStatus,
                                               Complete = false,
                                               TrimStyle = GetOptionValue(j.ItemNumber.Trim(), 100),
                                               Colour = GetOptionValue(j.ItemNumber.Trim(), 101),
                                               OrderNumber = j.OrderNumber == null ? "" : j.OrderNumber.Trim(),
                                               ItemNumber = j.ItemNumber.Trim(),
                                               ItemType = j.ItemType.Trim(),
                                               DescLine1 = j.ItemNumber.Trim(),
                                               DescLine2 = seqMessageProcessor.GetSequencingMessage(station, j.ItemNumber.Trim(), tokens),
                                               JobId = (int)j.JobId,
                                               OrderId = j.OrderId,
                                               Item = itemDetailsModel.GetItemInfoByLineId(j.ItemNumber.Trim(), station.Line.Id),
                                           }).OrderBy(s => s.OrderId).ToList();

                        if (currentjobs.Count() != itemsinschedule)
                            return null;

                        var lastcurrentjob = currentjobs.Last();

                        var nextjobs = (from j in lineBuffer.Where(i => i.OrderId > lastcurrentjob.OrderId).Take(itemsinschedule).ToList()
                                        select new Seat
                                        {
                                            OrderStatus = (int)j.OrderStatus,
                                            Complete = false,
                                            TrimStyle = GetOptionValue(j.ItemNumber.Trim(), 100),
                                            Colour = GetOptionValue(j.ItemNumber.Trim(), 101),
                                            OrderNumber = j.OrderNumber == null ? "" : j.OrderNumber.Trim(),
                                            ItemNumber = j.ItemNumber.Trim(),
                                            ItemType = j.ItemType.Trim(),
                                            DescLine1 = j.ItemNumber.Trim(),
                                            DescLine2 = seqMessageProcessor.GetSequencingMessage(station, j.ItemNumber.Trim(), tokens),
                                            JobId = (int)j.JobId,
                                            OrderId = j.OrderId,
                                            Item = itemDetailsModel.GetItemInfoByLineId(j.ItemNumber.Trim(), station.Line.Id)
                                        }).OrderBy(s => s.OrderId).ToList();

                        if (nextjobs.Count() == itemsinschedule)
                        {
                            nextTrimScheduleInfo = new TrimScheduleInfo
                            {
                                Complete = false,
                                SeatSchedule = nextjobs,
                                SummaryList = GetSummaryHeaderItems(nextjobs, station.Line.Id),
                                ScheduleStatus = new TrimScheduleStatus
                                {
                                    CurrentBoxNumber = (scheduleStatus.NextBoxNum + 1) > boxqty ? 1 : scheduleStatus.NextBoxNum + 1,
                                    CurrentScheduleNumber = scheduleStatus.NextScheduleNum + 1
                                }
                            };
                        }
                        else
                        {
                            return null;
                        }

                        for (int i = 0; i < nextjobs.Count(); i++)
                            nextjobs[i].Index = i;

                        return nextTrimScheduleInfo;

                    }
                }
            }
            return null;
        }
        public string GetSequencingMessage(SequencingStation station, string itemNumber, IEnumerable<MessageToken> lineTokens)
        {
            #region Get Sequencing Message
            ItemDetailsFactory itemDetailsModel = new ItemDetailsFactory();
            var item = itemDetailsModel.GetItemInfo(itemNumber);

            if (item == null)
                return String.Format("*Unknown Part* ({0})", itemNumber);

            string[] tokens = station.MessageFormat.Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);

            var formattedMsg = station.MessageFormat.Trim();

            if (tokens[0] == null) throw new NotImplementedException();


            var filteredParts = item.ItemInfos.Where(
                                part =>
                                ((ItemDetailsFactory.GetBuildcodeOption(part) & station.SeatOptions) ==
                                 (ItemDetailsFactory.GetBuildcodeOption(part) & station.SeatOptions2Check) &&
                                 (ItemDetailsFactory.GetBuildcodeOption(part) & station.SeatOptions) == (station.SeatOptions & station.SeatOptions2Check) &&
                                 part.LineId == station.Line.Id)).OrderBy(part => part.PartIndex).ToList();

            List<MessageToken> validLineTokens = lineTokens.Where(t => tokens.Contains(t.Token.Trim())).ToList();
            var colourExtensions = MessageTokensCacheManager.ColourExtensions;
            var trimExtensions = MessageTokensCacheManager.TrimExtensions;

            if (filteredParts.Count() > 0)
            {
                ItemInfo part = null;

                if (station.Line.Id == 1 || station.Line.Id == 2)
                    part = filteredParts.Where(p => p.PartIndex == 1).FirstOrDefault();
                else if (station.Line.Id == 3)
                    part = filteredParts.Where(p => p.PartIndex == 3).FirstOrDefault();

                if (part != null)
                {
                    //3Pass ONLY
                    if (part.ItemComponent == "NO-2LH" || part.ItemComponent == "NO-2RH")
                        return String.Format("NO BUILD ({0})", itemNumber);
                    
                    //iterate through and look or feneric options number replacement
                    foreach (var specialOption in tokens)
                    {
                        if (specialOption.Contains("OPTION"))
                        {
                            var optionNumberString = new String(specialOption.Where(Char.IsDigit).ToArray());
                            formattedMsg = formattedMsg.Replace(specialOption.Trim(), ItemDetailsFactory.GetOptionValue(part, int.Parse(optionNumberString)));
                        }
                    }

                    foreach (var token in validLineTokens)
                    {
                        var _trimstyle = ItemDetailsFactory.GetOptionValue(part, 100);
                        var _colour = ItemDetailsFactory.GetOptionValue(part, 101);
                        var _isRU = ItemDetailsFactory.IsRU(part.ItemType);

                        switch (token.SpecialValue.Trim())
                        {
                            case "8PASS":
                                #region 8PASS
                                if (_isRU)
                                {
                                    var _8pass = ItemDetailsFactory.GetOptionValue(part, 115);
                                    if (_8pass.Equals("8PASS"))
                                        formattedMsg = formattedMsg.Replace(token.Token.Trim(), token.MessageTrue.Trim());
                                }
                                formattedMsg = formattedMsg.Replace(token.Token.Trim(), token.MessageFalse.Trim());
                                break;
                                #endregion
                            case "7PASS":
                                #region 7PASS
                                if (_isRU)
                                {
                                    var _8pass = ItemDetailsFactory.GetOptionValue(part, 115);
                                    if (!_8pass.Equals("8PASS"))
                                        formattedMsg = formattedMsg.Replace(token.Token.Trim(), token.MessageTrue.Trim());
                                }
                                formattedMsg = formattedMsg.Replace(token.Token.Trim(), token.MessageFalse.Trim());
                                break;
                                #endregion
                            case "RU":
                                #region RU
                                if (_isRU)
                                    formattedMsg = formattedMsg.Replace(token.Token.Trim(), token.MessageTrue.Trim());
                                formattedMsg = formattedMsg.Replace(token.Token.Trim(), token.MessageFalse.Trim());
                                break;
                            #endregion
                            case "3PMANSHIELD":
                                #region 3PMANSHIELD
                                if (_isRU)
                                {
                                    //TODO:
                                    formattedMsg = formattedMsg.Replace(token.Token.Trim(), token.MessageFalse.Trim());
                                }
                                else
                                {
                                    if (_colour.Equals("R4") || _colour.Equals("X9") || ItemDetailsFactory.GetOptionValue(part, 3) == "PWR")
                                        formattedMsg = formattedMsg.Replace(token.Token.Trim(), token.MessageTrue.Trim());
                                    formattedMsg = formattedMsg.Replace(token.Token.Trim(), token.MessageFalse.Trim());
                                }
                                break;
                            #endregion
                            case "3PPWRSHIELD":
                                #region 3PPWRSHIELD
                                var _pwr = "";
                                if (_isRU)
                                    _pwr = ItemDetailsFactory.GetOptionValue(part, 103);
                                else
                                    _pwr = ItemDetailsFactory.GetOptionValue(part, 3);

                                if (_pwr == "PWR")
                                    formattedMsg = formattedMsg.Replace(token.Token.Trim(), token.MessageFalse.Trim());
                                formattedMsg = formattedMsg.Replace(token.Token.Trim(), token.MessageTrue.Trim());

                                break;
                            #endregion
                            case "3PMAN!RQ":
                                #region 3PMAN!RQ
                                var _pwr2 = "";
                                if (_isRU)
                                    _pwr2 = ItemDetailsFactory.GetOptionValue(part, 103);
                                else
                                    _pwr2 = ItemDetailsFactory.GetOptionValue(part, 3);

                                if (_pwr2 != "PWR")
                                    formattedMsg = formattedMsg.Replace(token.Token.Trim(), token.MessageTrue.Trim());
                                formattedMsg = formattedMsg.Replace(token.Token.Trim(), token.MessageFalse.Trim());
                                break;
                            #endregion
                            case "3PX9MAN!RQ":
                                #region 3PX9MAN!RQ
                                if (_isRU)
                                {
                                    //TODO:
                                    //formattedMsg = _trimstyle + " " + _colour;
                                }
                                else
                                {
                                    if (_colour.Equals("R4") || _colour.Equals("X9"))
                                    {
                                        if (ItemDetailsFactory.GetOptionValue(part, 3) != "PWR")
                                            formattedMsg = formattedMsg.Replace(token.Token.Trim(), token.MessageTrue.Trim());
                                    }
                                }
                                formattedMsg = formattedMsg.Replace(token.Token.Trim(), token.MessageFalse.Trim());
                                break;
                            #endregion
                            case "RMVQD!RQ":
                                #region RMVQD!RQ
                                if (_isRU)
                                {
                                    formattedMsg = formattedMsg.Replace(token.Token.Trim(), token.MessageFalse.Trim());
                                }
                                else
                                {
                                    if (ItemDetailsFactory.GetOptionValue(part, 11) == "LXQD")
                                        formattedMsg = formattedMsg.Replace(token.Token.Trim(), token.MessageTrue.Trim());
                                    formattedMsg = formattedMsg.Replace(token.Token.Trim(), token.MessageFalse.Trim());
                                }
                                break;
                            #endregion
                            case "PWRCARPET":
                                #region PWRCARPET
                                if (_isRU)
                                {
                                    //TODO:
                                    //formattedMsg = _trimstyle + " " + _colour;
                                }
                                else
                                {
                                    if (ItemDetailsFactory.GetOptionValue(part, 3) == "PWR")
                                    {
                                        if (_colour.Equals("D1"))
                                            formattedMsg = formattedMsg.Replace(token.Token.Trim(), "(X9 Carpet)");
                                        else if (_colour.Equals("X9"))
                                            formattedMsg = formattedMsg.Replace(token.Token.Trim(), "(X9 Carpet)");
                                        else if (_colour.Equals("L9"))
                                            formattedMsg = formattedMsg.Replace(token.Token.Trim(), "(L5 Carpet)");
                                        else if (_colour.Equals("L5"))
                                            formattedMsg = formattedMsg.Replace(token.Token.Trim(), "(L5 Carpet)");
                                        else if (_colour.Equals("TU"))
                                            formattedMsg = formattedMsg.Replace(token.Token.Trim(), "(X9 Carpet)");
                                    }
                                    else
                                    {
                                        formattedMsg = formattedMsg.Replace(token.Token.Trim(), token.MessageTrue.Trim());
                                    }
                                }
                                formattedMsg = formattedMsg.Replace(token.Token.Trim(), token.MessageFalse.Trim());
                                break;
                            #endregion
                            case "POWERBOLD":
                                #region POWERBOLD
                                if (_isRU)
                                {
                                    //TODO:
                                    //formattedMsg = _trimstyle + " " + _colour;
                                }
                                else
                                {
                                    if (ItemDetailsFactory.GetOptionValue(part, 3) == "PWR")
                                        formattedMsg = formattedMsg.Replace(token.Token.Trim(), token.MessageTrue.Trim());
                                    formattedMsg = formattedMsg.Replace(token.Token.Trim(), token.MessageFalse.Trim());
                                }
                                break;
                            #endregion
                            case "SIDESHIELDCLR":
                                #region SIDESHIELD

                                var _shldclr = _colour;
                                if (colourExtensions != null)
                                {
                                    var _ext =
                                        colourExtensions.Where(
                                            cExt =>
                                            cExt.ColourCode == _shldclr && cExt.ExtensionName == "Side Shield").ToList();

                                    if (_ext.Count() > 0)
                                        _shldclr = _ext[0].ColourCodeExt.Trim();
                                }
                                formattedMsg = formattedMsg.Replace(token.Token.Trim(), _shldclr);

                                break;
                            #endregion
                            case "TRIMCLR":
                                #region TRIMCLR
                                if (_isRU)
                                {
                                    //TODO:
                                    //formattedMsg = _trimstyle + " " + _colour;
                                }
                                else
                                {
                                    var _trimclr = _colour;
                                    if (colourExtensions != null)
                                    {
                                        var _ext =
                                            colourExtensions.Where(
                                                cExt =>
                                                cExt.ColourCode == _trimclr && cExt.ExtensionName == "Trim").ToList();

                                        if (_ext.Count() > 0)
                                            _trimclr = _ext[0].ColourCodeExt.Trim();
                                    }
                                    formattedMsg = formattedMsg.Replace(token.Token.Trim(), _trimclr);
                                }
                                break;
                            #endregion
                            case "FRTSHIELD":
                                #region FRTSHIELD
                                if (_isRU)
                                {
                                    //TODO:
                                    //formattedMsg = _trimstyle + " " + _colour;
                                }
                                else
                                {
                                    if (_colour.Equals("R4") || _colour.Equals("X9"))
                                        formattedMsg = formattedMsg.Replace(token.Token.Trim(), token.MessageTrue.Trim());
                                    formattedMsg = formattedMsg.Replace(token.Token.Trim(), token.MessageFalse.Trim());
                                }
                                break;
                            #endregion
                            case "FRTASHIELD":
                                #region FRTASHIELD

                                if (_colour.Equals("X9") || _colour.Equals("R4"))
                                    formattedMsg = formattedMsg.Replace(token.Token.Trim(), token.MessageTrue);
                                else
                                {
                                    var _drvpart = filteredParts.Where(p => p.PartIndex == 1).FirstOrDefault();
                                    var _paspart = filteredParts.Where(p => p.PartIndex == 2).FirstOrDefault();

                                    string partstring1 = string.Empty;
                                    string partstring2 = string.Empty;

                                    if (ItemDetailsFactory.GetOptionValue(_drvpart, 1) == "DRV")
                                    {
                                        if (ItemDetailsFactory.GetOptionValue(_drvpart, 3) == "PWR")
                                            partstring1 = "580298-" + _colour + " PWR (DRV)";
                                        else
                                            partstring1 = "580278-" + _colour + " MAN (DRV)";
                                    }

                                    if (ItemDetailsFactory.GetOptionValue(_paspart, 1) == "PASS")
                                    {
                                        if (ItemDetailsFactory.GetOptionValue(_paspart, 3) == "PWR")
                                            partstring2 = "580288-" + _colour + " PWR (PASS)";
                                        else
                                            partstring2 = "580268-" + _colour + " MAN (PASS)";
                                    }

                                    formattedMsg = formattedMsg.Replace(token.Token.Trim(), partstring1 + "," + partstring2);
                                }

                                break;
                            #endregion
                            case "FRTCARPET":
                                #region FRTCARPET
                                if (_colour.Equals("D1"))
                                    formattedMsg = formattedMsg.Replace(token.Token.Trim(), "550608N-D1,550608N-D1");
                                else if (_colour.Equals("L5"))
                                    formattedMsg = formattedMsg.Replace(token.Token.Trim(), "550608N-L5,550608N-L5");
                                else if (_colour.Equals("L9"))
                                    formattedMsg = formattedMsg.Replace(token.Token.Trim(), "550608N-L9,550608N-L9");
                                else if (_colour.Equals("TU"))
                                    formattedMsg = formattedMsg.Replace(token.Token.Trim(), "550608N-TU,550608N-TU");
                                else
                                    formattedMsg = formattedMsg.Replace(token.Token.Trim(), token.MessageTrue);

                                break;
                            #endregion
                            case "ONLYLXQD":
                                #region ONLYLXQD
                                var _lxqd = "";
                                if (_isRU)
                                    _lxqd = ItemDetailsFactory.GetOptionValue(part, 103);
                                else
                                    _lxqd = ItemDetailsFactory.GetOptionValue(part, 11);


                                if (_lxqd.ToUpper() == "LXQD")
                                    formattedMsg = formattedMsg.Replace(token.Token.Trim(), token.MessageFalse.Trim());
                                formattedMsg = formattedMsg.Replace(token.Token.Trim(), token.MessageTrue.Trim());

                                break;
                            #endregion
                            case "BENCH":
                                #region BENCH
                                if (part.ItemType == "2B")
                                {
                                    if (_colour.Equals("D1") || _colour.Equals("L5") || _colour.Equals("TU"))
                                        formattedMsg = formattedMsg.Replace(token.Token.Trim(), token.MessageFalse.Trim());
                                }
                                formattedMsg = formattedMsg.Replace(token.Token.Trim(), token.MessageTrue.Trim());
                                break;
                            #endregion
                            case "TBENCH!RQ":
                                #region TBENCH!RQ

                                if (part.ItemType == "2B")
                                    formattedMsg = formattedMsg.Replace(token.Token.Trim(), token.MessageTrue.Trim());
                                formattedMsg = formattedMsg.Replace(token.Token.Trim(), token.MessageFalse.Trim());

                                break;
                            #endregion
                            case "TNOBUILD!RQ":
                                #region
                                if (_isRU)
                                {
                                    //TODO:
                                    //formattedMsg = _trimstyle + " " + _colour;
                                }
                                else
                                {
                                    if (part.ItemComponent.Trim() == "NO-2LH" || part.ItemComponent.Trim() == "NO-2RH")
                                        formattedMsg = formattedMsg.Replace(token.Token.Trim(), token.MessageTrue.Trim());
                                    formattedMsg = formattedMsg.Replace(token.Token.Trim(), token.MessageFalse.Trim());
                                }
                                break;
                            #endregion
                            case "CLOTH!RQ":
                                #region CLOTH!RQ
                                var cloth = "";
                                if (_isRU)
                                    cloth = ItemDetailsFactory.GetOptionValue(part, 108);
                                else
                                    cloth = ItemDetailsFactory.GetOptionValue(part, 5);

                                if (cloth.ToUpper() == "CLOTH")
                                {
                                    if (ItemDetailsFactory.GetOptionValue(part, 10).ToUpper() != "HT")
                                        formattedMsg = formattedMsg.Replace(token.Token.Trim(), token.MessageTrue.Trim());
                                }
                                formattedMsg = formattedMsg.Replace(token.Token.Trim(), token.MessageFalse.Trim());

                                break;
                            #endregion
                            case "H7N7X9!RQ":
                                #region H7N7X9!RQ
                                if (_isRU)
                                {
                                    //TODO:
                                }
                                else
                                {
                                    if (_trimstyle.Equals("H7") || _trimstyle.Equals("N7"))
                                    {
                                        if (_colour.Equals("X9") && ItemDetailsFactory.GetOptionValue(part, 9) != "BUX")
                                            formattedMsg = formattedMsg.Replace(token.Token.Trim(), token.MessageTrue.Trim());
                                    }
                                }
                                formattedMsg = formattedMsg.Replace(token.Token.Trim(), token.MessageFalse.Trim());
                                break;
                            #endregion
                            case "HDRCOLOUR":
                                #region HDRCOLOUR
                                var _hdrcolour = _colour;
                                if (colourExtensions != null)
                                {
                                    var _ext = colourExtensions.Where(cExt => cExt.ColourCode == _hdrcolour && cExt.ExtensionName == "Headrest").ToList();

                                    if (_ext.Count() > 0)
                                        _hdrcolour = _ext[0].ColourCodeExt.Trim();
                                }
                                formattedMsg = formattedMsg.Replace(token.Token.Trim(), _hdrcolour);
                                break;
                            #endregion
                            case "HDRTRIM":
                                #region HDRTRIM
                                var _hdrtrim = _trimstyle;
                                if (trimExtensions != null)
                                {
                                    var _ext = trimExtensions.Where(tExt => tExt.TrimCode == _hdrtrim && tExt.ExtensionName == "Headrest").ToList();
                                    if (_ext.Count() > 0)
                                        _hdrtrim = _ext[0].TrimCodeExt.Trim();
                                }
                                formattedMsg = formattedMsg.Replace(token.Token.Trim(), _hdrtrim);
                                break;
                            #endregion
                            case "X9R4!RQ":
                                #region X9R4!RQ
                                if (_isRU)
                                {
                                    var sng = ItemDetailsFactory.GetOptionValue(part, 114);
                                    if (sng == "SNG")
                                    {
                                        if (_colour.Equals("X9") || _colour.Equals("R4"))
                                            formattedMsg = formattedMsg.Replace(token.Token.Trim(), token.MessageTrue);
                                    }
                                    formattedMsg = formattedMsg.Replace(token.Token.Trim(), token.MessageFalse.Trim());
                                }
                                else
                                {
                                    var sng = ItemDetailsFactory.GetOptionValue(part, 13);
                                    if (sng == "SNG")
                                    {
                                        if (_colour.Equals("X9") || _colour.Equals("R4"))
                                            formattedMsg = formattedMsg.Replace(token.Token.Trim(), token.MessageTrue);
                                    }
                                    formattedMsg = formattedMsg.Replace(token.Token.Trim(), token.MessageFalse.Trim());
                                }
                                break;
                            #endregion
                            case "BENCH!RQ":
                                #region BENCH!RQ
                                if (part.ItemType == "2B")
                                    formattedMsg = formattedMsg.Replace(token.Token.Trim(), token.MessageTrue.Trim());
                                formattedMsg = formattedMsg.Replace(token.Token.Trim(), token.MessageFalse.Trim());
                                break;
                            #endregion
                            case "SKU":
                                #region SKU
                                formattedMsg = formattedMsg.Replace(token.Token.Trim(), item.ItemNumber);
                                break;
                            #endregion
                            case "X9ARM!RQ":
                                #region X9ARM_NOTREQ
                                if (_isRU)
                                {
                                    //TODO:
                                    //formattedMsg = _trimstyle + " " + _colour;
                                }
                                else
                                {
                                    //option 101 - colour
                                    //option 100 - trim style
                                    if ((ItemDetailsFactory.GetOptionValue(part, 101) == "X9" && ItemDetailsFactory.GetOptionValue(part, 100) == "H7") || (ItemDetailsFactory.GetOptionValue(part, 101) == "X9" && ItemDetailsFactory.GetOptionValue(part, 100) == "AL") ||
                                        (ItemDetailsFactory.GetOptionValue(part, 101) == "X9" && ItemDetailsFactory.GetOptionValue(part, 100) == "ML") || (ItemDetailsFactory.GetOptionValue(part, 101) == "X9" && ItemDetailsFactory.GetOptionValue(part, 100) == "TL") ||
                                        (ItemDetailsFactory.GetOptionValue(part, 101) == "X9" && ItemDetailsFactory.GetOptionValue(part, 100) == "JL"))
                                    {
                                        //skip
                                        formattedMsg = formattedMsg.Replace(token.Token.Trim(), token.MessageTrue.Trim());
                                    }
                                    else
                                    {
                                        if ((ItemDetailsFactory.GetOptionValue(part, 101) == "X9" && ItemDetailsFactory.GetOptionValue(part, 100) == "N7") || (ItemDetailsFactory.GetOptionValue(part, 101) == "X9" && ItemDetailsFactory.GetOptionValue(part, 100) == "ZL") ||
                                            (ItemDetailsFactory.GetOptionValue(part, 101) == "X9" && ItemDetailsFactory.GetOptionValue(part, 100) == "CL"))
                                        {
                                            formattedMsg = formattedMsg.Replace(token.Token.Trim(), "(92)");
                                        }
                                    }
                                }
                                formattedMsg = formattedMsg.Replace(token.Token.Trim(), token.MessageFalse.Trim());
                                break;
                            #endregion
                            case "SHLDCLR":
                                #region SHLDCLR
                                if (_isRU)
                                {
                                    //TODO:
                                    //formattedMsg = _trimstyle + " " + _colour;
                                }
                                else
                                {
                                    var _clrshield = _colour;
                                    if (colourExtensions != null)
                                    {
                                        var _ext =
                                            colourExtensions.Where(
                                                cExt =>
                                                cExt.ColourCode == _clrshield && cExt.ExtensionName == "Side Shield").ToList();

                                        if (_ext.Count() > 0)
                                            _clrshield = _ext[0].ColourCodeExt.Trim();
                                    }
                                    formattedMsg = formattedMsg.Replace(token.Token.Trim(), _clrshield);
                                }
                                break;
                            #endregion
                            case "RHD":
                                #region RHD
                                if (_isRU)
                                {
                                    //TODO:
                                    //formattedMsg = _trimstyle + " " + _colour;
                                }
                                else
                                {
                                    //option 2 - lhd/rhd
                                    var _rhd = (ItemDetailsFactory.GetOptionValue(part, 2) == "RHD")
                                                   ? "RHD"
                                                   : String.Empty;
                                    formattedMsg = formattedMsg.Replace(token.Token.Trim(), _rhd);
                                }
                                break;
                            #endregion
                            case "LHPWR":
                                #region LHPWR

                                var _lhpart = item.ItemInfos.Where(i => i.PartIndex == 1).FirstOrDefault();
                                var _rhpart = item.ItemInfos.Where(i => i.PartIndex == 2).FirstOrDefault();

                                if (_isRU)
                                {
                                    var finalStr = string.Empty;
                                    var isLhPwr = (_lhpart.Buildcode & 4) == 4;
                                    var isRhPwr = (_rhpart.Buildcode & 4) == 4;

                                    if (isLhPwr && isRhPwr)
                                        finalStr = "PWR/PWR";
                                    else if (isLhPwr && !isRhPwr)
                                        finalStr = "PWR/MAN %BLD%";
                                    else if (!isLhPwr && !isRhPwr)
                                        finalStr = "MAN/MAN %BLD%";

                                    formattedMsg = formattedMsg.Replace(token.Token.Trim(), finalStr);
                                }
                                else {
                                    if (_lhpart != null)
                                    {
                                        formattedMsg = formattedMsg.Replace(token.Token.Trim(), ((_lhpart.Buildcode & 4) == 4)
                                                                            ? token.MessageTrue
                                                                            : token.MessageFalse);
                                    }
                                }
                                break;
                            #endregion
                            case "RHPWR":
                                #region RHPWR
                                if (_isRU)
                                {
                                    formattedMsg = formattedMsg.Replace(token.Token.Trim(), string.Empty);
                                }
                                else
                                {
                                    var _rhpartA = item.ItemInfos.Where(i => i.PartIndex == 2).FirstOrDefault();
                                    if (_rhpartA != null)
                                    {
                                        formattedMsg = formattedMsg.Replace(token.Token.Trim(), ((_rhpartA.Buildcode & 4) == 4)
                                                                            ? token.MessageTrue
                                                                            : token.MessageFalse);
                                    }
                                }
                                formattedMsg = formattedMsg.Replace(token.Token.Trim(), token.MessageFalse.Trim());
                                break;
                            #endregion
                            case "BCKPNL":
                                #region BCKPNL
                                if (_isRU)
                                {
                                    if (!_trimstyle.Equals("H7"))
                                    {
                                        var _frtRUBackPanel = ItemDetailsFactory.GetOptionValue(part, 102);
                                        formattedMsg = formattedMsg.Replace(token.Token.Trim(), _frtRUBackPanel);
                                    }
                                    formattedMsg = formattedMsg.Replace(token.Token.Trim(), "*Skipped*");
                                }
                                else
                                {
                                    //option 330 back panel part number
                                    //option 101 - colour
                                    var _lhback = "N/A";
                                    var _rhback = "N/A";

                                    if (item.ItemInfos.Where(prt => prt.PartIndex == 1).Count() > 0)
                                        _lhback = ItemDetailsFactory.GetOptionValue(item.ItemInfos.Where(prt => prt.PartIndex == 1).First(), 102);

                                    if (item.ItemInfos.Where(prt => prt.PartIndex == 2).Count() > 0)
                                        _rhback = ItemDetailsFactory.GetOptionValue(item.ItemInfos.Where(prt => prt.PartIndex == 2).First(), 102);

                                    var _back = String.Format("{0}/{1}", _lhback.Trim(), _rhback.Trim());
                                    if (_back.ToUpper().Equals("MESH/MESH") && ItemDetailsFactory.GetOptionValue(part, 101) == "X9")
                                    {
                                        //???
                                        _back += " %LGHT%";
                                    }
                                    formattedMsg = formattedMsg.Replace(token.Token.Trim(), _back);
                                }
                                break;
                            #endregion
                            case "X9PWR!RQ":
                                #region %9PWR_NOTREQ
                                if (_isRU)
                                {
                                    //TODO:
                                    //formattedMsg = _trimstyle + " " + _colour;
                                }
                                else
                                {
                                    //option 101 - colour
                                    if (ItemDetailsFactory.GetOptionValue(part, 101) == "X9" || ItemDetailsFactory.GetOptionValue(part, 101) == "R4")
                                    {
                                        formattedMsg = formattedMsg.Replace(token.Token.Trim(), (((part.Buildcode & token.SeatOptions) ==
                                              (token.SeatOptions & token.SeatOptions2Check)) &&
                                             ((part.Buildcode & token.SeatOptions) ==
                                              (part.Buildcode & token.SeatOptions2Check)))
                                                ? token.MessageTrue.Trim()
                                                : token.MessageFalse.Trim());
                                    }
                                }
                                formattedMsg = formattedMsg.Replace(token.Token.Trim(), token.MessageFalse.Trim());
                                break;
                            #endregion
                            case "X9PASSMAN!RQ":
                                #region X9PASSMAN!RQ
                                if (_isRU)
                                {
                                    //TODO:
                                    //formattedMsg = _trimstyle + " " + _colour;
                                }
                                else
                                {
                                    var _rhpartpassnotreq = item.ItemInfos.Where(i => i.PartIndex == 2).FirstOrDefault();
                                    if (ItemDetailsFactory.GetOptionValue(_rhpartpassnotreq, 101) == "X9")
                                    {
                                        if (ItemDetailsFactory.GetOptionValue(_rhpartpassnotreq, 104) == "MAN")
                                            formattedMsg = formattedMsg.Replace(token.Token.Trim(), token.MessageTrue.Trim());
                                    }
                                }
                                formattedMsg = formattedMsg.Replace(token.Token.Trim(), token.MessageFalse.Trim());
                                break;
                            #endregion
                            case "X9MAN!RQ":
                                #region X9MAN_NOTREQ
                                if (_isRU)
                                {
                                    //TODO:
                                    //formattedMsg = _trimstyle + " " + _colour;
                                }
                                else
                                {
                                    //option 101 - colour
                                    if (ItemDetailsFactory.GetOptionValue(part, 101) == "X9")
                                    {
                                        if (ItemDetailsFactory.GetOptionValue(part, 3) == "MAN")
                                            formattedMsg = formattedMsg.Replace(token.Token.Trim(), token.MessageTrue.Trim());

                                    }
                                }
                                formattedMsg = formattedMsg.Replace(token.Token.Trim(), token.MessageFalse.Trim());
                                break;
                            #endregion
                            case "X9H7RT!RQ":
                                #region X9H7RT_NOTREQ
                                if (_isRU)
                                {
                                    //TODO:
                                    //formattedMsg = _trimstyle + " " + _colour;
                                }
                                else
                                {
                                    //option 101 - colour
                                    //option 100 - trim style
                                    //option 4 - program
                                    //option 9 - BUX
                                    if (ItemDetailsFactory.GetOptionValue(part, 101) == "X9" && ItemDetailsFactory.GetOptionValue(part, 100) == "H7"
                                        && ItemDetailsFactory.GetOptionValue(part, 4) == "RT" && ItemDetailsFactory.GetOptionValue(part, 9) == "")
                                    {
                                        formattedMsg = formattedMsg.Replace(token.Token.Trim(), token.MessageTrue.Trim());
                                    }
                                    else if (ItemDetailsFactory.GetOptionValue(part, 101) == "X9" && ItemDetailsFactory.GetOptionValue(part, 100) == "N7"
                                        && ItemDetailsFactory.GetOptionValue(part, 4) == "RT" && ItemDetailsFactory.GetOptionValue(part, 9) == "")
                                    {
                                        formattedMsg = formattedMsg.Replace(token.Token.Trim(), token.MessageTrue.Trim());
                                    }
                                    else
                                    {
                                        formattedMsg = formattedMsg.Replace(token.Token.Trim(), token.MessageFalse.Trim());
                                    }
                                }
                                break;
                            #endregion
                            case "DRVWIREH":
                                #region DRVWIREH
                                if (_isRU)
                                {
                                    //TODO:
                                    var _wire = ItemDetailsFactory.GetOptionValue(part, 225);
                                    formattedMsg = formattedMsg.Replace(token.Token.Trim(), _wire);
                                }
                                else
                                {
                                    var drvpart = filteredParts.Where(p => p.PartIndex == 1).FirstOrDefault();
                                    if (drvpart != null)
                                    {
                                        var wire = ItemDetailsFactory.GetOptionValue(drvpart, 225);
                                        formattedMsg = formattedMsg.Replace(token.Token.Trim(), wire);
                                        //Adding MEM to 793 and 973 AK


                                        //  if (formattedMsg.Equals(793.ToString()) || formattedMsg.Equals(973.ToString()))  // || formattedMsg.Equals(313.ToString())
                                        //     formattedMsg = formattedMsg + "MEM";
                                    }
                                    else
                                    {
                                        formattedMsg = formattedMsg.Replace(token.Token.Trim(), token.MessageFalse.Trim());
                                    }
                                }
                                break;
                            #endregion
                            case "PASWIREH":
                                #region PASWIREH
                                if (_isRU)
                                {
                                    //TODO:
                                    var _wire = ItemDetailsFactory.GetOptionValue(part, 226);
                                    formattedMsg = formattedMsg.Replace(token.Token.Trim(), _wire);
                                }
                                else
                                {
                                    var paspart = filteredParts.Where(p => p.PartIndex == 2).FirstOrDefault();
                                    if (paspart != null)
                                    {
                                        var wire = ItemDetailsFactory.GetOptionValue(paspart, 225);
                                        formattedMsg = formattedMsg.Replace(token.Token.Trim(), wire);
                                    }
                                    else
                                    {
                                        formattedMsg = formattedMsg.Replace(token.Token.Trim(), token.MessageFalse.Trim());
                                    }
                                }
                                break;
                            #endregion
                            case "RTBUCKLES":
                                #region RTBUCKLES
                                if (_isRU)
                                {
                                    //TODO:
                                    //formattedMsg = _trimstyle + " " + _colour;
                                }
                                else
                                {
                                    var rpart = filteredParts.Where(p => p.PartIndex == 3).FirstOrDefault();
                                    if (rpart != null)
                                    {
                                        var pas = ItemDetailsFactory.GetOptionValue(rpart, 101);
                                        formattedMsg = formattedMsg.Replace(token.Token.Trim(), pas);
                                    }
                                    else
                                        formattedMsg = formattedMsg.Replace(token.Token.Trim(), token.MessageFalse.Trim());
                                }
                                break;
                            #endregion
                            case "SEATBELT":
                                #region SEATBELT
                                if (_isRU)  //OP1100N
                                {
                                    formattedMsg = formattedMsg.Replace(token.Token.Trim(), "*Skipped*");
                                }
                                else
                                {
                                    var spart = filteredParts.Where(p => p.PartIndex == 1).FirstOrDefault();
                                    if (spart != null)
                                    {
                                        var pas2 = ItemDetailsFactory.GetOptionValue(spart, 420);
                                        var pas = "";
                                        if (pas2 != "")
                                        {
                                            pas = pas2.Substring(pas2.Length - 2) + "-" + pas2.Substring(0, 2);
                                            if (spart.ItemDescription.Contains("MAN"))
                                                pas = pas + "-<span style=\"color: blue;\">MAN</span>";
                                            else if (spart.ItemDescription.Contains("PWR"))
                                                pas = pas + "-<span style=\"color: #FF6600;\">PWR</span>";
                                        }
                                        formattedMsg = pas; // formattedMsg.Replace(token.Token.Trim(), pas);
                                    }
                                    else
                                        formattedMsg = formattedMsg.Replace(token.Token.Trim(), token.MessageFalse.Trim());
                                }
                                break;
                            #endregion
                            case "PASSEATBELT":
                                #region PASSEATBELT
                                if (_isRU)
                                {
                                    var ruSeatBeltPartno = ItemDetailsFactory.GetOptionValue(part, 270);
                                    var msg = ruSeatBeltPartno;
                                    if (ruSeatBeltPartno.Length > 4)
                                        msg = ruSeatBeltPartno.Substring(ruSeatBeltPartno.Length - 4) + " (2)";

                                    formattedMsg = formattedMsg.Replace(token.Token.Trim(), msg);
                                }
                                else
                                {
                                    var seatpart = filteredParts.Where(p => p.PartIndex == 2).FirstOrDefault();
                                    if (seatpart != null)
                                    {
                                        var pas2 = ItemDetailsFactory.GetOptionValue(seatpart, 420);
                                        var pas = "";
                                        if (pas2 != "")
                                        {
                                            pas = pas2.Substring(pas2.Length - 2) + "-" + pas2.Substring(0, 2);
                                            if (seatpart.ItemDescription.Contains("MAN"))
                                                pas = pas + "-<span style=\"color: blue;\">MAN</span>";
                                            else if (seatpart.ItemDescription.Contains("PWR"))
                                                pas = pas + "-<span style=\"color: #FF6600;\">PWR</span>";
                                        }
                                        formattedMsg = pas; // formattedMsg.Replace(token.Token.Trim(), pas);
                                    }
                                    else
                                    {
                                        formattedMsg = formattedMsg.Replace(token.Token.Trim(), token.MessageFalse.Trim());
                                    }
                                }
                                break;
                            #endregion

                            default:
                                #region DEFAULT
                                formattedMsg = formattedMsg.Replace(token.Token.Trim(), (((part.Buildcode & token.SeatOptions) ==
                                                                  (token.SeatOptions & token.SeatOptions2Check)) &&
                                                                 ((part.Buildcode & token.SeatOptions) ==
                                                                  (part.Buildcode & token.SeatOptions2Check)))
                                                                    ? token.MessageTrue.Trim()
                                                                   : token.MessageFalse.Trim());
                                #endregion
                                break;
                        }
                    }

                    return formattedMsg;
                }
            }

            return String.Format("*Skipped* ({0})", itemNumber);
        }
        public JobBuffer ScheduleAdminView(int id)
        {
            PartSequencingDataContext dbSeq = new PartSequencingDataContext();
            HP_MESDataContext dbMes = new HP_MESDataContext();
            ItemDetailsFactory itemDetailsModel = new ItemDetailsFactory();
            var seqMessageProcessor = new SeqMessageProcessor();

            var station = UpdateNextBoxBySequencingId(id);

            var tokens = MessageTokensCacheManager.MessageTokens.Where(t => t.LineId == station.Line.Id || t.LineId == 4).ToList();

            var lineBuffer = GetAdminSequencingBuffer(station);

            var boxBuffer = new JobBuffer
            {
                CurrentBoxNum = station.NextBox.BoxNum,
                CurrentScheduleNum = station.NextBox.ScheduleNum,
                SequencingStation = station,
                Buffer = new List<SequenceItem>()
            };

            var tracking = GetSequencingHistory(station);

            boxBuffer.Buffer = (lineBuffer.GroupJoin(tracking, jb => jb.OrderId, st => st.OrderId, (jb, g) => new { _jb = jb, g }).
            SelectMany(t => t.g.DefaultIfEmpty(), (t, st) => new SequenceItem
            {
                BoxNum = st == null ? 0 : st.BoxNum,
                OrderId = t._jb.OrderId,
                Complete = true,
                OrderNumber = t._jb.OrderNumber == null ? "" : t._jb.OrderNumber,
                ScheduleNum = st == null ? 0 : st.ScheduleNum,
                ItemNumber = t._jb.ItemNumber,
                JobId = (int)t._jb.JobId,
                Region = t._jb.Region.Trim(),
                Type = st == null ? 0 : (int)st.Type,
                Message = seqMessageProcessor.GetSequencingMessage(station, t._jb.ItemNumber, tokens),
                Item = itemDetailsModel.GetItemInfo(t._jb.ItemNumber)
            })).ToList();

            boxBuffer.Buffer = boxBuffer.Buffer.OrderBy(s => s.OrderId).ToList();

            var itemsinbox = station.PartsPerBox;
            var boxperschedule = station.BoxPerSchedule;
            var itemsintheschedule = itemsinbox * boxperschedule;
            if (station.NextBox != null)
            {

                var currentbox = station.NextBox.BoxNum;

                var jobs = boxBuffer.Buffer.Where(i => i.OrderId >= station.NextBox.OrderId).Take(itemsintheschedule).OrderBy(s => s.OrderId).ToList();

                foreach (var item in jobs)
                {
                    item.Complete = false;
                    item.BoxNum = currentbox;
                    item.ScheduleNum = station.NextBox.ScheduleNum;

                    if (item.Message.ToUpper().IndexOf("*SKIPPED*") < 0)
                    {
                        item.Skipped = false;
                        --itemsinbox;
                    }
                    else
                        item.Skipped = true;


                    if (itemsinbox > 0 && itemsintheschedule > 0) continue;
                    if (itemsintheschedule <= 1)
                    {
                        break;
                    }

                    itemsinbox = station.PartsPerBox;
                    --boxperschedule;
                    currentbox = currentbox >= station.BoxQty ? 1 : ++currentbox;

                }

            }

            var scheduleNum = -1;
            var boxNum = -1;
            var initialJobId = Int32.MinValue;

            for(var i=0;i<boxBuffer.Buffer.Count(); i++) {
                if (i == 0) {
                    initialJobId = boxBuffer.Buffer[i].JobId;
                }

                if (boxBuffer.Buffer[i].ScheduleNum != scheduleNum)
                {
                    
                    boxBuffer.Buffer[i].StartDrawSchedule = true;
                    boxBuffer.Buffer[i].InitialJobId = initialJobId;

                    var countSchedule = 0;
                    for(var j=i; j<boxBuffer.Buffer.Count();j++) {
                        if (boxBuffer.Buffer[j].ScheduleNum == boxBuffer.Buffer[i].ScheduleNum) {
                            countSchedule++;
                        } else {
                            break;
                        }
                    }

                    if (boxBuffer.Buffer[i].Complete)
                        boxBuffer.Buffer[i].ScheduleCount = countSchedule;
                    else
                    {
                        if (countSchedule > itemsintheschedule)
                            boxBuffer.Buffer[i].ScheduleCount = itemsintheschedule;
                        else
                            boxBuffer.Buffer[i].ScheduleCount = countSchedule;
                    }
                    initialJobId = boxBuffer.Buffer[i].JobId;
                    scheduleNum = boxBuffer.Buffer[i].ScheduleNum;
                }
                else
                {
                    boxBuffer.Buffer[i].StartDrawSchedule = false;

                }
            }

            scheduleNum = -1;
            for (var i = 0; i < boxBuffer.Buffer.Count(); i++)
            {
                if (boxBuffer.Buffer[i].BoxNum != boxNum || boxBuffer.Buffer[i].ScheduleNum != scheduleNum)
                {
                    boxBuffer.Buffer[i].StartDrawBox = true;

                    var countBox = 0;
                    for (var j = i; j < boxBuffer.Buffer.Count(); j++)
                    {
                        if (boxBuffer.Buffer[j].BoxNum == boxBuffer.Buffer[i].BoxNum && boxBuffer.Buffer[j].ScheduleNum == boxBuffer.Buffer[i].ScheduleNum)
                        {
                            countBox++;
                        }
                        else
                            break;
                    }
                    boxBuffer.Buffer[i].BoxCount = countBox;
                    boxNum = boxBuffer.Buffer[i].BoxNum;
                    scheduleNum = boxBuffer.Buffer[i].ScheduleNum;
                }
                else
                {
                    boxBuffer.Buffer[i].StartDrawBox = false;
                }
            }
            
            return boxBuffer;

        }