public JamViewModel(int jamNumber, long currentGameTimeInMilliseconds, int currentPeriod)
 {
     this.LeadJammers = new List<LeadJammerViewModel>();
     this.JamPasses = new List<Jam.JamPass>();
     this.JamId = Guid.NewGuid();
     this.JamNumber = jamNumber;
     this.GameTimeElapsedMillisecondsStart = currentGameTimeInMilliseconds;
     this.CurrentPeriod = currentPeriod;
     JamClock = new StopwatchWrapper(PolicyViewModel.Instance.JamClockTimePerJam);
 }
        public static GameViewModel getGameFromDb(Guid id)
        {
            try
            {
                ManagementContext db = new ManagementContext();
                var getGame = (from xx in db.Games.Include("GameLinks").Include("GameMemberPenalties").Include("GameMemberAssists").Include("GameMemberBlocks").Include("GameTeams").Include("GameJams").Include("GamePolicy").Include("GameScores").Include("GameTimeouts").Include("GameTeams.Logo")
                               where xx.GameId == id
                               //game must be published to be used.

                               //where xx.IsGamePublishedOnline == true
                               select xx).FirstOrDefault();

                if (getGame == null)
                    return null;
                GameViewModel game = new GameViewModel();

                if (getGame.GameTournament != null)
                {
                    game.TournamentId = getGame.GameTournament.TournamentId;
                    game.TournamentName = getGame.GameTournament.TournamentName;
                }
                if (getGame.FederationOwners.Count > 0)
                {
                    game.FederationId = getGame.FederationOwners.FirstOrDefault().Federation.FederationId;
                    game.FederationName = getGame.FederationOwners.FirstOrDefault().Federation.Name;
                }
                if (getGame.SelectedShop != null)
                    game.SelectedShop = getGame.SelectedShop.MerchantId.ToString();
                game.IsThereVideoOfGame = (GameVideoTypeEnum)Enum.Parse(typeof(GameVideoTypeEnum), getGame.IsThereVideoOfGame.ToString());
                game.StreamingUrlOfVideo = getGame.StreamingUrlOfVideo;
                game.StreamingUrlOfVideoMobile = getGame.StreamingUrlOfVideoMobile;
                game.EmbededVideoHtml = getGame.EmbededVideoHtml;
                game.LastModified = DateTime.UtcNow;
                game.IdForOnlineManagementUse = getGame.IdForOnlineManagementUse;
                game.ElapsedTimeGameClockMilliSeconds = (long)getGame.ElapsedGameTimeInMilliseconds;
                game.GameDate = getGame.GameDate;
                game.GameEndDate = getGame.GameEndDate;
                game.GameId = id;
                game.GameName = getGame.GameName;
                game.HasGameEnded = getGame.IsGameOver;
                game.HasGameStarted = getGame.HasGameStarted;
                game.GameLocation = getGame.GameLocation;
                game.PublishGameOnline = getGame.IsGameLive;
                game.SaveGameOnline = getGame.IsGameScrimmage;
                GameClock.getPeriodClock(id, game);
                GameClock.getIntermissionClock(id, game);
                GameClock.getLineUpClock(id, game);
                GameClock.getTimeOutClock(id, game);
                if (getGame.Paywall != null)
                    game.PaywallId = getGame.Paywall.PaywallId;


                if (getGame.ScoreboardType == 0)
                    game.ScoreboardMode = ScoreboardModeEnum.Debug;
                else if (getGame.ScoreboardType == 1)
                    game.ScoreboardMode = ScoreboardModeEnum.Live;

                //we order the teams so the first one in, is also the first one out.
                var getTeams = getGame.GameTeams.OrderByDescending(x => x.Created).ToList();
                if (game.ScoresTeam1 == null)
                    game.ScoresTeam1 = new List<ScoreViewModel>();
                if (game.ScoresTeam2 == null)
                    game.ScoresTeam2 = new List<ScoreViewModel>();

                game.GameLinks = new List<GameLinkViewModel>();
                var gameLinkss = getGame.GameLinks.ToList();
                for (int i = 0; i < getGame.GameLinks.Count; i++)
                {
                    GameLinkViewModel gameLink = new GameLinkViewModel();
                    gameLink.GameLink = gameLinkss[i].Link;
                    gameLink.LinkId = gameLinkss[i].GameLinkId;
                    gameLink.LinkType = (GameLinkTypeEnum)Enum.ToObject(typeof(GameLinkTypeEnum), gameLinkss[i].LinkType);
                    game.GameLinks.Add(gameLink);
                }


                for (int i = 0; i < getTeams.Count; i++)
                {
                    TeamViewModel tvm = new TeamViewModel();
                    tvm.TeamId = getTeams[i].TeamId;
                    tvm.Logo = new Portable.Classes.Team.TeamLogo();

                    tvm.TeamLinkId = getTeams[i].TeamIdLink;
                    var dc = new ManagementContext();
                    if (getTeams[i].Logo != null)
                    {
                        tvm.Logo.ImageUrl = getTeams[i].Logo.ImageUrl;
                        tvm.Logo.TeamLogoId = getTeams[i].Logo.TeamLogoId;
                    }

                    tvm.TeamName = getTeams[i].TeamName;
                    tvm.TimeOutsLeft = getTeams[i].CurrentTimeouts;
                    tvm.TeamMembers = new System.Collections.ObjectModel.ObservableCollection<TeamMembersViewModel>();
                    if (i == 0)
                    {
                        game.CurrentTeam1Score = getTeams[i].CurrentScore;
                        game.Team1 = tvm;
                    }
                    else if (i == 1)
                    {
                        game.CurrentTeam2Score = getTeams[i].CurrentScore;
                        game.Team2 = tvm;
                    }


                    foreach (var mem in getTeams[i].GameMembers)
                    {
                        TeamMembersViewModel mvm = new TeamMembersViewModel();
                        mvm.SkaterId = mem.GameMemberId;
                        mvm.SkaterName = mem.MemberName;
                        mvm.SkaterNumber = mem.MemberNumber;
                        mvm.SkaterLinkId = mem.MemberLinkId;

                        if (getTeams[i].TeamId == game.Team1.TeamId)
                            game.Team1.TeamMembers.Add(mvm);
                        else if (getTeams[i].TeamId == game.Team2.TeamId)
                            game.Team2.TeamMembers.Add(mvm);
                    }
                    //scores must come after teams  members get added.
                    var scores = getTeams[i].GameScores.OrderBy(x => x.JamNumber);
                    foreach (var score in scores)
                    {
                        try
                        {
                            ScoreViewModel svm = new ScoreViewModel(score.Point, score.PeriodTimeRemainingMilliseconds, score.JamId, score.JamNumber, score.PeriodNumber, score.DateTimeScored, score.GameScoreId);

                            if (getTeams[i].TeamId == game.Team1.TeamId)
                            {
                                if (score.MemberWhoScored != null)
                                    svm.PlayerWhoScored = game.Team1.TeamMembers.Where(x => x.SkaterId == score.MemberWhoScored.GameMemberId).FirstOrDefault();
                                game.ScoresTeam1.Add(svm);
                            }
                            else if (getTeams[i].TeamId == game.Team2.TeamId)
                            {
                                if (score.MemberWhoScored != null)
                                    svm.PlayerWhoScored = game.Team2.TeamMembers.Where(x => x.SkaterId == score.MemberWhoScored.GameMemberId).FirstOrDefault();
                                game.ScoresTeam2.Add(svm);
                            }
                        }
                        catch (Exception exception)
                        {
                            ErrorDatabaseManager.AddException(exception, exception.GetType());
                        }
                    }


                }
                game.BlocksForTeam1 = new List<BlockViewModel>();
                game.BlocksForTeam2 = new List<BlockViewModel>();
                game.AssistsForTeam1 = new List<AssistViewModel>();
                game.AssistsForTeam2 = new List<AssistViewModel>();
                game.PenaltiesForTeam1 = new List<PenaltyViewModel>();
                game.PenaltiesForTeam2 = new List<PenaltyViewModel>();
                //blocks must come after teams  members get added.
                var blocks = getGame.GameMemberBlocks.OrderBy(x => x.JamNumber);
                foreach (var block in blocks)
                {
                    try
                    {
                        BlockViewModel svm = new BlockViewModel(block.PeriodTimeRemainingMilliseconds, block.JamNumber, block.PeriodNumber, block.DateTimeBlocked, block.GameBlockId);

                        var member = game.Team1.TeamMembers.Where(x => x.SkaterId == block.MemberWhoBlocked.GameMemberId).FirstOrDefault();
                        if (member == null)
                        {
                            member = game.Team2.TeamMembers.Where(x => x.SkaterId == block.MemberWhoBlocked.GameMemberId).FirstOrDefault();
                            svm.PlayerWhoBlocked = member;
                            game.BlocksForTeam2.Add(svm);
                        }
                        else
                        {
                            svm.PlayerWhoBlocked = member;
                            game.BlocksForTeam1.Add(svm);
                        }
                    }
                    catch (Exception exception)
                    {
                        ErrorDatabaseManager.AddException(exception, exception.GetType());
                    }
                }

                //blocks must come after teams  members get added.
                var assists = getGame.GameMemberAssists.OrderBy(x => x.JamNumber);
                foreach (var assist in assists)
                {
                    try
                    {
                        AssistViewModel svm = new AssistViewModel(assist.PeriodTimeRemainingMilliseconds, assist.JamNumber, assist.PeriodNumber, assist.DateTimeAssisted, assist.GameAssistId);

                        var member = game.Team1.TeamMembers.Where(x => x.SkaterId == assist.MemberWhoAssisted.GameMemberId).FirstOrDefault();
                        if (member == null)
                        {
                            member = game.Team2.TeamMembers.Where(x => x.SkaterId == assist.MemberWhoAssisted.GameMemberId).FirstOrDefault();
                            svm.PlayerWhoAssisted = member;
                            game.AssistsForTeam2.Add(svm);
                        }
                        else
                        {
                            svm.PlayerWhoAssisted = member;
                            game.AssistsForTeam1.Add(svm);
                        }
                    }
                    catch (Exception exception)
                    {
                        ErrorDatabaseManager.AddException(exception, exception.GetType());
                    }
                }

                var penal = getGame.GameMemberPenalties.OrderBy(x => x.JamNumber);
                foreach (var pen in penal)
                {
                    try
                    {
                        PenaltyViewModel svm = new PenaltyViewModel((PenaltiesEnum)Enum.Parse(typeof(PenaltiesEnum), pen.PenaltyType.ToString()), pen.PeriodTimeRemainingMilliseconds, pen.JamNumber, pen.PeriodNumber, pen.DateTimePenaltied, pen.GamePenaltyId);

                        var member = game.Team1.TeamMembers.Where(x => x.SkaterId == pen.MemberWhoPenaltied.GameMemberId).FirstOrDefault();
                        if (member == null)
                        {
                            member = game.Team2.TeamMembers.Where(x => x.SkaterId == pen.MemberWhoPenaltied.GameMemberId).FirstOrDefault();
                            svm.PenaltyAgainstMember = member;
                            game.PenaltiesForTeam2.Add(svm);
                        }
                        else
                        {
                            svm.PenaltyAgainstMember = member;
                            game.PenaltiesForTeam1.Add(svm);
                        }
                    }
                    catch (Exception exception)
                    {
                        ErrorDatabaseManager.AddException(exception, exception.GetType());
                    }
                }

                var getJams = getGame.GameJams.OrderBy(x => x.JamNumber).ToList();
                var getJamClocks = (from xx in db.GameStopWatch
                                    where xx.StopwatchForId == id
                                    where xx.Type == (int)StopWatchTypeEnum.JamClock
                                    select xx).ToList();

                foreach (var jam in getJams)
                {
                    try
                    {
                        JamViewModel jvm = new JamViewModel(jam.JamNumber, jam.GameTimeElapsedMillisecondsStart, jam.CurrentPeriod);
                        var scoresT1 = game.ScoresTeam1.Where(x => x.JamNumber == jam.JamNumber);
                        foreach (var score in scoresT1)
                            jvm.TotalPointsForJamT1 += score.Points;

                        var scoresT2 = game.ScoresTeam2.Where(x => x.JamNumber == jam.JamNumber);
                        foreach (var score in scoresT2)
                            jvm.TotalPointsForJamT2 += score.Points;

                        var getClock = getJamClocks.Where(x => x.JamNumber == jam.JamNumber).FirstOrDefault();
                        if (getClock != null)
                        {
                            StopwatchWrapper stop = new StopwatchWrapper();
                            stop.IsClockAtZero = getClock.IsClockAtZero == 1 ? true : false;
                            stop.IsRunning = getClock.IsRunning == 1 ? true : false;
                            stop.StartTime = getClock.StartDateTime;
                            stop.TimeElapsed = getClock.TimeElapsed;
                            stop.TimeRemaining = getClock.TimeRemaining;
                            stop.TimerLength = getClock.Length;
                            jvm.JamClock = stop;
                        }

                        if (jam.Blocker1Team1 != null)
                        {
                            TeamMembersViewModel tmvm = new TeamMembersViewModel();
                            tmvm.SkaterId = jam.Blocker1Team1.GameMemberId;
                            tmvm.SkaterLinkId = jam.Blocker1Team1.MemberLinkId;
                            tmvm.SkaterName = jam.Blocker1Team1.MemberName;
                            tmvm.SkaterNumber = jam.Blocker1Team1.MemberNumber;
                            jvm.Blocker1T1 = tmvm;
                        }
                        if (jam.Blocker2Team1 != null)
                        {
                            TeamMembersViewModel tmvm = new TeamMembersViewModel();
                            tmvm.SkaterId = jam.Blocker2Team1.GameMemberId;
                            tmvm.SkaterLinkId = jam.Blocker2Team1.MemberLinkId;
                            tmvm.SkaterName = jam.Blocker2Team1.MemberName;
                            tmvm.SkaterNumber = jam.Blocker2Team1.MemberNumber;
                            jvm.Blocker2T1 = tmvm;
                        }
                        if (jam.Blocker3Team1 != null)
                        {
                            TeamMembersViewModel tmvm = new TeamMembersViewModel();
                            tmvm.SkaterId = jam.Blocker3Team1.GameMemberId;
                            tmvm.SkaterLinkId = jam.Blocker3Team1.MemberLinkId;
                            tmvm.SkaterName = jam.Blocker3Team1.MemberName;
                            tmvm.SkaterNumber = jam.Blocker3Team1.MemberNumber;
                            jvm.Blocker3T1 = tmvm;
                        }
                        if (jam.Blocker4Team1 != null)
                        {
                            TeamMembersViewModel tmvm = new TeamMembersViewModel();
                            tmvm.SkaterId = jam.Blocker4Team1.GameMemberId;
                            tmvm.SkaterLinkId = jam.Blocker4Team1.MemberLinkId;
                            tmvm.SkaterName = jam.Blocker4Team1.MemberName;
                            tmvm.SkaterNumber = jam.Blocker4Team1.MemberNumber;
                            jvm.Blocker4T1 = tmvm;
                        }
                        if (jam.PivotTeam1 != null)
                        {
                            TeamMembersViewModel tmvm = new TeamMembersViewModel();
                            tmvm.SkaterId = jam.PivotTeam1.GameMemberId;
                            tmvm.SkaterLinkId = jam.PivotTeam1.MemberLinkId;
                            tmvm.SkaterName = jam.PivotTeam1.MemberName;
                            tmvm.SkaterNumber = jam.PivotTeam1.MemberNumber;
                            jvm.PivotT1 = tmvm;
                        }
                        if (jam.JammerTeam1 != null)
                        {
                            TeamMembersViewModel tmvm = new TeamMembersViewModel();
                            tmvm.SkaterId = jam.JammerTeam1.GameMemberId;
                            tmvm.SkaterLinkId = jam.JammerTeam1.MemberLinkId;
                            tmvm.SkaterName = jam.JammerTeam1.MemberName;
                            tmvm.SkaterNumber = jam.JammerTeam1.MemberNumber;
                            jvm.JammerT1 = tmvm;
                        }
                        if (jam.Blocker1Team2 != null)
                        {
                            TeamMembersViewModel tmvm = new TeamMembersViewModel();
                            tmvm.SkaterId = jam.Blocker1Team2.GameMemberId;
                            tmvm.SkaterLinkId = jam.Blocker1Team2.MemberLinkId;
                            tmvm.SkaterName = jam.Blocker1Team2.MemberName;
                            tmvm.SkaterNumber = jam.Blocker1Team2.MemberNumber;
                            jvm.Blocker1T2 = tmvm;
                        }
                        if (jam.Blocker2Team2 != null)
                        {
                            TeamMembersViewModel tmvm = new TeamMembersViewModel();
                            tmvm.SkaterId = jam.Blocker2Team2.GameMemberId;
                            tmvm.SkaterLinkId = jam.Blocker2Team2.MemberLinkId;
                            tmvm.SkaterName = jam.Blocker2Team2.MemberName;
                            tmvm.SkaterNumber = jam.Blocker2Team2.MemberNumber;
                            jvm.Blocker2T2 = tmvm;
                        }
                        if (jam.Blocker3Team2 != null)
                        {
                            TeamMembersViewModel tmvm = new TeamMembersViewModel();
                            tmvm.SkaterId = jam.Blocker3Team2.GameMemberId;
                            tmvm.SkaterLinkId = jam.Blocker3Team2.MemberLinkId;
                            tmvm.SkaterName = jam.Blocker3Team2.MemberName;
                            tmvm.SkaterNumber = jam.Blocker3Team2.MemberNumber;
                            jvm.Blocker3T2 = tmvm;
                        }
                        if (jam.Blocker4Team2 != null)
                        {
                            TeamMembersViewModel tmvm = new TeamMembersViewModel();
                            tmvm.SkaterId = jam.Blocker4Team2.GameMemberId;
                            tmvm.SkaterLinkId = jam.Blocker4Team2.MemberLinkId;
                            tmvm.SkaterName = jam.Blocker4Team2.MemberName;
                            tmvm.SkaterNumber = jam.Blocker4Team2.MemberNumber;
                            jvm.Blocker4T2 = tmvm;
                        }
                        if (jam.PivotTeam2 != null)
                        {
                            TeamMembersViewModel tmvm = new TeamMembersViewModel();
                            tmvm.SkaterId = jam.PivotTeam2.GameMemberId;
                            tmvm.SkaterLinkId = jam.PivotTeam2.MemberLinkId;
                            tmvm.SkaterName = jam.PivotTeam2.MemberName;
                            tmvm.SkaterNumber = jam.PivotTeam2.MemberNumber;
                            jvm.PivotT2 = tmvm;
                        }
                        if (jam.JammerTeam2 != null)
                        {
                            TeamMembersViewModel tmvm = new TeamMembersViewModel();
                            tmvm.SkaterId = jam.JammerTeam2.GameMemberId;
                            tmvm.SkaterLinkId = jam.JammerTeam2.MemberLinkId;
                            tmvm.SkaterName = jam.JammerTeam2.MemberName;
                            tmvm.SkaterNumber = jam.JammerTeam2.MemberNumber;
                            jvm.JammerT2 = tmvm;
                        }

                        //gets all the lead jammers for this particular jam.
                        var getLeadJammers = jam.LeadJammers.ToList();
                        foreach (var lJam in getLeadJammers)
                        {
                            try
                            {
                                if (lJam.GameMemberId != new Guid())
                                {
                                    LeadJammerViewModel ljvm = new LeadJammerViewModel();
                                    ljvm.GameTimeInMilliseconds = lJam.GameTimeInMilliseconds;
                                    ljvm.Jammer = new TeamMembersViewModel();
                                    ljvm.Jammer.SkaterId = lJam.GameMemberId;
                                    ljvm.JamTimeInMilliseconds = lJam.JamTimeInMilliseconds;
                                    ljvm.GameLeadJamId = lJam.GameJamLeadId;
                                    jvm.LeadJammers.Add(ljvm);
                                }
                            }
                            catch (Exception exception)
                            { ErrorDatabaseManager.AddException(exception, exception.GetType()); }
                        }

                        game.Jams.Add(jvm);
                    }
                    catch (Exception exception)
                    { ErrorDatabaseManager.AddException(exception, exception.GetType()); }
                }


                var Penalties = getGame.GameMemberPenaltyBox.ToList();

                for (int i = 0; i < Penalties.Count; i++)
                {
                    SkaterInPenaltyBoxViewModel skater = new SkaterInPenaltyBoxViewModel();
                    skater.GameTimeInMillisecondsReleased = Penalties[i].GameTimeMilliSecondsReturned;
                    skater.GameTimeInMillisecondsSent = Penalties[i].GameTimeMilliSecondsSent;
                    skater.JamNumberReleased = Penalties[i].JamNumberReturned;
                    skater.JamNumberSent = Penalties[i].JamNumberSent;
                    skater.JamTimeInMillisecondsReleased = Penalties[i].JamTimeMilliSecondsReturned;
                    skater.JamTimeInMillisecondsSent = Penalties[i].JamTimeMilliSecondsSent;
                    skater.PenaltyId = Penalties[i].PenaltyIdFromGame;
                    skater.PenaltyScale = (PenaltyScaleEnum)Enum.ToObject(typeof(PenaltyScaleEnum), (int)Penalties[i].PenaltyScale);
                    skater.PenaltyType = (PenaltiesEnum)Enum.Parse(typeof(PenaltiesEnum), Penalties[i].PenaltyType);
                    skater.PenaltyNumberForSkater = Penalties[i].PenaltyNumberForSkater;
                    bool checkIfPenaltyIsAssigned = false;


                    for (int j = 0; j < game.Team1.TeamMembers.Count; j++)
                    {
                        if (game.Team1.TeamMembers[j].SkaterId == Penalties[i].Member.GameMemberId)
                        {
                            skater.PlayerSentToBox = game.Team1.TeamMembers[j];
                            game.Team1.TeamMembers[j].Penalties.Add(new PenaltyViewModel(skater.PenaltyType));
                            checkIfPenaltyIsAssigned = true;
                            break;
                        }
                    }
                    if (!checkIfPenaltyIsAssigned)
                    {
                        for (int j = 0; j < game.Team2.TeamMembers.Count; j++)
                        {
                            if (game.Team2.TeamMembers[j].SkaterId == Penalties[i].Member.GameMemberId)
                            {
                                skater.PlayerSentToBox = game.Team2.TeamMembers[j];
                                game.Team2.TeamMembers[j].Penalties.Add(new PenaltyViewModel(skater.PenaltyType));
                                checkIfPenaltyIsAssigned = true;
                                break;
                            }
                        }
                    }

                    game.PenaltyBox.Add(skater);
                }

                var getPolicies = getGame.GamePolicy;
                if (getPolicies != null)
                {
                    if (game.Policy == null)
                        game.Policy = PolicyViewModel.Instance;
                    game.Policy.AdChangeAutomaticallyChangeImage = getPolicies.AdChangeAutomaticallyChangeImage == 0 ? false : true;
                    game.Policy.AdChangeDisplayChangesInMilliSeconds = (long)getPolicies.AdChangeDisplayChangesInMilliSeconds;
                    game.Policy.AdChangeShowAdsDuringIntermission = getPolicies.AdChangeShowAdsDuringIntermission == 0 ? false : true;
                    game.Policy.AdChangeShowAdsRandomly = getPolicies.AdChangeShowAdsRandomly == 0 ? false : true;
                    game.Policy.AdChangeUseLineUpClock = getPolicies.AdChangeUseLineUpClock == 0 ? false : true;
                    game.Policy.AlwaysShowJamClock = getPolicies.AlwaysShowJamClock == 0 ? false : true;
                    game.Policy.EnableAdChange = getPolicies.EnableAdChange == 0 ? false : true;
                    game.Policy.EnableIntermissionNaming = getPolicies.EnableIntermissionNaming == 0 ? false : true;
                    game.Policy.EnableIntermissionStartOfClock = getPolicies.EnableIntermissionStartOfClock == 0 ? false : true;
                    game.Policy.FirstIntermissionNameConfirmedText = getPolicies.FirstIntermissionNameConfirmedText;
                    game.Policy.FirstIntermissionNameText = getPolicies.FirstIntermissionNameText;
                    game.Policy.GameSelectionType = (GameTypeEnum)Enum.Parse(typeof(GameTypeEnum), getPolicies.GameSelectionType);
                    game.Policy.HideClockTimeAfterBout = getPolicies.HideClockTimeAfterBout == 0 ? false : true;
                    game.Policy.IntermissionOtherText = getPolicies.IntermissionOtherText;
                    game.Policy.IntermissionStartOfClockInMilliseconds = (long)getPolicies.IntermissionStartOfClockInMilliseconds;
                    game.Policy.IntermissionStopClockEnable = getPolicies.IntermissionStopClockEnable == 0 ? false : true;
                    game.Policy.IntermissionStopClockIncrementJamNumber = getPolicies.IntermissionStopClockIncrementJamNumber == 0 ? false : true;
                    game.Policy.IntermissionStopClockIncrementPeriodNumber = getPolicies.IntermissionStopClockIncrementPeriodNumber == 0 ? false : true;
                    game.Policy.IntermissionStopClockResetJamNumber = getPolicies.IntermissionStopClockResetJamNumber == 0 ? false : true;
                    game.Policy.IntermissionStopClockResetJamTime = getPolicies.IntermissionStopClockResetJamTime == 0 ? false : true;
                    game.Policy.IntermissionStopClockResetPeriodNumber = getPolicies.IntermissionStopClockResetPeriodNumber == 0 ? false : true;
                    game.Policy.IntermissionStopClockResetPeriodTime = getPolicies.IntermissionStopClockResetPeriodTime == 0 ? false : true;
                    game.Policy.JamClockControlsLineUpClock = getPolicies.JamClockControlsLineUpClock == 0 ? false : true;
                    game.Policy.JamClockControlsTeamPositions = getPolicies.JamClockControlsTeamPositions == 0 ? false : true;
                    game.Policy.JamClockTimePerJam = (long)getPolicies.JamClockTimePerJam;
                    game.Policy.LineupClockControlsStartJam = getPolicies.LineupClockControlsStartJam == 0 ? false : true;
                    game.Policy.LineUpClockPerJam = (long)getPolicies.LineUpClockPerJam;
                    game.Policy.NumberOfPeriods = (int)getPolicies.NumberOfPeriods;
                    game.Policy.PenaltyBoxControlsLeadJammer = getPolicies.PenaltyBoxControlsLeadJammer == 0 ? false : true;
                    game.Policy.PeriodClock = (long)getPolicies.PeriodClock;
                    game.Policy.PeriodClockControlsLineupJamClock = getPolicies.PeriodClockControlsLineupJamClock == 0 ? false : true;
                    game.Policy.SecondIntermissionNameConfirmedText = getPolicies.SecondIntermissionNameConfirmedText;
                    game.Policy.SecondIntermissionNameText = getPolicies.SecondIntermissionNameText;
                    game.Policy.TimeOutClock = (long)getPolicies.TimeOutClock;
                    game.Policy.TimeoutClockControlsLineupClock = getPolicies.TimeoutClockControlsLineupClock == 0 ? false : true;
                    game.Policy.TimeOutsPerPeriod = (int)getPolicies.TimeOutsPerPeriod;
                }




                //TODO: get game adverts.


                var getTimeOuts = getGame.GameTimeouts.ToList();
                foreach (var time in getTimeOuts)
                {
                    TimeOutViewModel tvm = new TimeOutViewModel();

                    tvm.TimeoutId = time.TimeOutId;
                    if (time.GameTeam != null && time.GameTeam.TeamId == game.Team1.TeamId)
                        tvm.TimeOutType = TimeOutTypeEnum.Team1;
                    else if (time.GameTeam != null && time.GameTeam.TeamId == game.Team2.TeamId)
                        tvm.TimeOutType = TimeOutTypeEnum.Team2;
                    else
                        tvm.TimeOutType = TimeOutTypeEnum.Offical;

                    if (game.TimeOuts == null)
                        game.TimeOuts = new List<TimeOutViewModel>();

                    game.TimeOuts.Add(tvm);
                }

                return game;
            }
            catch (Exception exception)
            {
                ErrorDatabaseManager.AddException(exception, exception.GetType());
            }
            return null;
        }
 public TimeOutViewModel(StopwatchWrapper timeOutClock, TimeOutTypeEnum type)
 {
     TimeOutClock = timeOutClock;
     TimeOutType = type;
     TimeoutId = Guid.NewGuid();
 }
        /// <summary>
        /// updates the stop watch clock for a chosen clock.
        /// </summary>
        /// <param name="gameId"></param>
        /// <param name="db"></param>
        /// <param name="watch"></param>
        /// <param name="type"></param>
        private static bool updateClock(Guid gameId, ManagementContext db, StopwatchWrapper watch, StopWatchTypeEnum type, DataModels.Game.Game g)
        {
            try
            {
                if (watch.StartTime != new DateTime())
                {
                    var findPeriod = (from xx in db.GameStopWatch
                                      where xx.StopwatchForId == gameId
                                      where xx.Type == (int)type
                                      select xx).FirstOrDefault();
                    if (findPeriod == null)
                    {
                        GameStopwatch stop = new GameStopwatch();
                        stop.Created = DateTime.UtcNow;
                        stop.IsClockAtZero = watch.IsClockAtZero == true ? 1 : 0;
                        stop.IsRunning = watch.IsRunning == true ? 1 : 0;
                        stop.Length = watch.TimerLength;
                        stop.StartDateTime = watch.StartTime;
                        stop.StopwatchForId = gameId;
                        stop.TimeElapsed = watch.TimeElapsed;
                        stop.TimeRemaining = watch.TimeRemaining;
                        stop.Type = (int)type;
                        stop.Game = g;
                        db.GameStopWatch.Add(stop);
                        int c = db.SaveChanges();
                        return c > 0;
                    }
                    else
                    {
                        findPeriod.Game = findPeriod.Game;
                        findPeriod.IsClockAtZero = watch.IsClockAtZero == true ? 1 : 0;
                        findPeriod.IsRunning = watch.IsRunning == true ? 1 : 0;
                        findPeriod.Length = watch.TimerLength;
                        findPeriod.StartDateTime = watch.StartTime;
                        findPeriod.TimeElapsed = watch.TimeElapsed;
                        findPeriod.TimeRemaining = watch.TimeRemaining;
                        int c = db.SaveChanges();
                        return c > 0;
                    }
                }
            }
            catch (Exception e)
            {
                ErrorDatabaseManager.AddException(e, e.GetType(), ErrorGroupEnum.Database);
            }
            return false;

        }