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

public class RedPackControl : MonoBehaviour, GameListenerHelper.IRedPackListener
{

    public GameObject redpackListObj;
    public GameObject redpackNumImg;
    public Text redPackNumTv;
    public Text redPackTimeTv;
    private LandEntity[] redPack;
    public LandEntity[] getRedPackItem
    {
        get { return redPack; }
    }
    private AndroidJavaObject mainActivity;
    private void Awake()
    {
        AndroidJavaClass jc = new AndroidJavaClass("com.unity3d.player.UnityPlayer");
        mainActivity = jc.GetStatic<AndroidJavaObject>("currentActivity");
    }

    private void Start()
    {
        redPackListDataUpdate();
    }

    private bool isUpdate = false;
    private void Update()
    {
        if (isUpdate)
        {
            isUpdate = false;
            if (redPackNum > 0)
            {
                redPackTimeTv.text = "";
                redpackNumImg.SetActive(true);
                redPackNumTv.text = redPackNum.ToString();
            }
            else
            {
                redPackNumTv.text = redPackNum.ToString();
                redpackNumImg.SetActive(false);
                setRedPackTime(minTime);
            }
        }
    }


    public void onClickRedPack()
    {
        
        if (redpackListObj.activeSelf == true)
        {
            GameObject.Find("GameManager").GetComponent<UnityToAndroid>().onEventUtil("click_redpack_dissmiss", "点击红包列表消失");
            redpackListObj.SetActive(false);
        }
        else
        {
            GameObject.Find("GameManager").GetComponent<UnityToAndroid>().onEventUtil("click_redpack_show", "点击红包列表展示");
            redPackListData();
        }
    }

