using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
/// <summary>
/// 水果倒计时
/// </summary>
public class Fruit_Time : MonoBehaviour
{
    //GameObject
    public GameObject flyParent;//动画父类
    public GameObject flyClock;//减少时间动画
    public GameObject flyWater;//浇水动画
    public GameObject floatBottle;//浮动的加速剂图标

    public GameObject floatWaterObj;//浇水图标
    public GameObject floatWormObj;//长虫图标
    //CanvasGroup
    private CanvasGroup mTimerCG;
    //收获数据
    public LandRewardControl landReward;
    //time
    private Coroutine timeControl;
    private int fruitTime = -3;//当前倒计时时长
    private int totalTime = 0;//总时长
    private bool isPauseTime = false;//是否暂停时间
    private int stopTime = 0;//暂停时间
    //progressBar
    public Image progressBarImg;
    public Text fruitTimeTv;
    //index
    public int position = -1;
    private LandListEvent landEvent;
    private FarmControl landFarm;
    public LandDataControl mLandData;
    private void Awake()
    {
        landEvent = GameObject.Find("Form_land")?.GetComponent<LandListEvent>();
        landFarm = GameObject.Find("Form_land")?.GetComponent<FarmControl>();
        mTimerCG = GetComponent<CanvasGroup>();
    }
    private bool isVideoWater = false;
    //看视频获得加速剂成功 走浇水动画
    public void onVideoBottle()
    {
        if (fruitTime <= 0)
        {
            if (mLandData.setSpeedFruitData(position))
            {
                mLandData.setLandDataTime(0);
                mLandData.updateMyLand();
                landEvent.updateLandList(2, position,0);
            }
        }
        else
        {
            //是看视频过来的加速剂
            isVideoWater = true;

            flyWater.SetActive(true);
            floatBottle.SetActive(false);
        }
    }
    //点击除虫
    public void onClickCleanWorm()
    {
        if (position != -1)
        {
            GameObject.Find("GameManager").GetComponent<UnityToAndroid>().onEventUtil("click_clean_worm", "除虫点击");
            landEvent.updateLandList(5, position, 0);
        }
    }

    //点击使用加速剂
    public void onClickSpeedFruit()
    {
        if (position != -1)
        {
            landFarm.setOnclickLandId = position;
            GameObject.Find("Music_Background").GetComponent<MusicPlayerControl>().playBtnAudio();
            GameObject.Find("GameManager").GetComponent<UnityToAndroid>().onEventUtil("click_accelerometer_farm_" + (position + 1), "使用加速剂" + (position + 1));
            if (fruitTime <= 0)
            {
                mLandData.setLandDataTime(0);
                if (mLandData.setSpeedFruitData(position))
                {
                    GameObject.Find("guideParent").GetComponent<UserGuide>().setStep2();
                    mLandData.updateMyLand();
                    landEvent.updateLandList(2, position,0);
                    Debug.Log("点击倒计时完成 变收获状态成功+ID" + position);
                }
                else
                {
                    Debug.Log("点击倒计时完成 变收获状态失败+ID" + position);
                }
            }
            else
            {
                //加速剂数量不足
                if (CoinNumControl.GetIns().getMyWaterBottle <= 0)
                {
                    Debug.Log("加速剂不足了");
                    GameObject.Find("dialog").GetComponent<DialogControl>().showGetJiasu(CoinNumControl.GetIns().getAcceleratorsVideoBottleNum, CoinNumControl.GetIns().getSubAcceleratorsVideoNum);
                }
                else
                {   //不是看视频过来的加速剂 走本地处理
                    isVideoWater = false;
                    CoinNumControl.GetIns().useMyWaterBottle();
                    //倒计时还未完成 可以加速
                    Debug.Log("加速水果成功 减少时间");
                    //showFlyTime();
                    flyWater.SetActive(true);
                    floatBottle.SetActive(false);
                    landEvent.updateLandList(-2, position,0);
                }
            }
        }
    }

    //展示加速
    public void showFlyTime()
    {
        //int timeMin = LandListDataControl.GetIns().getLandListDataPos(position).acceleratorsSecond;
        int timeMin = mLandData.getMyLandEntity.acceleratorsSecond;
        Debug.Log("执行减少分钟动画");
        StartCoroutine(waitFiveSecend());
        Vector3 v = new Vector3(0, 0, -20f);
        GameObject clock = Instantiate(flyClock, v, Quaternion.identity);
        clock.transform.SetParent(flyParent.transform);
        clock.transform.localPosition = v;
        clock.GetComponent<Clock_Fly>().startMove(timeMin);
        //减少时间
        if (!isVideoWater)
        {
            fruitTime = fruitTime - (timeMin * 60);
        }
    }

