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

public class TitleControl : MonoBehaviour, GameListenerHelper.IHomeListener
{
    //加速剂数量
    private int acceleratorsNum;
    //加速剂数量
    public int AcceleratordNum
    {
        get { return acceleratorsNum; }
        set { acceleratorsNum = value; }
    }

    //宝箱数量
    private int boxNum;
    //宝箱数量
    public int getBoxNum
    {
        get { return boxNum; }
        set { boxNum = value; }
    }

    //可拆红包数量
    private int bottomRedPackNum;
    //可拆红包数量
    public int getBottomRedPackNum
    {
        get { return bottomRedPackNum; }
    }

    //title
    public RawImage image;
    public Text  coinNumTv,subClockNumTv,cloudSpeedTimeTv, lotteryTv;
    public Image timeBg;
    //拆红包
    public GameObject redPackNumOBJ;//相差的数量
    public GameObject redPackNumOBJOK;//可以拆开
    //music 开关
    public Image musicOpenImg, musicCloseImg;
    public GameObject musicPlayer;
    //Android
    private AndroidJavaObject mainActivity;
    //
    private string moneyValue, coinValue,subClockValue;

    private bool isLoadSuccess = false;
    private int coinTime;//倒计时

    private HomeInfoEntity tempHomeInfo;

    private void Awake()
    {
        AndroidJavaClass jc = new AndroidJavaClass("com.unity3d.player.UnityPlayer");
        mainActivity = jc.GetStatic<AndroidJavaObject>("currentActivity");
        //设置音乐
        initSound();
    }

    void Start()
    {
        //获取首页信息
        getHomeInfo();
    }

    private void initSound()
    {
        if (SaveData.GetIns().isOpenMusic() == 0)
        {
            AudioSource a = musicPlayer.GetComponent<AudioSource>();
            a.Play();
            musicCloseImg.gameObject.SetActive(false);
            musicOpenImg.gameObject.SetActive(true);
        }
        else
        {
            AudioSource a = musicPlayer.GetComponent<AudioSource>();
            a.Pause();
            musicCloseImg.gameObject.SetActive(true);
            musicOpenImg.gameObject.SetActive(false);
        }
    }

    private void Update()
    {
        //if (isLoadSuccess)
        //{
        //    isLoadSuccess = false;
        //    updateHomeData(tempHomeInfo);
        //}
        if (CoinNumControl.GetIns().getRemainRewardNum > 0)
        {
            if (redPackNumOBJ.activeSelf != true)
            {
                redPackNumOBJ.SetActive(true);
            }
            if (redPackNumOBJOK.activeSelf != false)
            {
                redPackNumOBJOK.SetActive(false);
            }
        }
        else
        {
            if (redPackNumOBJ.activeSelf != false)
            {
                redPackNumOBJ.SetActive(false);
            }
            if (redPackNumOBJOK.activeSelf != true)
            {
                redPackNumOBJOK.SetActive(true);
            }
        }
    }

    //点击音乐
    public void onClickMusic(int isOpen)
    {
        
        if (isOpen == 0)
        {
            AudioSource a = musicPlayer.GetComponent<AudioSource>();
            a.Play();
            musicCloseImg.gameObject.SetActive(false);
            musicOpenImg.gameObject.SetActive(true);
            GameObject.Find("GameManager").GetComponent<UnityToAndroid>().onEventUtil("sound_on", "开声音按钮点击");
        }
        else
        {
            AudioSource a = musicPlayer.GetComponent<AudioSource>();
            a.Pause();
            musicCloseImg.gameObject.SetActive(true);
            musicOpenImg.gameObject.SetActive(false);
            GameObject.Find("GameManager").GetComponent<UnityToAndroid>().onEventUtil("sound_off", "关声音按钮点击");
        }
        SaveData.GetIns().setMusicTag(isOpen);
    }





    //获取title数据
    public void getHomeInfo()
    {
        try
        {
            mainActivity.Call("getHomeInfo", new GameListenerHelper.ICallBackListener(this));
        }
        catch(Exception e)
        {
            Debug.Log("getTitleData ERROR"+ e.Message);
            //string ss = "{\"acceleratorsNum\":2,\"boxNum\":10,\"cents\":2500,\"cloudNum\":9,\"cloudSecond\":-2,\"coins\":560,\"ore\":0,\"subClockNum\":10}";
            //string ss = "{\"acceleratorsNum\":0,\"boxNum\":10,\"cents\":0,\"cloudNum\":10,\"cloudSecond\":-2,\"coins\":331,\"imgUrl\":\"https://thirdwx.qlogo.cn/mmopen/vi_32/boljf7VZDAs1hzako6PVdplak7ollRwnphH7vmMqibKDclqDCRJuK2icbpCic7NoTicPE8sEq50ibQVA1bsbQiaZUM9g/132\",\"ore\":0,\"plantBoxNum\":5,\"subClockNum\":15}";
            //onHomeResult(JsonUtility.FromJson<HomeInfoEntity>(ss));
        }
    }

