using System.Collections;
using System.Collections.Generic;
using System.Text;
using UnityEngine;
using UnityEngine.SceneManagement;
using UnityEngine.UI;

public class TestAdok : MonoBehaviour
{

    public ADokManager adok;
    public Text txtResult;
    public Text txtResult2;


    public InputField txtPageNo;

    public GameObject pnlReg;
    public GameObject pnlData;
    public GameObject pnlLead;
    public GameObject pnlAdv;
    public GameObject pnlLeagues;
    public GameObject pnlMain;


    public GameObject refLeabueBtn;
    List<GameObject> CreatedLeagues;


    public GameObject ReadAccessBtn;
    public GameObject WriteAccessBtn;
    public GameObject CameraAccessBtn;
    public GameObject PhoneStateStateBtn;
    [Space]
    [Space]

    [Header("PermitionGranted")]
    public ColorBlock PermitionGranted;
    [Space]
    [Space]

    [Header("NoPermition")]
    public ColorBlock NoPermition;


    public Image AvatarImg;
    // Use this for initialization
    void Start()
    {
        Screen.sleepTimeout = SleepTimeout.NeverSleep;
        CreatedLeagues = new List<GameObject>();
        adok = GameObject.FindObjectOfType<ADokManager>();
        adok.OnVideoAdvShowEnd += Adok_OnVideoAdvShowEnd;

        adok.OnSyncChange += Adok_OnSyncChange;

        adok.OnLeaderChange += Adok_OnLeaderChange;

        adok.OnSyncGetChange += Adok_OnSyncGetChange;

        adok.OnMyLeaderChange += Adok_OnMyLeaderChange;
        adok.OnGetDateChange += Adok_OnGetDateChange;
        adok.OnGetTimeChange += Adok_OnGetTimeChange;
        adok.OnMultiUserChange += Adok_OnMultiUserChange;


        adok.OnGetLeagues += Adok_OnGetLeagues;
        CheckAllPermition();
    }


    public void OnApplicationFocus(bool pause)
    {
        CheckAllPermition();
    }

    public void CheckAllPermition()
    {
        if (!adok.CheckReadAccessPermition())
        {
            ReadAccessBtn.GetComponent<Button>().colors = NoPermition;
        }
        else
        {
            ReadAccessBtn.GetComponent<Button>().colors = PermitionGranted;
        }

        if (!adok.CheckWriteAccessPermition())
        {
            WriteAccessBtn.GetComponent<Button>().colors = NoPermition;
        }
        else
        {
            WriteAccessBtn.GetComponent<Button>().colors = PermitionGranted;
        }

        if (!adok.CheckCameraAccessPermition())
        {
            CameraAccessBtn.GetComponent<Button>().colors = NoPermition;
        }
        else
        {
            CameraAccessBtn.GetComponent<Button>().colors = PermitionGranted;
        }

        if (!adok.CheckPhoneStatePermition())
        {
            PhoneStateStateBtn.GetComponent<Button>().colors = NoPermition;
        }
        else
        {
            PhoneStateStateBtn.GetComponent<Button>().colors = PermitionGranted;
        }
    }

    public void RequesReadAccessPermition()
    {
        if (!adok.CheckReadAccessPermition())
        {
            ReadAccessBtn.GetComponent<Button>().colors = NoPermition;
            adok.RequestReadAccessPermition();
        }
        else
        {
            ReadAccessBtn.GetComponent<Button>().colors = PermitionGranted;
        }
    }

    public void RequesWriteAccessPermition()
    {
        if (!adok.CheckWriteAccessPermition())
        {
            WriteAccessBtn.GetComponent<Button>().colors = NoPermition;
            adok.RequestWriteAccessPermition();
        }
        else
        {
            WriteAccessBtn.GetComponent<Button>().colors = PermitionGranted;
        }
    }