    public void onClickRedPackReceive(int redPackId,string redPackMoneyNum)
    {
        GameObject.Find("GameManager").GetComponent<UnityToAndroid>().onEventUtil("click_redpack_item_"+ redPackId, "红包点击"+ redPackId);
        try
        {
            if (redpackListObj.activeSelf == true)
            {
                redpackListObj.SetActive(false);
            }
            mainActivity.Call("onRedPackReceive",redPackId,redPackMoneyNum);
        }
        catch (Exception e)
        {

            //string result = "{\"redPackList\":[{\"coins\":100,\"countDownSecond\":10,\"id\":1,\"totalSecond\":180},{\"coins\":100,\"countDownSecond\":10,\"id\":2,\"totalSecond\":180},{\"coins\":300,\"countDownSecond\":0,\"id\":3,\"totalSecond\":300},{\"coins\":500,\"countDownSecond\":0,\"id\":4,\"totalSecond\":600},{\"coins\":800,\"countDownSecond\":0,\"id\":5,\"totalSecond\":1200},{\"coins\":1000,\"countDownSecond\":0,\"id\":6,\"totalSecond\":1800},{\"coins\":1200,\"countDownSecond\":0,\"id\":7,\"totalSecond\":3600},{\"coins\":1500,\"countDownSecond\":0,\"id\":8,\"totalSecond\":5400},{\"coins\":1800,\"countDownSecond\":0,\"id\":9,\"totalSecond\":7200},{\"coins\":2000,\"countDownSecond\":0,\"id\":10,\"totalSecond\":10800}]}";
            //string result2 = "{\"redpacklist\":[{\"coins\":2,\"coinsstr\":\"0.02\",\"countdownsecond\":119,\"id\":1,\"totalsecond\":120},{\"coins\":3,\"coinsstr\":\"0.03\",\"countdownsecond\":289,\"id\":2,\"totalsecond\":300},{\"coins\":5,\"coinsstr\":\"0.05\",\"countdownsecond\":438,\"id\":3,\"totalsecond\":600},{\"coins\":8,\"coinsstr\":\"0.08\",\"countdownsecond\":388,\"id\":4,\"totalsecond\":1200},{\"coins\":10,\"coinsstr\":\"0.1\",\"countdownsecond\":978,\"id\":5,\"totalsecond\":1800},{\"coins\":20,\"coinsstr\":\"0.2\",\"countdownsecond\":2773,\"id\":6,\"totalsecond\":3600},{\"coins\":30,\"coinsstr\":\"0.3\",\"countdownsecond\":4566,\"id\":7,\"totalsecond\":5400},{\"coins\":40,\"coinsstr\":\"0.4\",\"countdownsecond\":6360,\"id\":8,\"totalsecond\":7200},{\"coins\":60,\"coinsstr\":\"0.6\",\"countdownsecond\":9953,\"id\":9,\"totalsecond\":10800},{\"coins\":100,\"coinsstr\":\"1\",\"countdownsecond\":20747,\"id\":10,\"totalsecond\":21600}]}";

            //LandListEntity bean = JsonUtility.FromJson<LandListEntity>(result2);
            //onRedPackListResult(bean.redPackList);
            Debug.Log("onClickRedPack ERROR" + e.Message);
        }
    }
    public void redPackListDataUpdate()
    {
        try
        {
            mainActivity.Call("onGetRedPackList", new GameListenerHelper.ICallBackListener(this));
        }
        catch (Exception e)
        {

            //string result = "{\"redPackList\":[{\"coins\":100,\"countDownSecond\":10,\"id\":1,\"totalSecond\":180},{\"coins\":100,\"countDownSecond\":10,\"id\":2,\"totalSecond\":180},{\"coins\":300,\"countDownSecond\":0,\"id\":3,\"totalSecond\":300},{\"coins\":500,\"countDownSecond\":0,\"id\":4,\"totalSecond\":600},{\"coins\":800,\"countDownSecond\":0,\"id\":5,\"totalSecond\":1200},{\"coins\":1000,\"countDownSecond\":0,\"id\":6,\"totalSecond\":1800},{\"coins\":1200,\"countDownSecond\":0,\"id\":7,\"totalSecond\":3600},{\"coins\":1500,\"countDownSecond\":0,\"id\":8,\"totalSecond\":5400},{\"coins\":1800,\"countDownSecond\":0,\"id\":9,\"totalSecond\":7200},{\"coins\":2000,\"countDownSecond\":0,\"id\":10,\"totalSecond\":10800}]}";
            //string result2 = "{\"redPackList\":[{\"coins\":2,\"coinsStr\":\"0.02\",\"countDownSecond\":0,\"id\":1,\"totalSecond\":120},{\"coins\":3,\"coinsStr\":\"0.03\",\"countDownSecond\":289,\"id\":2,\"totalSecond\":300},{\"coins\":5,\"coinsStr\":\"0.05\",\"countDownSecond\":438,\"id\":3,\"totalSecond\":600},{\"coins\":8,\"coinsStr\":\"0.08\",\"countDownSecond\":388,\"id\":4,\"totalSecond\":1200},{\"coins\":10,\"coinsStr\":\"0.1\",\"countDownSecond\":978,\"id\":5,\"totalSecond\":1800},{\"coins\":20,\"coinsStr\":\"0.2\",\"countDownSecond\":2773,\"id\":6,\"totalSecond\":3600},{\"coins\":30,\"coinsStr\":\"0.3\",\"countDownSecond\":4566,\"id\":7,\"totalSecond\":5400},{\"coins\":40,\"coinsStr\":\"0.4\",\"countDownSecond\":6360,\"id\":8,\"totalSecond\":7200},{\"coins\":60,\"coinsStr\":\"0.6\",\"countDownSecond\":9953,\"id\":9,\"totalSecond\":10800},{\"coins\":100,\"coinsStr\":\"1\",\"countDownSecond\":20747,\"id\":10,\"totalSecond\":21600}]}";
            //LandListEntity bean = JsonUtility.FromJson<LandListEntity>(result2);
            //onRedPackListResult(bean.redPackList);
            Debug.Log("onClickRedPack ERROR" + e.Message);
        }
    }

