void SendRestMessageAsync(TeamData team, LiveStream.TeamStates teamState)
    {
        LiveStream.Team newTeam = new LiveStream.Team(
            teamState,
            CurDivision.ToString(),
            CurRound.ToString(),
            ((EPool)CurPool).ToString(),
            CurTeam);

        foreach (PlayerData pd in team.Players)
        {
            newTeam.Players.Add(new LiveStream.Player(pd));
        }

        SendRestMessageAsync(newTeam);
    }
    public static IEnumerator SendRestMessage(LiveStream.Team team)
    {
        // http://localhost:9000
        using (UnityWebRequest www = CreateUnityWebRequest(LivestreamCompUrl + "/api/teams", team))
        {
            yield return(www.Send());

            if (www.isError)
            {
                Debug.Log("Rest Api: " + www.error);
            }
            else
            {
                Debug.Log("Rest Api: " + www.downloadHandler.text);
            }
        }
    }
    void TestRestApi()
    {
        LiveStream.Player playerA = new LiveStream.Player();
        playerA.Name = "Ryan";
        LiveStream.Player playerB = new LiveStream.Player();
        playerB.Name = "James";
        LiveStream.Player playerC = new LiveStream.Player();
        playerC.Name = "Jake";
        LiveStream.Player playerD = new LiveStream.Player();
        playerD.Name = "Randy";

        LiveStream.Team teamA = new LiveStream.Team();
        teamA.State = LiveStream.TeamStates.JudgesReady;
        teamA.Players.Add(playerA);
        teamA.Players.Add(playerB);

        LiveStream.Team teamB = new LiveStream.Team();
        teamB.State = LiveStream.TeamStates.JudgesReady;
        teamB.Players.Add(playerB);
        teamB.Players.Add(playerC);

        LiveStream.Team teamC = new LiveStream.Team();
        teamC.State = LiveStream.TeamStates.JudgesReady;
        teamC.Players.Add(playerB);
        teamC.Players.Add(playerC);
        teamC.Players.Add(playerD);

        LiveStream.Team teamD = new LiveStream.Team();
        teamD.State = LiveStream.TeamStates.JudgesReady;
        teamD.Players.Add(playerC);
        teamD.Players.Add(playerD);

        SendRestMessageAsync(teamA);

        LiveStream.TeamList teamList = new LiveStream.TeamList();
        teamList.Teams.Add(teamA);
        teamList.Teams.Add(teamB);
        teamList.Teams.Add(teamC);
        teamList.Teams.Add(teamD);

        //SendRestMessageAsync(teamList);

        teamD.DifficultyScore = 10f;
        SendRestMessageAsync(teamD);
    }
    public void StopRoutine(bool bCancelled)
    {
        if (bJudging)
        {
            Global.NetObj.ServerSendStopRoutine();

            bJudging          = false;
            bLockedForJudging = false;

            // Send message to livestream
            TeamData stoppedTeam = Global.GetTeamData(CurDivision, CurRound, CurPool, CurTeam);
            if (bCancelled && !bRoutineTimeElapsed)
            {
                SendRestMessageAsync(stoppedTeam, LiveStream.TeamStates.Stopped);
            }
            else
            {
                LiveStream.Team finishedTeam = new LiveStream.Team(
                    LiveStream.TeamStates.ScoresRecorded,
                    CurDivision.ToString(),
                    CurRound.ToString(),
                    ((EPool)CurPool).ToString(),
                    CurTeam);

                foreach (PlayerData pd in stoppedTeam.Players)
                {
                    finishedTeam.Players.Add(new LiveStream.Player(pd));
                }

                finishedTeam.DifficultyScore         = stoppedTeam.RoutineScores.GetDiffPoints();
                finishedTeam.ArtisticImpressionScore = stoppedTeam.RoutineScores.GetAIPoints();
                finishedTeam.ExecutionScore          = stoppedTeam.RoutineScores.GetExPoints();

                SendRestMessageAsync(finishedTeam);
            }
        }
    }
 void SendRestMessageAsync(LiveStream.Team team)
 {
     StartCoroutine(Global.SendRestMessage(team));
 }
    void OnGUI()
    {
        if (!bIsEditing)
        {
            CatCombo.List(new Rect(20, Screen.height * .03f, Screen.width * .18f, Screen.height * .07f),
                          CatComboList[CatCombo.GetSelectedItemIndex()], CatComboList, CatComboStyle);

            if (CatPickingFrameUpdate != Time.frameCount)
            {
                CatPickingFrameUpdate = Time.frameCount;
                bCatPicking           = CatCombo.IsPicking;
            }
        }

        if (bInEditingMode && CatCombo.GetSelectedItemIndex() == 3)
        {
            bInEditingMode  = false;
            bIsEditing      = false;
            EditAIData      = null;
            EditExData      = null;
            EditDiffData    = null;
            bEditAddNewData = false;
        }

        if (!bIsEditing && GUI.Button(new Rect(Screen.width * .3f, Screen.height * .04f, Screen.width * .25f, Screen.height * .07f), "Export SCHEDULE to Excel"))
        {
            ExportScheduleToExcel();
        }

        if (!bIsEditing && GUI.Button(new Rect(Screen.width * .56f, Screen.height * .04f, Screen.width * .23f, Screen.height * .07f), "Export RESULTS to Excel"))
        {
            ExportResultsToExcel();
        }

        if (GUI.Button(new Rect(Screen.width * .83f, Screen.height * .04f, Screen.width * .17f - 20, Screen.height * .07f), "Send All Teams to Livestream"))
        {
            LiveStream.TeamList teamList = new LiveStream.TeamList();
            EDivision           division = EDivision.Open;
            foreach (DivisionData dd in Global.AllData.AllDivisions)
            {
                ERound round = ERound.Finals;
                foreach (RoundData rd in dd.Rounds)
                {
                    EPool pool = EPool.A;
                    foreach (PoolData pd in rd.Pools)
                    {
                        int teamNumber = 0;
                        foreach (TeamDataDisplay td in pd.Teams)
                        {
                            LiveStream.Team team = new LiveStream.Team(
                                LiveStream.TeamStates.Inited,
                                division.ToString(),
                                round.ToString(),
                                pool.ToString(),
                                teamNumber
                                );

                            foreach (PlayerData playerData in td.Data.Players)
                            {
                                team.Players.Add(new LiveStream.Player(playerData));
                            }

                            teamList.Teams.Add(team);

                            ++teamNumber;
                        }

                        ++pool;
                    }

                    ++round;
                }

                ++division;
            }

            SendRestMessageAsync(teamList);
        }

        #region Round Buttons
        GUIStyle RoundStyle      = new GUIStyle("button");
        string   LongestRoundStr = "Women - Quarterfinals";
        Rect     RoundRect       = new Rect(20, Screen.height * .15f, RoundStyle.CalcSize(new GUIContent(LongestRoundStr)).x + 40, Screen.height * .75f - 20);
        if (!bIsEditing && !bCatPicking)
        {
            GUILayout.BeginArea(RoundRect);

            RoundsScrollPos = GUILayout.BeginScrollView(RoundsScrollPos, GUILayout.MaxHeight(RoundRect.height / 2f));
            GUIStyle RoundButtonStyle = new GUIStyle("button");
            RoundButtonStyle.alignment = TextAnchor.MiddleLeft;
            for (int DivIndex = 0; DivIndex < Global.AllData.AllDivisions.Length; ++DivIndex)
            {
                DivisionData DivData = Global.AllData.AllDivisions[DivIndex];
                if (DivData.HasAnyPoolData())
                {
                    for (int RoundIndex = 0; RoundIndex < DivData.Rounds.Length; ++RoundIndex)
                    {
                        RoundData RData = DivData.Rounds[RoundIndex];
                        if (RData.HasAnyPoolData())
                        {
                            GUIContent RoundContent  = new GUIContent(((EDivision)DivIndex) + " - " + ((ERound)RoundIndex) + (RData.ContainsJudgeScores() ? " - Results" : ""));
                            Vector2    RoundTextSize = RoundButtonStyle.CalcSize(RoundContent);
                            if (GUILayout.Button(RoundContent, RoundButtonStyle, GUILayout.Width(RoundRect.width * .9f), GUILayout.Height(RoundTextSize.y)))
                            {
                                CurDivIndex   = DivIndex;
                                CurRoundIndex = RoundIndex;
                            }
                        }
                    }
                }
            }
            GUILayout.EndScrollView();

            GUILayout.EndArea();
        }
        #endregion

        if (CurDivIndex != -1 && CurRoundIndex != -1)
        {
            float StartX   = RoundRect.x + RoundRect.width + 20;
            Rect  TeamRect = new Rect(StartX, RoundRect.y, Screen.width - StartX - 20, Screen.height - RoundRect.y - 20);
            GUILayout.BeginArea(TeamRect);
            GUILayout.BeginVertical();
            TeamsScrollPos = GUILayout.BeginScrollView(TeamsScrollPos);
            GUILayout.BeginVertical();

            GUIStyle labelStyle = new GUIStyle("label");
            labelStyle.normal.textColor = Color.black;

            if (!bIsEditing)
            {
                List <PoolData> Pools     = Global.AllData.AllDivisions[CurDivIndex].Rounds[CurRoundIndex].Pools;
                int             PoolIndex = 0;
                foreach (PoolData pd in Pools)
                {
                    GUILayout.Label("Pool " + pd.PoolName, labelStyle);

                    // This is a hack to get team result order
                    List <RoutineScoreData> sortedScores = new List <RoutineScoreData>();
                    foreach (TeamDataDisplay tdd in pd.Teams)
                    {
                        if (sortedScores.Count == 0)
                        {
                            sortedScores.Add(tdd.Data.RoutineScores);
                        }
                        else
                        {
                            int insertIndex = 0;
                            foreach (RoutineScoreData score in sortedScores)
                            {
                                if (tdd.Data.RoutineScores.GetTotalPoints() > score.GetTotalPoints())
                                {
                                    sortedScores.Insert(insertIndex, tdd.Data.RoutineScores);
                                    break;
                                }

                                ++insertIndex;
                            }

                            if (insertIndex == sortedScores.Count)
                            {
                                sortedScores.Insert(insertIndex, tdd.Data.RoutineScores);
                            }
                        }
                    }

                    int TeamNum = 0;
                    foreach (TeamDataDisplay tdd in pd.Teams)
                    {
                        ++TeamNum;
                        GUILayout.Label(TeamNum + ": " + tdd.Data.PlayerNames + ":", labelStyle);

                        for (int ScoresIndex = 0; ScoresIndex < (CurCat == ECategoryView.Overview ? 1 : 3); ++ScoresIndex)
                        {
                            GUIStyle EditStyle = new GUIStyle("button");
                            EditStyle.alignment = TextAnchor.MiddleLeft;
                            string ResultsStr = tdd.Data.RoutineScores.GetResultsString(ScoresIndex, CurCat, true, sortedScores);
                            if (ResultsStr.Length > 0)
                            {
                                if (bInEditingMode)
                                {
                                    if (GUILayout.Button("    " + ResultsStr, EditStyle))
                                    {
                                        bIsEditing     = true;
                                        EditPoolIndex  = PoolIndex;
                                        EditTeamIndex  = TeamNum - 1;
                                        EditJudgeIndex = ScoresIndex;
                                    }
                                }
                                else
                                {
                                    GUILayout.Label("    " + ResultsStr, labelStyle);
                                }
                            }
                            else if (bInEditingMode && (ScoresIndex == 0 || tdd.Data.RoutineScores.GetResultsString(ScoresIndex - 1, CurCat, true).Length > 0))
                            {
                                if (GUILayout.Button("    Enter New Scores", EditStyle))
                                {
                                    bIsEditing     = true;
                                    EditPoolIndex  = PoolIndex;
                                    EditTeamIndex  = TeamNum - 1;
                                    EditJudgeIndex = ScoresIndex;
                                }
                            }
                        }
                    }

                    ++PoolIndex;
                }
            }
            else
            {
                TeamData Data = Global.AllData.AllDivisions[CurDivIndex].Rounds[CurRoundIndex].Pools[EditPoolIndex].Teams[EditTeamIndex].Data;
                if (CatCombo.GetSelectedItemIndex() == 0)
                {
                    if (EditAIData == null)
                    {
                        AIData JData = null;
                        if (EditJudgeIndex >= Data.RoutineScores.AIResults.Count)
                        {
                            EditAIData      = new AIData((EDivision)CurDivIndex, (ERound)CurRoundIndex, (EPool)EditPoolIndex, EditTeamIndex);
                            bEditAddNewData = true;
                            EditJudgeName   = "Judge Name";
                        }
                        else
                        {
                            JData      = Data.RoutineScores.AIResults[EditJudgeIndex];
                            EditAIData = new AIData(JData);
                        }
                    }

                    GUILayout.BeginHorizontal();

                    if (EditAIData.JudgeNameId == -1)
                    {
                        GUILayout.Label("Judge:", labelStyle);
                        EditJudgeName = GUILayout.TextField(EditJudgeName);

                        char[]   Seperators = new char[] { ',', ' ' };
                        string[] Splits     = EditJudgeName.Split(Seperators, System.StringSplitOptions.RemoveEmptyEntries);
                        NameData JudgeName  = null;
                        if (Splits.Length == 2)
                        {
                            JudgeName = NameDatabase.FindInDatabase(Splits[0], Splits[1]);

                            if (JudgeName == null)
                            {
                                JudgeName = NameDatabase.FindInDatabase(Splits[1], Splits[0]);
                            }
                        }

                        if (JudgeName != null)
                        {
                            EditAIData.JudgeNameId = JudgeName.Id;
                        }
                    }
                    else
                    {
                        GUILayout.Label(NameDatabase.FindInDatabase(EditAIData.JudgeNameId).DisplayName + ": ", labelStyle);
                    }

                    GUILayout.Label("V: ", labelStyle);
                    float.TryParse(GUILayout.TextField(EditAIData.Variety.ToString()), out EditAIData.Variety);
                    GUILayout.Label("T: ", labelStyle);
                    float.TryParse(GUILayout.TextField(EditAIData.Teamwork.ToString()), out EditAIData.Teamwork);
                    GUILayout.Label("M: ", labelStyle);
                    float.TryParse(GUILayout.TextField(EditAIData.Music.ToString()), out EditAIData.Music);
                    GUILayout.Label("Fm: ", labelStyle);
                    float.TryParse(GUILayout.TextField(EditAIData.Form.ToString()), out EditAIData.Form);
                    GUILayout.Label("Fw: ", labelStyle);
                    float.TryParse(GUILayout.TextField(EditAIData.Flow.ToString()), out EditAIData.Flow);
                    GUILayout.Label("G: ", labelStyle);
                    float.TryParse(GUILayout.TextField(EditAIData.General.ToString()), out EditAIData.General);
                    GUILayout.Label("Total: " + EditAIData.GetTotalPoints().ToString(), labelStyle);

                    GUILayout.EndHorizontal();
                }
                else if (CatCombo.GetSelectedItemIndex() == 1)
                {
                    if (EditExData == null)
                    {
                        ExData JData = null;
                        if (EditJudgeIndex >= Data.RoutineScores.ExResults.Count)
                        {
                            EditExData      = new ExData((EDivision)CurDivIndex, (ERound)CurRoundIndex, (EPool)EditPoolIndex, EditTeamIndex);
                            bEditAddNewData = true;
                            EditJudgeName   = "Judge Name";
                        }
                        else
                        {
                            JData      = Data.RoutineScores.ExResults[EditJudgeIndex];
                            EditExData = new ExData(JData);
                        }
                    }

                    GUILayout.BeginHorizontal();

                    if (EditExData.JudgeNameId == -1)
                    {
                        GUILayout.Label("Judge:", labelStyle);
                        EditJudgeName = GUILayout.TextField(EditJudgeName);

                        char[]   Seperators = new char[] { ',', ' ' };
                        string[] Splits     = EditJudgeName.Split(Seperators, System.StringSplitOptions.RemoveEmptyEntries);
                        NameData JudgeName  = null;
                        if (Splits.Length == 2)
                        {
                            JudgeName = NameDatabase.FindInDatabase(Splits[0], Splits[1]);

                            if (JudgeName == null)
                            {
                                JudgeName = NameDatabase.FindInDatabase(Splits[1], Splits[0]);
                            }
                        }

                        if (JudgeName != null)
                        {
                            EditExData.JudgeNameId = JudgeName.Id;
                        }
                    }
                    else
                    {
                        GUILayout.Label(NameDatabase.FindInDatabase(EditExData.JudgeNameId).DisplayName + ": ", labelStyle);
                    }

                    GUILayout.Label(".1: ", labelStyle);
                    int.TryParse(GUILayout.TextField(EditExData.Point1Count.ToString()), out EditExData.Point1Count);
                    GUILayout.Label(".2: ", labelStyle);
                    int.TryParse(GUILayout.TextField(EditExData.Point2Count.ToString()), out EditExData.Point2Count);
                    GUILayout.Label(".3: ", labelStyle);
                    int.TryParse(GUILayout.TextField(EditExData.Point3Count.ToString()), out EditExData.Point3Count);
                    GUILayout.Label(".5: ", labelStyle);
                    int.TryParse(GUILayout.TextField(EditExData.Point5Count.ToString()), out EditExData.Point5Count);
                    GUILayout.Label("Total: " + EditExData.GetTotalPoints().ToString(), labelStyle);

                    GUILayout.EndHorizontal();
                }
                else if (CatCombo.GetSelectedItemIndex() == 2)
                {
                    if (EditDiffData == null)
                    {
                        DiffData JData = null;
                        if (EditJudgeIndex >= Data.RoutineScores.DiffResults.Count)
                        {
                            EditDiffData    = new DiffData(20, (EDivision)CurDivIndex, (ERound)CurRoundIndex, (EPool)EditPoolIndex, EditTeamIndex);
                            bEditAddNewData = true;
                            EditJudgeName   = "Judge Name";
                        }
                        else
                        {
                            JData        = Data.RoutineScores.DiffResults[EditJudgeIndex];
                            EditDiffData = new DiffData(JData);
                        }
                    }

                    GUILayout.BeginVertical();

                    GUILayout.BeginHorizontal();
                    GUILayout.Label("Number of Scores: ", labelStyle);
                    int.TryParse(GUILayout.TextField(EditDiffData.NumScores.ToString()), out EditDiffData.NumScores);
                    EditDiffData.NumScores = Mathf.Clamp(EditDiffData.NumScores, 0, 20);
                    GUILayout.EndHorizontal();

                    GUILayout.BeginHorizontal();

                    if (EditDiffData.JudgeNameId == -1)
                    {
                        GUILayout.Label("Judge:", labelStyle);
                        EditJudgeName = GUILayout.TextField(EditJudgeName);

                        char[]   Seperators = new char[] { ',', ' ' };
                        string[] Splits     = EditJudgeName.Split(Seperators, System.StringSplitOptions.RemoveEmptyEntries);
                        NameData JudgeName  = null;
                        if (Splits.Length == 2)
                        {
                            JudgeName = NameDatabase.FindInDatabase(Splits[0], Splits[1]);

                            if (JudgeName == null)
                            {
                                JudgeName = NameDatabase.FindInDatabase(Splits[1], Splits[0]);
                            }
                        }

                        if (JudgeName != null)
                        {
                            EditDiffData.JudgeNameId = JudgeName.Id;
                        }
                    }
                    else
                    {
                        GUILayout.Label(NameDatabase.FindInDatabase(EditDiffData.JudgeNameId).DisplayName + ": ", labelStyle);
                    }

                    for (int i = 0; i < EditDiffData.NumScores; ++i)
                    {
                        string DiffStr = GUILayout.TextField(EditDiffData.DiffScores[i].ToString() + EditDiffData.GetConsecString(i));
                        if (DiffStr.Contains("-"))
                        {
                            DiffStr = DiffStr.Replace("-", "");
                            EditDiffData.ConsecScores[i] = -1;
                        }
                        else if (DiffStr.Contains("+"))
                        {
                            DiffStr = DiffStr.Replace("+", "");
                            EditDiffData.ConsecScores[i] = 1;
                        }
                        else
                        {
                            EditDiffData.ConsecScores[i] = 0;
                        }
                        float.TryParse(DiffStr, out EditDiffData.DiffScores[i]);
                    }

                    GUILayout.EndHorizontal();
                    GUILayout.EndVertical();
                }
            }

            GUILayout.EndVertical();
            GUILayout.EndScrollView();

            if (CatCombo.GetSelectedItemIndex() != 3)
            {
                if (!bInEditingMode && GUILayout.Button("Enter Edit Mode", GUILayout.Height(Screen.height * .1f)))
                {
                    bInEditingMode = true;
                }
                else if (bInEditingMode)
                {
                    if (bIsEditing)
                    {
                        GUILayout.BeginHorizontal();

                        if (GUILayout.Button("Delete Score", GUILayout.Height(Screen.height * .1f)))
                        {
                            TeamData Data = Global.AllData.AllDivisions[CurDivIndex].Rounds[CurRoundIndex].Pools[EditPoolIndex].Teams[EditTeamIndex].Data;
                            if (CatCombo.GetSelectedItemIndex() == 0)
                            {
                                if (!bEditAddNewData && EditJudgeIndex < Data.RoutineScores.AIResults.Count)
                                {
                                    Data.RoutineScores.AIResults[EditJudgeIndex] = new AIData();
                                }
                                EditAIData = null;
                            }
                            else if (CatCombo.GetSelectedItemIndex() == 1)
                            {
                                if (!bEditAddNewData && EditJudgeIndex < Data.RoutineScores.ExResults.Count)
                                {
                                    Data.RoutineScores.ExResults[EditJudgeIndex] = new ExData();
                                }
                                EditExData = null;
                            }
                            else if (CatCombo.GetSelectedItemIndex() == 2)
                            {
                                if (!bEditAddNewData && EditJudgeIndex < Data.RoutineScores.DiffResults.Count)
                                {
                                    Data.RoutineScores.DiffResults[EditJudgeIndex] = new DiffData();
                                }
                                EditDiffData = null;
                            }

                            bIsEditing      = false;
                            EditAIData      = null;
                            EditExData      = null;
                            EditDiffData    = null;
                            bEditAddNewData = false;

                            Global.AllData.Save();
                        }

                        if (GUILayout.Button("Discard Changes", GUILayout.Height(Screen.height * .1f)))
                        {
                            bIsEditing      = false;
                            EditAIData      = null;
                            EditExData      = null;
                            EditDiffData    = null;
                            bEditAddNewData = false;
                        }

                        if (GUILayout.Button("Save Changes", GUILayout.Height(Screen.height * .1f)))
                        {
                            TeamData Data = Global.AllData.AllDivisions[CurDivIndex].Rounds[CurRoundIndex].Pools[EditPoolIndex].Teams[EditTeamIndex].Data;
                            if (CatCombo.GetSelectedItemIndex() == 0)
                            {
                                if (bEditAddNewData)
                                {
                                    Data.RoutineScores.AIResults.Add(new AIData(EditAIData));
                                }
                                else
                                {
                                    Data.RoutineScores.AIResults[EditJudgeIndex] = new AIData(EditAIData);
                                }
                                EditAIData = null;
                            }
                            else if (CatCombo.GetSelectedItemIndex() == 1)
                            {
                                if (bEditAddNewData)
                                {
                                    Data.RoutineScores.ExResults.Add(new ExData(EditExData));
                                }
                                else
                                {
                                    Data.RoutineScores.ExResults[EditJudgeIndex] = new ExData(EditExData);
                                }
                                EditExData = null;
                            }
                            else if (CatCombo.GetSelectedItemIndex() == 2)
                            {
                                if (bEditAddNewData)
                                {
                                    Data.RoutineScores.DiffResults.Add(new DiffData(EditDiffData));
                                }
                                else
                                {
                                    Data.RoutineScores.DiffResults[EditJudgeIndex] = new DiffData(EditDiffData);
                                }
                                EditDiffData = null;
                            }

                            bIsEditing      = false;
                            bEditAddNewData = false;

                            Global.AllData.Save();
                        }
                        GUILayout.EndHorizontal();
                    }
                    else if (GUILayout.Button("Exit Edit Mode", GUILayout.Height(Screen.height * .1f)))
                    {
                        bInEditingMode = false;
                    }
                }
            }

            GUILayout.EndVertical();
            GUILayout.EndArea();
        }
    }