    public void onHomeResult(HomeInfoEntity home)
    {
        ////首页加速剂次数
        //Debug.Log("首页加速剂次数" + home.acceleratorsNum);
        //acceleratorsNum = home.acceleratorsNum;
        ////宝箱数
        //boxNum = home.boxNum;
        //tempHomeInfo = home;
        //isLoadSuccess = true;
        StartCoroutine(updateHomeData(home));
    }

    private IEnumerator updateHomeData(HomeInfoEntity home)
    {
        yield return null;
        CoinNumControl.GetIns().getSubAcceleratorsVideoNum = home.subAcceleratorsVideoNum;//同步看视频获取加速剂次数
        CoinNumControl.GetIns().getAcceleratorsVideoBottleNum = home.acceleratorsVideoNum;//看视频将要给的加速剂数量
        CoinNumControl.GetIns().getSubSeedVideoNum = home.subSeedVideoNum;//同步看视频获取种子次数
        CoinNumControl.GetIns().getRemainRewardNum = home.plantBoxNum;//同步收获次数 可拆红包
        //可拆红包数量
        bottomRedPackNum = home.plantBoxNum;
       
        //首页加速剂次数
        Debug.Log("首页加速剂次数" + home.acceleratorsNum);
        acceleratorsNum = home.acceleratorsNum;
        //宝箱数
        boxNum = home.boxNum;
        //金币数
        Debug.Log("coin_num" + home.coins);
        CoinNumControl.GetIns().getMyCoin=home.coins;
        coinValue =home.coins.ToString();
        //钱数
        Debug.Log("moneyNum" + home.cents);
        moneyValue = (home.cents / 100f).ToString();
        //打卡数
        Debug.Log("subClockNum" + home.subClockNum);
        if (home.subClockNum <= 0)
        {
            subClockValue = "已打卡";
        }
        else
        {
            subClockValue = "差"+home.subClockNum+"次";
            // subClockValue = home.currentClockNum.ToString()+"/"+home.maxClockNum.ToString();
        }
        coinNumTv.text = coinValue;
        // moneyNumTv.text = moneyValue;
        //抽奖次数
        lotteryTv.text = home.lotteryNum.ToString();
        subClockNumTv.text = subClockValue;
        //云朵加速弹窗 剩余次数传值
        Debug.Log("cloudNum" + home.cloudNum);
        GameObject.Find("dialog").GetComponent<DialogControl>().CloudSpeedNum = home.cloudNum;
        //关闭上一次倒计时
        if (timeControl != null)
        {
            StopCoroutine(timeControl);
        }
        coinTime = home.cloudSecond;
        if (home.cloudSecond > 0)
        {
            cloudSpeedTimeTv.text = UnityUtil.getCountdown(coinTime);
            timeBg.gameObject.SetActive(true);
            //开启倒计时
            timeControl = StartCoroutine(timeConteller());
        }
        else
        {
            timeBg.gameObject.SetActive(false);
        }
        if (home.imgUrl != null)
        {
            if (null == image)
                image = GetComponent<RawImage>();
            try
            {
                LoadImageHelper helper = gameObject.AddComponent<LoadImageHelper>();
                helper.LoadImage(image, home.imgUrl);
            }
            catch
            {
                Debug.Log("LoadImage ERROR");
            }
        }
    }

    //time
    private Coroutine timeControl;
    public int CloudSpeedTime
    {
        get { return coinTime; }
        set { coinTime = value; }
    }
    private IEnumerator timeConteller()
    {

        while (coinTime >= 0)
        {

            yield return new WaitForSecondsRealtime(1f);
            if (coinTime <= 0)
            {
                //倒计时完成
                timeBg.gameObject.SetActive(false);
                //关闭上一次倒计时
                StopCoroutine(timeControl);
            }
            else
            {
                coinTime--;
            }
            cloudSpeedTimeTv.text = UnityUtil.getCountdown(coinTime);
        }

    }


    //error
    public void onError(string message)
    {
        //moneyNumTv.text = "0元";
        coinNumTv.text = "0";
        subClockNumTv.text = "0";
    }

}