    public void RequesCameraAccessPermition()
    {
        if (!adok.CheckCameraAccessPermition())
        {
            CameraAccessBtn.GetComponent<Button>().colors = NoPermition;
            adok.RequestCameraAccessPermition();
        }
        else
        {
            CameraAccessBtn.GetComponent<Button>().colors = PermitionGranted;
        }
    }

    public void RequesPhoneStateAccessPermition()
    {
        if (!adok.CheckPhoneStatePermition())
        {
            PhoneStateStateBtn.GetComponent<Button>().colors = NoPermition;
            adok.RequestPhoneStatePermition();
        }
        else
        {
            PhoneStateStateBtn.GetComponent<Button>().colors = PermitionGranted;
        }
    }

    public void GoToMain()
    {
        pnlReg.SetActive(false);
        pnlData.SetActive(false);
        pnlLead.SetActive(false);
        pnlAdv.SetActive(false);
        pnlLeagues.SetActive(false);
        pnlMain.SetActive(true);
    }

    public void GoToReg()
    {
        pnlReg.SetActive(true);
        pnlData.SetActive(false);
        pnlLead.SetActive(false);
        pnlAdv.SetActive(false);
        pnlLeagues.SetActive(false);
        pnlMain.SetActive(false);
    }

    public void GoToData()
    {
        pnlReg.SetActive(false);
        pnlData.SetActive(true);
        pnlLead.SetActive(false);
        pnlAdv.SetActive(false);
        pnlLeagues.SetActive(false);
        pnlMain.SetActive(false);
    }

    public void GoToLead()
    {
        pnlReg.SetActive(false);
        pnlData.SetActive(false);
        pnlLead.SetActive(true);
        pnlAdv.SetActive(false);
        pnlLeagues.SetActive(false);
        pnlMain.SetActive(false);
    }

    public void GoToAdv()
    {
        pnlReg.SetActive(false);
        pnlData.SetActive(false);
        pnlLead.SetActive(false);
        pnlAdv.SetActive(true);
        pnlLeagues.SetActive(false);
        pnlMain.SetActive(false);
    }

    public void GoToLeagues()
    {
        pnlReg.SetActive(false);
        pnlData.SetActive(false);
        pnlLead.SetActive(false);
        pnlAdv.SetActive(false);
        pnlLeagues.SetActive(true);
        pnlMain.SetActive(false);
    }

    public void CheckPermittion()
    {
        bool b = adok.CheckFilePermission();
        txtResult.text = "CheckFilePermission: " + b.ToString();
        if (!b)
        {
            adok.ShowAccessFilePermittion();
        }
    }

