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


public class EachLeaguePlace : MonoBehaviour
{
    public Text txtData;
    public ADokManager adok;
    public string leagueId = "";
    public int myCount = 0;
    public Text txtResult;

    public List<GameObject> RewardBtns;
    public GameObject refRewardBtn;

    void Start()
    {
        RewardBtns = new List<GameObject>();
    }

    public void Join()
    {
        adok.OnGetJoinLeagues += Adok_OnGetJoinLeagues;
        adok.JoinLeagues(leagueId);
    }

    private void Adok_OnGetJoinLeagues(ADokManager.GetJoinLeagueResult GetJoinLeagueResultResult)
    {
        txtResult.text = GetJoinLeagueResultResult.ToString();
    }

    public void GetData()
    {
        adok.OnGetSavedLeagueDatas += Adok_OnGetSavedLeagueDatas;
        adok.GetLeagueDatas(leagueId, myCount.ToString());
    }

    private void Adok_OnGetSavedLeagueDatas(ADokManager.GetSavedLeagueDataResult GetSavedLeagueDataResultResult)
    {
        txtResult.text = adok.LeagueDatasResultJsonString.ToString();
    }

    public void SetData()
    {
        int n = Random.Range(1, 50);
        Debug.Log(n);
        adok.OnSyncLeagueChange += Adok_OnSyncLeagueChange;
        adok.SetDataLeague("score",n.ToString(), leagueId, true);
    }

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

    public void GetLeader()
    {
        adok.OnLeaderLeagueChange += Adok_OnLeaderLeagueChange;
        adok.GetLeagueLeaderBoard(leagueId, myCount.ToString());
    }

    private void Adok_OnLeaderLeagueChange(ADokManager.LeaderResults leaderLeagueResults)
    {
        txtResult.text = "LeaderResults: " + leaderLeagueResults.ToString();
        txtResult.text += "\n" + adok.LeaderLeagueJsonText;
    }

    public void GetReward()
    {
        adok.OnRewardChange += Adok_OnRewardChange;
        adok.GetMyRewards(leagueId);
    }

    private void Adok_OnRewardChange(ADokManager.RewardResults rewardResults)
    {
        txtResult.text = "RewardResults: " + rewardResults.ToString();
        txtResult.text += "\n" + adok.RewardJsonText;

        if (rewardResults == ADokManager.RewardResults.success)
        {
            for (int i = 0; i < RewardBtns.Count; i++)
            {
                Destroy(RewardBtns[i].gameObject);
            }
            RewardBtns.Clear();

            if (adok.rewardsJson != null)
            {
                for (int i = 0; i < adok.rewardsJson.result.Length; i++)
                {
                    Debug.Log(adok.rewardsJson.result.Length);
                    GameObject ins = Instantiate<GameObject>(refRewardBtn);

                    ins.transform.parent = refRewardBtn.transform.parent;
                    EachRewardBtn rewBtn = ins.GetComponent<EachRewardBtn>();
                    rewBtn.RewardId = adok.rewardsJson.result[i].id;
                    rewBtn.txtName.text = rewBtn.rewName = adok.rewardsJson.result[i].name;
                    rewBtn.des = adok.rewardsJson.result[i].des;
                    rewBtn.logoAdd = adok.rewardsJson.result[i].logoAdd;
                    rewBtn.rewardName = adok.rewardsJson.result[i].rewardName;
                    rewBtn.myCount = adok.rewardsJson.result[i].myCount;
                    rewBtn.kind = adok.rewardsJson.result[i].kind;
                    rewBtn.flname = adok.rewardsJson.result[i].flname;
                    rewBtn.leagueCount = adok.rewardsJson.result[i].leagueCount;

                    rewBtn.myLeague = this;
                    ins.SetActive(true);
                    RewardBtns.Add(ins);
                }
            }
        }
    }

}
