public void LiveMonitorItemsSetting()
        {
            LiveMonitorViewModel lmvm = new LiveMonitorViewModel();

            //empty page
            Assert.AreEqual(false, lmvm.rotating);

            //set screen sizes from fake event
            Grid FakeGrid = new Grid();

            FakeGrid.Height     = 1000;
            FakeGrid.Width      = 1500;
            FakeGrid.RenderSize = new Size(1500, 1000);
            lmvm.MainGridCreated.Execute(FakeGrid);

            Assert.AreEqual(1000, lmvm.MonitorHeight);
            Assert.AreEqual(1500, lmvm.MonitorWidth);
            Assert.AreEqual(768, lmvm.BrowserHeight);
            Assert.AreEqual(1366, lmvm.BrowserWidth);

            //set items dimentions from fake event
            Label Header = new Label();

            Header.RenderSize = new Size(10, 150);
            lmvm.ItemCreated.Execute(Header);

            Assert.AreEqual(150, lmvm.HeaderItemHeight);
            Assert.AreEqual(0, lmvm.ItemHeight);

            Label NoHeader = new Label();

            NoHeader.RenderSize = new Size(10, 100);
            lmvm.ItemCreated.Execute(NoHeader);

            Assert.AreEqual(150, lmvm.HeaderItemHeight);
            Assert.AreEqual(100, lmvm.ItemHeight);
        }
        public void Rotate()
        {
            if (ChangeTracker.LiveMonitors.Count <= 0)
            {
                return;
            }

            if (this.rotationCounter >= 1000)
            {
                rotationCounter = 0;
            }

            this.rotationCounter++;

            LiveMonitorViewModel lmvm = null;

            SyncList <IMatchVw> lMatches = Matches.ToSyncList();

            for (int i = 0; i < LiveMonitorsCollectionList.Count; i++)
            {
                if (!lmViewModels[i].rotating || lmViewModels[i].LiveOddsVisibility == Visibility.Collapsed)
                {
                    continue;
                }

                //check interval
                if (this.rotationCounter % lmViewModels[i].rotationInterval != 0 && this.rotationCounter != 0)
                {
                    continue;
                }

                lmvm = ChangeTracker.LiveMonitors[i].DataContext as LiveMonitorViewModel;
                Debug.Assert(lmvm != null);

                double dblItemHeight       = lmvm != null && lmvm.ItemHeight > 0.0d ? lmvm.ItemHeight : ITEM_DEFAULT_HEIGHT;
                double dblHeaderItemHeight = lmvm != null && lmvm.HeaderItemHeight > 0.0d ? lmvm.HeaderItemHeight : ITEM_WITH_HEADER_DEFAULT_HEIGHT;

                if (LiveMonitorRotationCounters[i] >= lMatches.Count)
                {
                    LiveMonitorRotationCounters[i] = LiveMonitorRotationCounters[i] - lMatches.Count;
                }

                int iMatchIdx = LiveMonitorRotationCounters[i];

                if (iMatchIdx >= lMatches.Count)
                {
                    iMatchIdx = 0;
                }

                IMatchVw matchVw = lMatches.Count > iMatchIdx ? lMatches[iMatchIdx] : null;



                SyncObservableCollection <IMatchVw> socMonitor = LiveMonitorsCollectionList[i];
                double dblCurrentMonitorFilledHeight           = 0;

                SyncList <IMatchVw> lMatchesTemp = new SyncList <IMatchVw>();

                string oldSport     = "";
                bool   isPreLiveOld = false;

                while (matchVw != null)
                {
                    if (matchVw == null || lMatchesTemp.Contains(matchVw))
                    {
                        break;
                    }

                    string currentSport = matchVw.SportDescriptor;
                    var    isPreLive    = matchVw.LiveBetStatus == eMatchStatus.NotStarted || !matchVw.IsLiveBet;
                    if (!string.IsNullOrEmpty(currentSport))
                    {
                        if (currentSport != oldSport)
                        {
                            matchVw.IsHeaderForRotation = true;
                        }
                        else if (isPreLive && !isPreLiveOld)
                        {
                            matchVw.IsHeaderForRotation = true;
                        }
                        else
                        {
                            matchVw.IsHeaderForRotation = false;
                        }
                        oldSport     = currentSport;
                        isPreLiveOld = isPreLive;
                    }

                    dblCurrentMonitorFilledHeight += matchVw.IsHeaderForRotation ? dblHeaderItemHeight : dblItemHeight;

                    if (dblCurrentMonitorFilledHeight > lmViewModels[i].LiveOddsHeight) //lmViewModels[i].LiveOddsHeight)
                    {
                        break;
                    }
                    else
                    {
                        lMatchesTemp.Add(matchVw);
                    }

                    iMatchIdx++;

                    if (iMatchIdx >= lMatches.Count)
                    {
                        iMatchIdx = 0;
                    }

                    matchVw = lMatches.Count > iMatchIdx ? lMatches[iMatchIdx] : null;

                    LiveMonitorRotationCounters[i]++;
                }

                Dispatcher.Invoke(() =>
                {
                    socMonitor.ApplyChanges(lMatchesTemp);     //damn thing does not applay normally changes for collections with duplicate matches with it...
                });
            }
        }
        private void SplitCollection()
        {
            if (ChangeTracker.LiveMonitors.Count <= 0)
            {
                return;
            }

            SyncList <IMatchVw> lMatches = Matches.ToSyncList();
            int iMatchIdx = 0;

            int page = -1;

            for (int i = 0; i < LiveMonitorsCollectionList.Count; i++)
            {
                if (lmViewModels[i].rotating || lmViewModels[i].LiveOddsVisibility == Visibility.Collapsed)
                {
                    continue;
                }

                LiveMonitorViewModel lmvm = null;
                Dispatcher.Invoke(() =>
                {
                    lmvm = ChangeTracker.LiveMonitors[i].Window.DataContext as LiveMonitorViewModel;
                });
                Debug.Assert(lmvm != null);

                double dblItemHeight       = lmvm != null && lmvm.ItemHeight > 0.0d ? lmvm.ItemHeight : ITEM_DEFAULT_HEIGHT;
                double dblHeaderItemHeight = lmvm != null && lmvm.HeaderItemHeight > 0.0d ? lmvm.HeaderItemHeight : ITEM_WITH_HEADER_DEFAULT_HEIGHT;

                Dispatcher.Invoke(() =>
                {
                    SyncObservableCollection <IMatchVw> socMonitor = LiveMonitorsCollectionList[i];
                    double dblCurrentMonitorFilledHeight           = 0;

                    SyncList <IMatchVw> lMatchesTemp = new SyncList <IMatchVw>();

                    IMatchVw matchVw    = lMatches.Count > iMatchIdx ? lMatches[iMatchIdx] : null;
                    string currentSport = "";
                    bool isPreLiveOld   = false;

                    while (matchVw != null)
                    {
                        var isPreLive = matchVw.LiveBetStatus == eMatchStatus.NotStarted || !matchVw.IsLiveBet;

                        if (matchVw.SportDescriptor != currentSport)
                        {
                            matchVw.IsHeaderForLiveMonitor = true;
                            currentSport = matchVw.SportDescriptor;
                        }
                        else if (page != i)
                        {
                            matchVw.IsHeaderForLiveMonitor = true;
                            page = i;
                        }
                        else if (isPreLive && !isPreLiveOld)
                        {
                            matchVw.IsHeaderForLiveMonitor = true;
                            isPreLiveOld = isPreLive;
                        }
                        else
                        {
                            if (dblCurrentMonitorFilledHeight + dblItemHeight <= LiveMonitorHeights[i])
                            {
                                matchVw.IsHeaderForLiveMonitor = false;
                            }
                        }

                        currentSport = matchVw.SportDescriptor;
                        page         = i;
                        isPreLiveOld = isPreLive;

                        dblCurrentMonitorFilledHeight += matchVw.IsHeaderForLiveMonitor ? dblHeaderItemHeight : dblItemHeight;

                        if (dblCurrentMonitorFilledHeight > LiveMonitorHeights[i])
                        {
                            page = i;
                            break;
                        }
                        else
                        {
                            lMatchesTemp.Add(matchVw);
                        }

                        iMatchIdx++;
                        matchVw = lMatches.Count > iMatchIdx ? lMatches[iMatchIdx] : null;
                    }
                    //Debug.Assert(ChangeTracker.LiveMonitors[i].threadId == socMonitor.threadid);

                    //Debug.Assert(Thread.CurrentThread == socMonitor.threadid);

                    socMonitor.ApplyChanges(lMatchesTemp);
                });
            }
        }
        public void SetMonitors(bool result)
        {
            lock (locker)
            {
                MonitorTemplates deserializedFeed = null;
                try
                {
                    if (!lmViewModelsDefault.Equals(lmViewModels))
                    {
                        lmViewModels = new SyncList <LiveMonitorViewModel>(lmViewModelsDefault);
                    }

                    foreach (LiveMonitorViewModel lv in lmViewModels)
                    {
                        lv.liveOddsPage = 1000;
                    }

                    string number    = StationRepository.StationNumber;
                    string templates = WsdlRepository.GetContentManagementData(StationRepository.StationNumber);
                    deserializedFeed = JsonConvert.DeserializeObject <MonitorTemplates>(templates);

                    var screens = Screen.AllScreens.Where(s => !s.Primary).ToList();
                    for (int i = 0; i < ChangeTracker.LiveMonitors.Count; i++)
                    {
                        string index = screens[i].DeviceName.Substring(screens[i].DeviceName.Length - 1, 1);
                        lmViewModels[i].LiveOddsVisibility = Visibility.Collapsed;

                        lmViewModels[i].HeaderTextVisibility = Visibility.Collapsed;
                        lmViewModels[i].BannerVisibility     = Visibility.Collapsed;
                        lmViewModels[i].rotating             = false;
                        lmViewModels[i].rotationInterval     = 10;
                        lmViewModels[i].MonitorWidth         = screens[i].WorkingArea.Width;
                        lmViewModels[i].MonitorHeight        = screens[i].WorkingArea.Height;

                        MonitorTemplate temp = deserializedFeed.monitorTemplates.Where(x => x.monitorPosition == index).FirstOrDefault();
                        if (temp == null)
                        {
                            Mediator.SendMessage(i + "|" + Visibility.Collapsed.ToString(), MsgTag.ChangeVisibility);

                            Dispatcher.Invoke(() =>
                            {
                                if (!lmViewModels[i].IsClosed)
                                {
                                    lmViewModels[i].ViewWindow.Visibility = Visibility.Visible;
                                }
                            });
                            lmViewModels[i].HeaderTextVisibility = Visibility.Collapsed;
                            lmViewModels[i].BannerVisibility     = Visibility.Collapsed;
                            lmViewModels[i].rotating             = false;
                            lmViewModels[i].rotationInterval     = 10;
                            lmViewModels[i].MonitorWidth         = screens[i].WorkingArea.Width;
                            lmViewModels[i].MonitorHeight        = screens[i].WorkingArea.Height;

                            lmViewModels[i].LiveOddsRow        = 0;
                            lmViewModels[i].LiveOddsVisibility = Visibility.Visible;
                            lmViewModels[i].LiveOddsHeight     = screens[i].WorkingArea.Height;
                            lmViewModels[i].liveOddsPage       = i + 1001;
                        }
                        else
                        {
                            string            templateId = temp.templateId;
                            AvailableTemplate template   = deserializedFeed.availableTemplates.Where(x => x.templateId == templateId).FirstOrDefault();
                            if (template != null)
                            {
                                for (int y = 0; y < template.content.Count(); y++)
                                {
                                    string type = template.content.ElementAt(y).contentType;
                                    if (type == "VFL")
                                    {
                                        Dispatcher.Invoke(() =>
                                        {
                                            if (!lmViewModels[i].IsClosed)
                                            {
                                                lmViewModels[i].ViewWindow.Visibility = Visibility.Collapsed;
                                            }
                                        });

                                        Mediator.SendMessage(i + "|" + Visibility.Visible.ToString(), MsgTag.ChangeVisibility);

                                        Mediator.SendMessage(i + "|" + SetVFLAddress(screens[i], 0), MsgTag.SetUpBrowser);
                                    }
                                    else if (type == "VHC")
                                    {
                                        Dispatcher.Invoke(() =>
                                        {
                                            if (!lmViewModels[i].IsClosed)
                                            {
                                                lmViewModels[i].ViewWindow.Visibility = Visibility.Collapsed;
                                            }
                                        });

                                        Mediator.SendMessage(i + "|" + Visibility.Visible.ToString(),
                                                             MsgTag.ChangeVisibility);
                                        Mediator.SendMessage(i + "|" + SetVHCAddress(screens[i]), MsgTag.SetUpBrowser);
                                    }
                                    else
                                    {
                                        Mediator.SendMessage(i + "|" + Visibility.Collapsed.ToString(), MsgTag.ChangeVisibility);


                                        Dispatcher.Invoke(() =>
                                        {
                                            if (!lmViewModels[i].IsClosed)
                                            {
                                                lmViewModels[i].ViewWindow.Visibility = Visibility.Visible;
                                            }
                                        });
                                        //lmViewModels[i].HeaderTextVisibility = Visibility.Collapsed;
                                        //lmViewModels[i].BannerVisibility = Visibility.Collapsed;
                                        //lmViewModels[i].rotating = false;
                                        //lmViewModels[i].rotationInterval = 10;
                                        //lmViewModels[i].MonitorWidth = screens[i].WorkingArea.Width;
                                        //lmViewModels[i].MonitorHeight = screens[i].WorkingArea.Height;

                                        if (type == "HEADER_TEXT")
                                        {
                                            lmViewModels[i].HeaderRow            = y;
                                            lmViewModels[i].HeaderTextVisibility = Visibility.Visible;
                                            double dd  = Int64.Parse(template.content.ElementAt(y).size);
                                            double sc  = screens[i].WorkingArea.Height;
                                            double res = dd * sc / 100;
                                            lmViewModels[i].HeaderTextHeight = res;
                                            lmViewModels[i].HeaderText       = template.content.ElementAt(y).data;
                                        }
                                        else if (type == "LIVE_ODDS")
                                        {
                                            lmViewModels[i].LiveOddsRow        = y;
                                            lmViewModels[i].LiveOddsVisibility = Visibility.Visible;
                                            double desiredHeight = Int64.Parse(template.content.ElementAt(y).size) *
                                                                   screens[i].WorkingArea.Height / 100;
                                            ;
                                            if (desiredHeight != lmViewModels[i].LiveOddsHeight)
                                            {
                                                Dispatcher.Invoke(() =>
                                                {
                                                    LiveMonitorsCollectionList[i].Clear();
                                                });

                                                lmViewModels[i].LiveOddsHeight = desiredHeight;
                                            }

                                            if (template.content.ElementAt(y).data.ToLowerInvariant().Contains("page"))
                                            {
                                                string page =
                                                    template.content.ElementAt(y)
                                                    .data.Substring(template.content.ElementAt(y).data.Length - 1, 1);
                                                lmViewModels[i].liveOddsPage     = Int16.Parse(page);
                                                lmViewModels[i].rotating         = false;
                                                lmViewModels[i].rotationInterval = 10;
                                            }
                                            else if (
                                                template.content.ElementAt(y)
                                                .data.ToLowerInvariant()
                                                .Contains("rotating"))
                                            {
                                                //if(!lmViewModels[i].rotating)
                                                //    LiveMonitorRotationCounters[i] = 0;

                                                lmViewModels[i].rotating         = true;
                                                lmViewModels[i].rotationInterval =
                                                    Int16.Parse(template.content.ElementAt(y).additParams);
                                            }
                                        }
                                        else if (type == "BANNER")
                                        {
                                            double dd  = Int64.Parse(template.content.ElementAt(y).size);
                                            double sc  = screens[i].WorkingArea.Height;
                                            double res = dd * sc / 100;

                                            lmViewModels[i].BannerRow        = y;
                                            lmViewModels[i].BannerVisibility = Visibility.Visible;
                                            lmViewModels[i].BannerHeight     = res;
                                            lmViewModels[i].BannerLink       = template.content.ElementAt(y).data;
                                        }
                                    }
                                }
                            }
                        }
                    }

                    LiveMonitorViewModel vm         = lmViewModels.Where(x => x.liveOddsPage != 1000).FirstOrDefault();
                    LiveMonitorViewModel vmRotating = lmViewModels.Where(x => x.rotating).FirstOrDefault();
                    if (vm != null || vmRotating != null)
                    {
                        lmViewModels = new SyncList <LiveMonitorViewModel>(lmViewModels.OrderBy(x => x.liveOddsPage).ToList());
                        LiveMonitorHeights.Clear();
                        for (int i = 0; i < lmViewModels.Count; i++)
                        {
                            LiveMonitorHeights.Add(lmViewModels[i].LiveOddsHeight);
                            lmViewModels[i].MatchesCollection = LiveMonitorsCollectionList[i];
                        }
                    }
                    else
                    {
                        ResetMonitorsHeight();
                    }
                }
                catch (Exception ex)
                {
                }
            }
        }