    private void Adok_OnGetLeagues(ADokManager.GetLeagueResult getLeagueResultResult)
    {
        txtResult.text = "getLeagueResultResult: " + getLeagueResultResult.ToString() + " " + adok.LeaguesResultJsonString;

        for (int i = 0; i < CreatedLeagues.Count; i++)
        {
            if (CreatedLeagues[i] != null)
            {
                Destroy(CreatedLeagues[i].gameObject);
            }
        }
        CreatedLeagues.Clear();

        if (getLeagueResultResult == ADokManager.GetLeagueResult.success)
        {
            if (adok.MyLeagues != null)
            {
                if (adok.MyLeagues.result == "ok")
                {
                    for (int i = 0; i < adok.MyLeagues.active.Length; i++)
                    {
                        GameObject ins = Instantiate<GameObject>(refLeabueBtn);

                        ins.transform.parent = refLeabueBtn.transform.parent;
                        EachLeaguePlace elp = ins.GetComponent<EachLeaguePlace>();
                        elp.txtData.text = "league Data: " + adok.MyLeagues.active[i].name + " ";
                        elp.txtData.text = adok.MyLeagues.active[i].des + " \n";
                        elp.leagueId = adok.MyLeagues.active[i].id;
                        elp.myCount = int.Parse(adok.MyLeagues.active[i].myCount);
                        if (adok.MyLeagues.active[i].join.Length > 0)
                        {
                            elp.txtData.text = "Join Data: Date" + adok.MyLeagues.active[i].join[0].joinDate + " ";
                            elp.txtData.text = "Time" + adok.MyLeagues.active[i].join[0].joinTime + " \n";
                        }
                        else
                        {
                            elp.txtData.text = "Player Not Joined To This League";
                        }

                        for (int j = 0; j < adok.MyLeagues.active[i].Rewards.Length; j++)
                        {
                            elp.txtData.text = "Reward-" + (j + 1) + "-> name: " + adok.MyLeagues.active[i].Rewards[j].name + " Des: " + adok.MyLeagues.active[i].Rewards[j].des + " \n";
                        }
                        ins.SetActive(true);
                        CreatedLeagues.Add(ins);
                    }

                    for (int i = 0; i < adok.MyLeagues.commingSoon.Length; i++)
                    {
                        GameObject ins = Instantiate<GameObject>(refLeabueBtn);

                        ins.transform.parent = refLeabueBtn.transform.parent;
                        EachLeaguePlace elp = ins.GetComponent<EachLeaguePlace>();
                        elp.txtData.text = "league Data: " + adok.MyLeagues.commingSoon[i].name + " ";
                        elp.txtData.text = adok.MyLeagues.commingSoon[i].des + " \n";
                        elp.leagueId = adok.MyLeagues.commingSoon[i].id;
                        elp.myCount = int.Parse(adok.MyLeagues.commingSoon[i].myCount);
                        if (adok.MyLeagues.commingSoon[i].join.Length > 0)
                        {
                            elp.txtData.text = "Join Data: Date" + adok.MyLeagues.commingSoon[i].join[0].joinDate + " ";
                            elp.txtData.text = "Time" + adok.MyLeagues.commingSoon[i].join[0].joinTime + " \n";
                        }
                        else
                        {
                            elp.txtData.text = "Player Not Joined To This League";
                        }

                        for (int j = 0; j < adok.MyLeagues.commingSoon[i].Rewards.Length; j++)
                        {
                            elp.txtData.text = "Reward-" + (j + 1) + "-> name: " + adok.MyLeagues.commingSoon[i].Rewards[j].name + " Des: " + adok.MyLeagues.commingSoon[i].Rewards[j].des + " \n";
                        }


                        ins.SetActive(true);
                        CreatedLeagues.Add(ins);
                    }

                    for (int i = 0; i < adok.MyLeagues.ended.Length; i++)
                    {
                        GameObject ins = Instantiate<GameObject>(refLeabueBtn);

                        ins.transform.parent = refLeabueBtn.transform.parent;
                        EachLeaguePlace elp = ins.GetComponent<EachLeaguePlace>();
                        elp.txtData.text = "league Data: " + adok.MyLeagues.ended[i].name + " ";
                        elp.txtData.text = adok.MyLeagues.ended[i].des + " \n";
                        elp.leagueId = adok.MyLeagues.ended[i].id;
                        elp.myCount = int.Parse(adok.MyLeagues.ended[i].myCount);

                        if (adok.MyLeagues.ended[i].join.Length > 0)
                        {
                            elp.txtData.text = "Join Data: Date" + adok.MyLeagues.ended[i].join[0].joinDate + " ";
                            elp.txtData.text = "Time" + adok.MyLeagues.ended[i].join[0].joinTime + " \n";
                        }
                        else
                        {
                            elp.txtData.text = "Player Not Joined To This League";
                        }

                        for (int j = 0; j < adok.MyLeagues.ended[i].Rewards.Length; j++)
                        {
                            elp.txtData.text = "Reward-" + (j + 1).ToString() + "-> name: " + adok.MyLeagues.ended[i].Rewards[j].name + " Des: " + adok.MyLeagues.ended[i].Rewards[j].des + " \n";
                        }

                        ins.SetActive(true);
                        CreatedLeagues.Add(ins);

                    }
                }
            }
        }
    }





    private void Adok_OnMultiUserChange(ADokManager.MultiUserResult getMultiUserResult)
    {
        txtResult2.text = "getMultiUserResult: " + getMultiUserResult.ToString();
    }

