using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
using UnityEngine.UI;
using PlayFab;
using PlayFab.ClientModels;
using PlayFab.Json;
using SimpleJSON;

public static class PlayfabCloudScriptAPI {

    public static void LogErrorToServer(string _message) {
        PlayFabClientAPI.ExecuteCloudScript(new ExecuteCloudScriptRequest() {
            FunctionName = "logError", 
            FunctionParameter = new {
                message = _message
            },
            GeneratePlayStreamEvent = true, // REMOVE?
        }, OnSuccessCallback, OnErrorShared);
    }

    public static void AfterRegistration(string referredBy) {
        PlayFabClientAPI.ExecuteCloudScript(new ExecuteCloudScriptRequest() {
            FunctionName = "afterRegistration", // Function name to be used in Cloud Script handles.createNewTeacherAlphaMatch
            FunctionParameter = new {
                referred_by = referredBy
            }, // The parameter provided to your function
            GeneratePlayStreamEvent = true, // Optional - Shows this event in PlayStream
        }, OnSuccessfulShare, OnErrorShared);
    }

    private static void OnSuccessfulShare(ExecuteCloudScriptResult result) {
        // Cloud Script returns arbitrary results, so you have to evaluate them one step and one parameter at a time
        Debug.Log("You were referred");
    }

    public static void SetCurrentPlayerAsCreatorCorps() {
        PlayFabClientAPI.ExecuteCloudScript(new ExecuteCloudScriptRequest() {
            FunctionName = "setCurrentPlayerAsCreatorCorps",
            FunctionParameter = new {

            },
            GeneratePlayStreamEvent = true,
        }, OnSuccessCallback, OnErrorShared);
    }

    public static void UpdatePlayerMoodleData(string moodleUsername) {
        if (moodleUsername == null) {
            return;
        }

        PlayFabClientAPI.ExecuteCloudScript(new ExecuteCloudScriptRequest() {
            FunctionName = "updatePlayerMoodleData",
            FunctionParameter = new {
                moodle_username = moodleUsername,
            },
            GeneratePlayStreamEvent = true,
        }, OnSuccessCallback, OnErrorShared);
    }

    /*
    public static void UpdateBestTimes(string trackName, int lapTime, int raceTime)
    {
        if (trackName == null || lapTime == 0 || raceTime == 0)
        {
            Debug.Log("Race Incomplete");
            return;
        }

        PlayFabClientAPI.ExecuteCloudScript(new ExecuteCloudScriptRequest()
        {
            FunctionName = "updateBestTimes",
            FunctionParameter = new
            {
                track_name = trackName,
                lap_time = lapTime,
                race_time = raceTime,
            },
            GeneratePlayStreamEvent = true,
        }, OnSuccessCallback, OnErrorShared);
    }
    */

    public static void SetRaceStats(string winType)
    {
        PlayFabClientAPI.ExecuteCloudScript(new ExecuteCloudScriptRequest()
        {
            FunctionName = "setRaceStats",
            FunctionParameter = new
            {
                win_type = winType
            },
            GeneratePlayStreamEvent = true,
        }, OnSuccessfulSomething, OnErrorShared);
    }

    public static void SetRaceComplete()
    {
        PlayFabClientAPI.ExecuteCloudScript(new ExecuteCloudScriptRequest()
        {
            FunctionName = "setRaceComplete",
            FunctionParameter = new
            {
                blank_var = "none"
            },
            GeneratePlayStreamEvent = true,
        }, OnSuccessfulSomething, OnErrorShared);
    }

    public static void UpdateBestTimes(string trackName, float lapTime, float raceTime)
    {

        if (trackName == null || lapTime == 0 || raceTime == 0)
        {
            Debug.Log("Race Incomplete");
            return;
        }

        string bestLapTime = trackName + "_bestLapTime";
        string bestRaceTime = trackName + "_bestRaceTime";

        var request = new UpdateUserDataRequest
        {
            Data = new Dictionary<string, string> {
                { bestLapTime, lapTime.ToString() },
                { bestRaceTime, raceTime.ToString() }
            }
        };
        PlayFabClientAPI.UpdateUserData(request, null, updateErrors => Debug.LogError("Error on request: " + updateErrors.ErrorMessage));
    }

    private static void OnSuccessCallback(ExecuteCloudScriptResult result) {
        Debug.Log(result.FunctionResult);
    }

    private static void OnErrorShared(PlayFabError error) {
        Debug.Log("Error from PlayfabCloudScriptAPI.OnErrorShared");
        ErrorLogger.WriteToLog(error.GenerateErrorReport());
        Debug.Log(error.GenerateErrorReport());
    }

    public static void SetCompletedMatch(int completedMatches) {
        PlayFabClientAPI.ExecuteCloudScript(new ExecuteCloudScriptRequest() {
            FunctionName = "setCompletedMatches",
            FunctionParameter = new {
                completed_matches = completedMatches
            },
            GeneratePlayStreamEvent = true,
        }, OnSuccessfulSomething, OnErrorShared);
    }

    public static void SetWins(int wins) {
        PlayFabClientAPI.ExecuteCloudScript(new ExecuteCloudScriptRequest() {
            FunctionName = "setWins",
            FunctionParameter = new {
                win_number = wins
            },
            GeneratePlayStreamEvent = true,
        }, OnSuccessfulSomething, OnErrorShared);
    }

    public static void SetDefeats(int defeats) {
        PlayFabClientAPI.ExecuteCloudScript(new ExecuteCloudScriptRequest() {
            FunctionName = "setDefeats",
            FunctionParameter = new {
                defeat_number = defeats
            },
            GeneratePlayStreamEvent = true,
        }, OnSuccessfulSomething, OnErrorShared);
    }

    private static void OnSuccessfulSomething(ExecuteCloudScriptResult result) {
        Debug.Log("You have partaken in a match or level and may or may not have completed it!");
    }
}

