/// <summary>
    /// Posts the player name, player university team to the corresponding account using account ID
    /// </summary>
    /// <returns></returns>
    IEnumerator UploadPlayerRoutine()
    {
        WWWForm form = new WWWForm();

        form.AddField("player_name", GameSaveUtility.GetPlayerName());
        form.AddField("player_team", GameSaveUtility.GetUniversity());
        form.AddField("player_ID", GameSaveUtility.GetID());

        using (UnityWebRequest www = UnityWebRequest.Post(path + "addplayer.php", form))
        {
            yield return(www.SendWebRequest());

            if (www.isNetworkError || www.isHttpError)
            {
                Debug.Log(www.error);
            }
            else
            {
                Debug.Log("Form upload complete!");
                StringBuilder sb = new StringBuilder();
                foreach (System.Collections.Generic.KeyValuePair <string, string> dict in www.GetResponseHeaders())
                {
                    sb.Append(dict.Key).Append(": \t[").Append(dict.Value).Append("]\n");
                }

                // Print Headers
#if (UNITY_EDITOR)
                Debug.Log(sb.ToString());
#endif

                if (www.GetResponseHeaders().Count > 0 && www.GetResponseHeaders().ContainsKey("userAdded"))
                {
                    if (www.GetResponseHeaders()["userAdded"] == "true")
                    {
                        Debug.Log("User added succesful");
                    }
                    else
                    {
                        Debug.Log("User added failed");
                    }
                }
                else if (www.GetResponseHeaders().Count > 0 && www.GetResponseHeaders().ContainsKey("userUpdated"))
                {
                    if (www.GetResponseHeaders()["userUpdated"] == "true")
                    {
                        Debug.Log("User updated succesful");
                    }
                    else
                    {
                        Debug.Log("User updated failed");
                    }
                }
            }
        }
    }
    /// <summary>
    /// Posts the level objective name, chapter, mode, difficulty, score and star to the PlayerLevels table where it matches the player ID
    /// Called at the end of every level - updated simulataneously with the local storage of the level scores
    /// </summary>
    /// <returns></returns>
    IEnumerator StoreLevelDataRoutine(string objective, int score, int star)
    {
        WWWForm form = new WWWForm();

        form.AddField("player_ID", GameSaveUtility.GetID());
        form.AddField("level_name", objective);
        form.AddField("level_chapter", ObjectiveUtility.CurrentChapter.ToString());
        form.AddField("level_mode", ObjectiveUtility.CurrentGameMode.ToString());
        form.AddField("level_difficulty", ObjectiveUtility.CurrentDifficulty.ToString());
        form.AddField("level_score", score);
        form.AddField("level_star", star);

        using (UnityWebRequest www = UnityWebRequest.Post(path + "storelevels.php", form))
        {
            yield return(www.SendWebRequest());

            if (www.isNetworkError || www.isHttpError)
            {
                Debug.Log(www.error);
            }
            else
            {
                Debug.Log("Form upload complete!");
                StringBuilder sb = new StringBuilder();
                foreach (System.Collections.Generic.KeyValuePair <string, string> dict in www.GetResponseHeaders())
                {
                    sb.Append(dict.Key).Append(": \t[").Append(dict.Value).Append("]\n");
                }

                // Print Headers
#if (UNITY_EDITOR)
                Debug.Log(sb.ToString());
                Debug.Log(www.downloadHandler.text);
#endif

                if (www.GetResponseHeaders().Count > 0 && www.GetResponseHeaders().ContainsKey("levelUpdated"))
                {
                    if (www.GetResponseHeaders()["levelUpdated"] == "true")
                    {
                        Debug.Log("Level score updating succesfull");
                        // Print Body
#if (UNITY_EDITOR)
                        Debug.Log(www.downloadHandler.text);
#endif
                    }
                    else
                    {
                        Debug.Log("Score saving failed");
                    }
                }
            }
        }
    }
    /// <summary>
    /// Updates the score the individual level by POSTing the player ID, level objective, level chapter, level difficulty, level mode to identify corect row
    /// POSTs and score to store.
    /// </summary>
    /// <returns></returns>
    IEnumerator UpdateLevelDataRoutine(bool star)
    {
        WWWForm form = new WWWForm();

        form.AddField("player_id", GameSaveUtility.GetID());
        form.AddField("level_objective", ObjectiveUtility.CurrentObjective);
        form.AddField("level_chapter", ObjectiveUtility.CurrentChapter.ToString());
        form.AddField("level_difficulty", ObjectiveUtility.CurrentDifficulty.ToString());
        form.AddField("level_mode", ObjectiveUtility.CurrentGameMode.ToString());
        form.AddField("level_score", ObjectiveUtility.Score);
        if (star)
        {
            form.AddField("level_star", ObjectiveUtility.EarnedStars); // if a star is earned, POST the star earned
        }

        using (UnityWebRequest www = UnityWebRequest.Post(path + "storelevels.php", form))
        {
            yield return(www.SendWebRequest());

            if (www.isNetworkError || www.isHttpError)
            {
                Debug.Log(www.error);
            }
            else
            {
                Debug.Log("Form upload complete!");
                StringBuilder sb = new StringBuilder();
                foreach (System.Collections.Generic.KeyValuePair <string, string> dict in www.GetResponseHeaders())
                {
                    sb.Append(dict.Key).Append(": \t[").Append(dict.Value).Append("]\n");
                }

                // Print Headers
#if (UNITY_EDITOR)
                Debug.Log(sb.ToString());
#endif

                if (www.GetResponseHeaders().Count > 0 && www.GetResponseHeaders().ContainsKey("storeLevel"))
                {
                    if (www.GetResponseHeaders()["storeLevel"] == "true")
                    {
                        Debug.Log("Level score successfully updated");
                    }
                    else
                    {
                        Debug.Log("Level score failed up upate");
                    }
                }
            }
        }
    }
    /// <summary>
    /// Posts the earned stars to the player university using the account ID
    /// </summary>
    /// <returns></returns>
    IEnumerator UpdateLeaderboardRoutine()
    {
        WWWForm form = new WWWForm();

        form.AddField("player_id", GameSaveUtility.GetID());
        form.AddField("player_team", GameSaveUtility.GetUniversity());
        form.AddField("earnedStars", ObjectiveUtility.EarnedStars); // php will add the earned stars to the total stars of the player

        using (UnityWebRequest www = UnityWebRequest.Post(path + "updateleaderboard.php", form))
        {
            yield return(www.SendWebRequest());

            if (www.isNetworkError || www.isHttpError)
            {
                Debug.Log(www.error);
            }
            else
            {
                Debug.Log("Form upload complete!");
                StringBuilder sb = new StringBuilder();
                foreach (System.Collections.Generic.KeyValuePair <string, string> dict in www.GetResponseHeaders())
                {
                    sb.Append(dict.Key).Append(": \t[").Append(dict.Value).Append("]\n");
                }

                // Print Headers
#if (UNITY_EDITOR)
                Debug.Log(sb.ToString());
#endif

                if (www.GetResponseHeaders().Count > 0 && www.GetResponseHeaders().ContainsKey("leaderboardUpdated"))
                {
                    if (www.GetResponseHeaders()["leaderboardUpdated"] == "true")
                    {
                        Debug.Log("Team score updating succesfully");
                        // Print Body
#if (UNITY_EDITOR)
                        Debug.Log(www.downloadHandler.text);
#endif
                    }
                    else
                    {
                        Debug.Log("Score saving failed");
                    }
                }
            }
        }
    }
    /// <summary>
    /// Posts the player's money, team, first time boolean and name to the PlayerInfo database where it matches the player ID
    /// </summary>
    /// <param name="logout">Determines if the coroutine is being called alongside a logout action. If true, delete all player prefs</param>
    /// <returns></returns>
    IEnumerator StorePlayerDataRoutine(bool logout)
    {
        WWWForm form = new WWWForm();

        form.AddField("player_id", GameSaveUtility.GetID());
        form.AddField("player_money", GameSaveUtility.GetTotalBalance());
        if (GameSaveUtility.GetChapProgress(1))
        {
            form.AddField("progress1", 1);
        }
        if (GameSaveUtility.GetChapProgress(2))
        {
            form.AddField("progress2", 1);
        }
        if (GameSaveUtility.GetChapProgress(3))
        {
            form.AddField("progress3", 1);
        }


        using (UnityWebRequest www = UnityWebRequest.Post(path + "updateplayerinfo.php", form))
        {
            yield return(www.SendWebRequest());

            if (www.isNetworkError || www.isHttpError)
            {
                Debug.Log(www.error);
            }
            else
            {
                Debug.Log("Form upload complete!");
                StringBuilder sb = new StringBuilder();
                foreach (System.Collections.Generic.KeyValuePair <string, string> dict in www.GetResponseHeaders())
                {
                    sb.Append(dict.Key).Append(": \t[").Append(dict.Value).Append("]\n");
                }

                // Print Headers
#if (UNITY_EDITOR)
                Debug.Log(sb.ToString());
#endif

                if (www.GetResponseHeaders().Count > 0 && www.GetResponseHeaders().ContainsKey("userUpdated"))
                {
                    if (www.GetResponseHeaders()["userUpdated"] == "true")
                    {
                        Debug.Log("Player data saved succesfully");
                        if (logout)
                        {
                            Debug.Log("LOGOUT");
                            GameSaveUtility.Reset();   // if updating the player is called during a logout action, delete all player pref local storage
                            canvasManager.OpenStart(); // makes sure the page when logging back in starts with the start menu and not settings
                        }
                        // Print Body
#if (UNITY_EDITOR)
                        Debug.Log(www.downloadHandler.text);
#endif
                    }
                    else
                    {
                        Debug.Log("Player data saving failed");
                        Debug.Log(www.downloadHandler.text);
                    }
                }
            }
        }
    }
    /// <summary>
    /// Using the Signup Canvas input fields to post account name, account password and email to PHP. Returns the account ID to store in GameSaveUtility
    /// </summary>
    /// <returns></returns>
    IEnumerator LoginUserRoutione()
    {
        WWWForm form = new WWWForm();

        form.AddField("account_name", loginCanvas.transform.GetChild(0).GetComponent <InputField>().text);
        form.AddField("account_pass", loginCanvas.transform.GetChild(1).GetComponent <InputField>().text);

        string[] dbText;
        bool     successfulLogin = false;

        using (UnityWebRequest www = UnityWebRequest.Post(path + "verifylogin.php", form))
        {
            yield return(www.SendWebRequest());

            if (www.isNetworkError || www.isHttpError)
            {
                Debug.Log(www.error);
            }
            else
            {
                Debug.Log("Form load complete!");
                StringBuilder sb = new StringBuilder();
                foreach (System.Collections.Generic.KeyValuePair <string, string> dict in www.GetResponseHeaders())
                {
                    sb.Append(dict.Key).Append(": \t[").Append(dict.Value).Append("]\n");
                }

                // Print Headers
#if (UNITY_EDITOR)
                Debug.Log(sb.ToString());
#endif

                if (www.GetResponseHeaders().Count > 0 && www.GetResponseHeaders().ContainsKey("LoginSucceed"))
                {
                    if (www.GetResponseHeaders()["LoginSucceed"] == "true")
                    {
                        Debug.Log("Login succesful");
                        dbText = www.downloadHandler.text.Split('\n'); // splits all echoed text from PHP
                        status = true;
                        GameSaveUtility.SaveID(int.Parse(dbText[1]));  // know that the ID is echoed on the 2nd line of the db text body"
                        successfulLogin = true;                        // boolean trigger to retrieve all player data
                    }
                    else
                    {
                        loginPopup.transform.GetChild(0).GetComponent <Text>().text = "ERROR: Login";
                        string bodyError = "Login failed: ";
                        if (www.GetResponseHeaders().ContainsKey("PasswordFail"))
                        {
                            bodyError += "Inccorect password.\n";
                        }
                        if (www.GetResponseHeaders().ContainsKey("AccountFail"))
                        {
                            bodyError += "Inccorect account name.\n";
                        }
                        loginPopup.transform.GetChild(1).GetComponent <Text>().text = bodyError;
                        loginPopup.SetActive(true);
#if (UNITY_EDITOR)
                        Debug.Log(www.downloadHandler.text);
#endif
                    }
                }
                else
                {
                    // Failed
                    loginPopup.transform.GetChild(0).GetComponent <Text>().text = "ERROR: Login";
                    loginPopup.transform.GetChild(1).GetComponent <Text>().text = "Unable to connect to server.";
                    loginPopup.SetActive(true);
#if (UNITY_EDITOR)
                    Debug.Log(www.downloadHandler.text);
#endif
                }
            }
        }

        // if successful login, retrieve all player data and store to PlayerPrefs
        if (successfulLogin)
        {
            form = new WWWForm();
            int playerID = GameSaveUtility.GetID();
            form.AddField("player_ID", playerID);
            form.AddField("request", "getlevels");

            using (UnityWebRequest www = UnityWebRequest.Post(path + "getplayerdata.php", form))
            {
                yield return(www.SendWebRequest());

                if (www.isNetworkError || www.isHttpError)
                {
                    Debug.Log(www.error);
                }
                else
                {
                    Debug.Log("Form load complete!");
                    StringBuilder sb = new StringBuilder();
                    foreach (System.Collections.Generic.KeyValuePair <string, string> dict in www.GetResponseHeaders())
                    {
                        sb.Append(dict.Key).Append(": \t[").Append(dict.Value).Append("]\n");
                    }

                    // Print Headers
#if (UNITY_EDITOR)
                    Debug.Log(sb.ToString());
#endif

                    if (www.GetResponseHeaders().Count > 0)
                    {
                        Debug.Log(www.downloadHandler.text);
                        dbText = www.downloadHandler.text.Split('\n');
                        MenuData menuData = JsonUtility.FromJson <MenuData>(dbText[0]);
                        menuData.StoreLevels();                                               // store level information back into local PlayerPrefs
                        string     parsePlayer = dbText[1].Replace("[", "").Replace("]", ""); // format the player text outside of the array to be recognized as a PlayerData object JSON
                        PlayerData playerData  = JsonUtility.FromJson <PlayerData>(parsePlayer);
                        playerData.SetPlayerInfo();                                           // store player information back into the local PlayerPrefs
                    }
                    else
                    {
                        // Failed
                        loginPopup.transform.GetChild(0).GetComponent <Text>().text = "ERROR: Login";
                        loginPopup.transform.GetChild(1).GetComponent <Text>().text = "Unable to connect to server.";
                        loginPopup.SetActive(true);
#if (UNITY_EDITOR)
                        Debug.Log(www.downloadHandler.text);
#endif
                    }
                }
            }

            if (successfulLogin)
            {
                canvasManager.SwitchCanvas("menus");
            }
        }
    }