    private void Adok_OnGetTimeChange(ADokManager.GetTimeResults getTimeResults)
    {
        txtResult.text = "getTimeResults:" + getTimeResults.ToString() + " " + adok.curTime;
    }

    private void Adok_OnGetDateChange(ADokManager.GetDateResults getDateResults)
    {
        txtResult.text = "getDateResults:" + getDateResults.ToString() + " " + adok.curDate;
    }

    private void Adok_OnMyLeaderChange(ADokManager.LeaderResults leaderResult)
    {
        txtResult.text = "myLeaderResults:" + leaderResult.ToString() + " " + adok.myleaderText;
    }

    private void Adok_OnSyncGetChange(ADokManager.SyncResults syncGetResult)
    {
        txtResult.text = "syncGetResult:" + syncGetResult.ToString();
    }



    private void Adok_OnLeaderChange(ADokManager.LeaderResults leaderResult)
    {
        txtResult.text = "LeaderResults:" + leaderResult.ToString() + " " + adok.leaderText;
    }

    private void Adok_OnVideoAdvShowEnd(ADokManager.AdvsResults result)
    {
        txtResult.text = "AdvsResults:" + result.ToString();
    }

    private void Adok_OnSyncChange(ADokManager.SyncResults result)
    {
        txtResult.text = "SyncResults:" + result.ToString();
    }

    // Update is called once per frame
    void Update()
    {
        if (Input.GetKeyDown(KeyCode.Escape))
        {
            Application.Quit();
        }
    }

    public void SetDataNoSync()
    {
        adok.SetData("score", "12", ADokManager.SyncType.Sync);
        //adok.SetData("gem", "12", ADokManager.SyncType.Sync);
    }

    public void SetDataSync()
    {
        SyncNow();
    }

    public void SyncNow()
    {
        adok.SyncNow();
    }

    public void GetLastData()
    {
        adok.UpdateDataFromServer();
    }

    public void GetData()
    {
        txtResult.text = "score:" + adok.GetData("score");
        txtResult.text += " gems:" + adok.GetData("gem");
    }

    public void GetDataLeaderboard()
    {
        adok.GetLeaderBoard(ADokManager.TimePeriod.all, "score");
    }

    public void GetDataLeaderboardNext()
    {
        adok.GetLeaderBoardNextPage();
    }

    public void GetDataLeaderboardPre()
    {
        adok.GetLeaderBoardPrePage();
    }

    public void GetDataLeaderboardExact()
    {
        if (txtPageNo.text != "")
        {
            int page = 0;
            int.TryParse(txtPageNo.text, out page);
            adok.GetLeaderBoardExactPage(page);
        }

    }

    public void GetDataMyLeaderboard()
    {
        adok.GetMyLeaderBoard(ADokManager.TimePeriod.all, "score");
    }


    public void ClearCatch()
    {
        adok.ClearData();
    }

    public void getUserInfo()
    {
        txtResult.text = "playerId: " + adok.GetUserId() + " nickName:" + adok.GetNickName() + " userName:" + adok.GetUserName();
    }

    public void ShowAdv()
    {
        adok.ShowAdd();

    }

    public void GetDate()
    {
        adok.GetDate();
    }

    public void GetTime()
    {
        adok.GetTime();
    }

    public void CheckMultiUser()
    {
        adok.CheckMultiUser();
    }


    public void GetLeague()
    {
        adok.GetLeagues("3");
    }

    public void AvatarChooseImage()
    {
        adok.AvatarChooseImage();
    }

    public void AvatarCaptureImage()
    {
        adok.AvatarCaptureImage();
    }

    public void ShowAvatarImg()
    {
        Texture2D avatar = adok.GetAvatarImage();
        if (avatar != null)
        {
            AvatarImg.sprite = Sprite.Create(avatar, new Rect(0, 0, avatar.width, avatar.height), new Vector2(0.5f, 0.5f));
        }
    }

    float s = 0;
    public void Crash()
    {
        adok.Crash();
    }
}