    private void redPackListData()
    {
        try
        {
            mainActivity.Call("onGetRedPackList", new GameListenerHelper.ICallBackListener(this));
        }
        catch (Exception e)
        {

            //string result = "{\"redPackList\":[{\"coins\":100,\"countDownSecond\":10,\"id\":1,\"totalSecond\":180},{\"coins\":100,\"countDownSecond\":10,\"id\":2,\"totalSecond\":180},{\"coins\":300,\"countDownSecond\":0,\"id\":3,\"totalSecond\":300},{\"coins\":500,\"countDownSecond\":0,\"id\":4,\"totalSecond\":600},{\"coins\":800,\"countDownSecond\":0,\"id\":5,\"totalSecond\":1200},{\"coins\":1000,\"countDownSecond\":0,\"id\":6,\"totalSecond\":1800},{\"coins\":1200,\"countDownSecond\":0,\"id\":7,\"totalSecond\":3600},{\"coins\":1500,\"countDownSecond\":0,\"id\":8,\"totalSecond\":5400},{\"coins\":1800,\"countDownSecond\":0,\"id\":9,\"totalSecond\":7200},{\"coins\":2000,\"countDownSecond\":0,\"id\":10,\"totalSecond\":10800}]}";
            string result2 = "{\"redPackList\":[{\"coins\":2,\"coinsStr\":\"0.02\",\"countDownSecond\":10,\"id\":1,\"totalSecond\":120},{\"coins\":3,\"coinsStr\":\"0.03\",\"countDownSecond\":289,\"id\":2,\"totalSecond\":300},{\"coins\":5,\"coinsStr\":\"0.05\",\"countDownSecond\":438,\"id\":3,\"totalSecond\":600},{\"coins\":8,\"coinsStr\":\"0.08\",\"countDownSecond\":388,\"id\":4,\"totalSecond\":1200},{\"coins\":10,\"coinsStr\":\"0.1\",\"countDownSecond\":978,\"id\":5,\"totalSecond\":1800},{\"coins\":20,\"coinsStr\":\"0.2\",\"countDownSecond\":2773,\"id\":6,\"totalSecond\":3600},{\"coins\":30,\"coinsStr\":\"0.3\",\"countDownSecond\":4566,\"id\":7,\"totalSecond\":5400},{\"coins\":40,\"coinsStr\":\"0.4\",\"countDownSecond\":6360,\"id\":8,\"totalSecond\":7200},{\"coins\":60,\"coinsStr\":\"0.6\",\"countDownSecond\":9953,\"id\":9,\"totalSecond\":10800},{\"coins\":100,\"coinsStr\":\"1\",\"countDownSecond\":20747,\"id\":10,\"totalSecond\":21600}]}";
            LandListEntity bean = JsonUtility.FromJson<LandListEntity>(result2);
            onRedPackListResult(bean.redPackList);
            Debug.Log("onClickRedPack ERROR" + e.Message);
        }
    }

    private int minTime;
    private int redPackNum = 0;
    public void onRedPackListResult(LandEntity[] redPackList)
    {
        redPack = redPackList;
        minTime = 100000;
        redPackNum = 0;
        //Debug.Log(redPackList.Length);
        for (int i = 0; i < redPack.Length; i++)
        {
            if (redPack[i].countDownSecond < minTime)
            {
                minTime = redPack[i].countDownSecond;
            }
            if (redPack[i].countDownSecond <= 0)
            {
                redPackNum++;
            }

            //if (redPack[i].totalSecond < minTime)
            //{
            //    minTime = redPack[i].totalSecond;
            //}
            //if (redPack[i].totalSecond <= 0)
            //{
            //    redPackNum++;
            //}
        }
        //if (clickType == 2)
        //{
        //    Debug.Log("onRedPackListSuccess11");
        //    GameObject.Find("GameManager").GetComponent<UnityToAndroid>().onEventUtil("show_redpack_list", "红包列表展示");
        //    Debug.Log("onRedPackListSuccess12");
        //    //redpackListObj.SetActive(true);
        //    // 加载框架UI组件
        //    UIComponent UI
        //        = GameEntry.GetComponent<UIComponent>();
        //    // 加载UI
        //    UI.OpenUIForm("Assets/Prefabs/red_pack_list.prefab", "RedPack");
        //}
        Debug.Log("onRedPackListSuccess13");
        isUpdate = true;
    }



    public void onError(string message)
    {

    }










    private void setRedPackTime(int second)
    {
        //关闭上一次倒计时
        if (fruitTimeControl != null)
        {
            StopCoroutine(fruitTimeControl);
        }
        if (second <= 0)
        {
            //倒计时完成
            redPackTimeTv.text = "";
        }
        else
        {
            redPackTimeTv.text = UnityUtil.getCountdown(second);
            fruitTimeControl = StartCoroutine(fruitCountDown(second));
        }
    }

    private int coinTime=0;
    //time
    private Coroutine fruitTimeControl;
    //红包倒计时
    private IEnumerator fruitCountDown(int second)
    {
        coinTime = second;//倒计时
        while (coinTime >= 0)
        {
            yield return new WaitForSecondsRealtime(1f);
            if (coinTime <= 0)
            {//倒计时完成
                redPackTimeTv.text = "";
                //关闭上一次倒计时
                StopCoroutine(fruitTimeControl);
                if (fruitTimeControl != null)
                {
                    fruitTimeControl = null;
                }
                redPackListDataUpdate();
            }
            else
            {
                coinTime--;
                redPackTimeTv.text = UnityUtil.getCountdown(coinTime);
            }
        }
    }


}