    public void showCloudSpeedFlyTime()
    {
        //int timeMin = LandListDataControl.GetIns().getLandListDataPos(position).cloudSecond;
        int timeMin = mLandData.getMyLandEntity.cloudSecond;
        //云朵
        if (fruitTime > 0 && timeMin > 0 && !isPauseTime)
        {
            Vector3 v = new Vector3(0, 0, -20f);
            GameObject clock = Instantiate(flyClock, v, Quaternion.identity);
            clock.transform.SetParent(flyParent.transform);
            clock.transform.localPosition = v;
            clock.GetComponent<Clock_Fly>().startMove(timeMin);
            //减少时间
            //fruitTime = fruitTime - (timeMin * 60);
        }
    }

    private IEnumerator waitFiveSecend()
    {
        yield return new WaitForSeconds(5f);
        //水瓶显示出来
        floatBottle.SetActive(true);
    }

    void Start()
    {
        //initTime();
    }

    //初始化数据
    public void initTime(LandEntity entity)
    {
        if (entity != null)
        {
            stopTime = entity.stopSecond;
            setTime(entity);
        }
    }

    private void setTime(LandEntity entity)
    {
        int tempNextTime = 0;
        for (int i = 0; i < entity.growList.Length; i++)
        {
            if (landReward.getFruitNum < entity.growList[i].start)
            {
                totalTime = entity.growList[i].countdown;
                tempNextTime = 1;
                break;
            }
        }
        if (tempNextTime <= 0)
        {
            totalTime = entity.growList[entity.growList.Length - 1].countdown;
        }
        fruitTime = entity.countDownSecond;
        if (fruitTime <= 0)
        {
            fruitTime = totalTime;
        }
        //设置暂停状态
        setPauseTime(fruitTime);
        //关闭上一次倒计时
        if (timeControl != null)
        {
            StopCoroutine(timeControl);
        }
        timeControl = StartCoroutine(timeConteller());
    }


    private IEnumerator timeConteller()
    {
        fruitTimeTv.text = UnityUtil.getCountdown(fruitTime);
        while (fruitTime >= 0)
        {
            if (progressBarImg != null)
            {
                progressBarImg.fillAmount = 1 - (fruitTime / (float)totalTime);
            }
            yield return new WaitForSecondsRealtime(1f);
            if (fruitTime <= 0)
            {
                //倒计时完成
                //关闭上一次倒计时
                StopCoroutine(timeControl);
                //开启新一轮倒计时
                //timeControl = StartCoroutine(timeConteller());
                //onClickSpeedFruit();
                mLandData.setLandDataTime(0);
                GameObject.Find("Music_Background").GetComponent<MusicPlayerControl>().playBtnAudio();
                if (mLandData.setSpeedFruitData(position))
                {
                    GameObject.Find("guideParent").GetComponent<UserGuide>().setStep2();
                    mLandData.updateMyLand();
                    landEvent.updateLandList(2, position,0);
                }
            }
            else
            {
                setPauseTime(fruitTime);
                if (!isPauseTime)
                {
                    fruitTime--;
                }
            }
            fruitTimeTv.text = UnityUtil.getCountdown(fruitTime);
            //SaveData.GetIns().saveFlowerLastTime(transform.tag, coinTime);
        }
    }
    //判断是否暂停
    private void setPauseTime(int currentTime)
    {
        if (stopTime == currentTime && stopTime != 0)
        {
            isPauseTime = true;
            if (floatWormObj.activeSelf != true)
            {
                floatWormObj.SetActive(true);
            }
            if (floatWaterObj.activeSelf != false)
            {
                floatWaterObj.SetActive(false);
            }
            mTimerCG.alpha = 0;
        }
        else
        {
            isPauseTime = false;
            if (floatWaterObj.activeSelf != true)
            {
                floatWaterObj.SetActive(true);
            }
            if (floatWormObj.activeSelf != false)
            {
                floatWormObj.SetActive(false);
            }
            mTimerCG.alpha = 1;
        }
    }
}
