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

public class NewFruitControl : MonoBehaviour
{
    public Image paiziImg;
    public Text guideTimeTv;
    public Text unLockPriceTv, unLockNameTv, onClickViewTv;
    public GameObject floatCodeObj;
    private int clickType = -1;
    private int fruit_id = 0;
    private bool isPlayGetGoodsAnim = false;//播放收获果实
    private bool isPlayJiasujiAnim = false;//播放加速剂
    private string[] goodsName = { "白菜", "番茄", "南瓜", "萝卜", "玉米", "茄子", "西瓜", "葡萄", "草莓" };
    private int jiasujiTimeSecend = 0;
    private int cloudTimeSecend = 0;
    public GameObject flyClock;
    //点击事件 解锁地块传1 种植种子传2 收获果实传3 （加速剂 4 另一个接口）
    public void onClickLanType()
    {
        GameObject.Find("Music_Background").GetComponent<MusicPlayerControl>().playBtnAudio();
        if (clickType != -1 && GetComponent<FruitCodeType>().getIsShow)
        {
            Debug.Log("clickType" + clickType + "fruit_id" + fruit_id);
            if (clickType == 4)
            {
                GameObject.Find("GameManager").GetComponent<UnityToAndroid>().onEventUtil("click_accelerometer_farm_" + fruit_id, "使用加速剂" + fruit_id);
                GameObject.Find("Form_land").GetComponent<FarmControl>().onClickUnlockLand(fruit_id, 4);
            }
            else
            {
                if (clickType == 2)
                {
                    GameObject.Find("GameManager").GetComponent<UnityToAndroid>().onEventUtil("click_plant_farm_id_" + fruit_id, "种植农作物id_" + fruit_id);
                }
                if (clickType == 3)
                {
                    GameObject.Find("GameManager").GetComponent<UnityToAndroid>().onEventUtil("click_reward_farm_id_" + fruit_id, "收获农作物id_" + fruit_id);
                }
                if (clickType != 1)
                {
                    GameObject.Find("Form_land").GetComponent<FarmControl>().onClickUnlockLand(fruit_id, clickType);
                }
            }
        }
        else
        {
            GameObject.Find("Form_land").GetComponent<FarmControl>().getLandList();
        }
    }
    //点击事件 解锁地块传1 
    public void onClickUnlockLandType()
    {
        GameObject.Find("Music_Background").GetComponent<MusicPlayerControl>().playBtnAudio();
        if (clickType != -1)
        {
            Debug.Log("clickType" + clickType + "fruit_id" + fruit_id);
            GameObject.Find("Form_land").GetComponent<FarmControl>().onClickUnlockLand(fruit_id, 1);
        }
        else
        {
            GameObject.Find("Form_land").GetComponent<FarmControl>().getLandList();
        }
    }

    public void DdateError()
    {
        Debug.Log("更新土地失败");
    }
    private LandEntity tempLandData;
    private int tempLandStatus = -1;
    public void updateLand(LandEntity land)
    {
        if (land.name == transform.tag)
        {
            tempLandData = land;
            StartCoroutine(updateLandDateOnUi(land));
        }
    }

    private IEnumerator updateLandDateOnUi(LandEntity land)
    {
        yield return null;
        if (PlayerPrefs.GetInt("isFirstPlant", 0) != 0)
        {
            var v = transform.localPosition;
            v.z = 0;
            transform.localPosition = v;
            if (guideTimeTv != null)
            {
                guideTimeTv.gameObject.SetActive(false);
            }
        }
        updateLandData(land);
    }
    //private bool isUpdateLandSuccess = false;
    //private void Update()
    //{
    //    if (isUpdateLandSuccess)
    //    {
    //        if (PlayerPrefs.GetInt("isFirstPlant", 0) != 0)
    //        {
    //            var v = transform.localPosition;
    //            v.z = 0;
    //            transform.localPosition = v;
    //            if (guideTimeTv != null)
    //            {
    //                guideTimeTv.gameObject.SetActive(false);
    //            }
    //        }
    //        updateLandData(tempLandData);
    //        isUpdateLandSuccess = false;
    //    }
    //}

    private void updateLandData(LandEntity land)
    {
        jiasujiTimeSecend = land.acceleratorsSecond;
        cloudTimeSecend = land.cloudSecond;
        fruit_id = land.id;
        if (land.status == tempLandStatus)
        {
            if (land.status == 2)
            {
                setFruitTime(land.countDownSecond);
            }
            return;
        }
        else
        {
            if (jiasujiTimeControl != null)
            {
                StopCoroutine(jiasujiTimeControl);
            }
        }
        tempLandStatus = land.status;
        Debug.Log(land.status + "name" + land.name);
        //0 未解锁 1 未种植 2 已种植（显示倒计时）3 已成熟 4 待解锁
        if (paiziImg.gameObject.activeSelf != false)
        {
            paiziImg.gameObject.SetActive(false);
        }
        switch (land.status)
        {
            case 0:
                setLandType(0);
                setLandFruitType(land.id, false);
                setFloatCode(-2, 0, -1);
                clickType = -1;
                break;
            case 1:
                setLandType(1);
                setLandFruitType(land.id, false);
                setFloatCode(0, land.id, 2);
                clickType = 2;
                break;
            case 2:
                setLandType(2);
                setLandFruitType(land.id, true);
                setFloatCode(1, land.id, 4);
                clickType = 4;
                break;
            case 3:
                setLandType(2);
                setLandFruitType(land.id, true);
                setFloatCode(2, land.id, 3);
                clickType = 3;
                break;
            case 4:
                if (paiziImg.gameObject.activeSelf != true)
                {
                    paiziImg.gameObject.SetActive(true);
                }
                setLandType(0);
                setLandFruitType(land.id, false);
                setFloatCode(-2, 0, 1);
                unLockNameTv.text = "解锁" + land.name;
                unLockPriceTv.text = land.unlockPrice.ToString();
                clickType = 1;
                break;
            default:
                setLandType(0);
                setLandFruitType(land.id, false);
                setFloatCode(-2, 0, -1);
                clickType = -1;
                Debug.Log("default" + clickType);
                break;
        }
        setFruitTime(land.countDownSecond);
    }

    //五秒间隔
    private Coroutine jiasujiTimeControl;
    private IEnumerator jiasujiCountDown()
    {
        yield return new WaitForSecondsRealtime(5f);
        if (tempLandStatus == 2)
        {
            setFloatCode(1, fruit_id, 4);
        }

    }


    //设置土地 0 绿色 1 未种植 2 已种植
    public void setLandType(int type)
    {
        Debug.Log(transform.tag + "设置土地" + type);
        GetComponent<FruitCodeType>().setLandType(type);

    }
    //设置农作物
    public void setLandFruitType(int type, bool isShow)
    {
        Debug.Log(transform.tag + "设置农作物" + type + "isShow" + isShow);
        GetComponent<FruitCodeType>().setFruitType(type);
        GetComponent<FruitCodeType>().setFruitShow(isShow);
    }

    //浮动的提示
    //private GameObject tempFloatCode;


    public void setFloatCode(int type, int id, int clickTypes)
    {
        Debug.Log("setFloatCodeid" + clickTypes);
        if (type == -2)
        {
            GetComponent<FruitCodeType>().setFloatShow(false);
        }
        else
        {
            clickType = clickTypes;
            if (type == 0)
            {
                GetComponent<FruitCodeType>().setFloatCodeType(id);
            }
            else if (type == 1)
            {
                GetComponent<FruitCodeType>().setFloatCodeType(10);
            }
            else if (type == 2)
            {
                GetComponent<FruitCodeType>().setFloatCodeType(11);
            }
            GetComponent<FruitCodeType>().setFloatShow(true);
        }
    }

    //倒计时
    public Text fruit_timeTv;//果实成熟倒计时
    public SpriteRenderer timeSpriteCompent;//倒计时背景
    public void setFruitTime(int second)
    {
        if (fruitTimeControl != null)
        {
            StopCoroutine(fruitTimeControl);
        }
        if (second <= 0)
        {
            //倒计时完成
            fruit_timeTv.text = "";
            timeSpriteCompent.color = new Color(1, 1, 1, 0);
            //关闭上一次倒计时
            if (fruitTimeControl != null)
            {
                StopCoroutine(fruitTimeControl);
            }
            if (guideTimeTv != null)
            {
                guideTimeTv.gameObject.SetActive(false);
            }
        }
        else
        {
            timeSpriteCompent.color = new Color(1, 1, 1, 180);
            fruit_timeTv.text = UnityUtil.getCountdown(second);
            fruitTimeControl = StartCoroutine(fruitCountDown(second));
        }
    }
    //time
    private Coroutine fruitTimeControl;
    //农作物成熟倒计时
    private IEnumerator fruitCountDown(int second)
    {
        int coinTime = second;//倒计时
        while (coinTime >= 0)
        {

            yield return new WaitForSecondsRealtime(1f);
            if (coinTime <= 0)
            {//倒计时完成
                fruit_timeTv.text = "";
                if (guideTimeTv != null)
                {
                    guideTimeTv.gameObject.SetActive(false);
                }
                timeSpriteCompent.color = new Color(1, 1, 1, 0);
                //关闭上一次倒计时
                StopCoroutine(fruitTimeControl);
                fruitTimeControl = null;
                //if (!isPlayAnimation)
                //{
                GameObject.Find("Form_land").GetComponent<FarmControl>().getLandList();
                //}
            }
            else
            {
                coinTime--;
                fruit_timeTv.text = UnityUtil.getCountdown(coinTime);

                if (guideTimeTv != null)
                {
                    if (PlayerPrefs.GetInt("isFirstPlant", 0) == 0)
                    {
                        if (guideTimeTv != null)
                        {
                            guideTimeTv.gameObject.SetActive(true);
                        }
                        guideTimeTv.text = UnityUtil.getCountdown(coinTime);
                    }
                    else
                    {
                        guideTimeTv.gameObject.SetActive(false);
                    }
                }
            }
        }
    }




    public void showLandWater(int tag)
    {
        if (goodsName[tag - 1].Equals(transform.tag))
        {
            //开始加速剂浇水动画
            //if (acceleratedWaterControl != null)
            //{
            //    StopCoroutine(acceleratedWaterControl);
            //    acceleratedWaterControl = null;
            //}
            //if (tempFloatCode != null)
            //{
            //    Destroy(tempFloatCode);
            //    tempFloatCode = null;
            //}
            if (!isPlayJiasujiAnim)
            {
                isPlayJiasujiAnim = true;
                GetComponent<FruitCodeType>().setFloatShow(false);
                acceleratedWaterControl = StartCoroutine(showAcceleratedWaterAnimation());
            }

        }
    }

    public void showGetGoods(StatusEntity tag)
    {
        Debug.Log("收获1  ：" + tag.needCoin);
        if (goodsName[tag.id - 1].Equals(transform.tag))
        {
            if (!isPlayGetGoodsAnim)
            {
                isPlayGetGoodsAnim = true;
                //开始收获果实动画
                if (getFruitAnimControl != null)
                {
                    StopCoroutine(getFruitAnimControl);
                }
                //if (tempFloatCode != null)
                //{
                //    Destroy(tempFloatCode);
                //}
                Debug.Log("收获2  ：" + tag.needCoin);
                GetComponent<FruitCodeType>().setFloatShow(false);
                Debug.Log("收获3  ：" + tag.needCoin);
                GetComponent<FruitCodeType>().setFruitShow(false);
                try
                {
                    Debug.Log("收获4  ：" + tag.needCoin);
                    GameObject.Find("Music_Background").GetComponent<MusicPlayerControl>().playgetRewardAudio();
                    getFruitAnimControl = StartCoroutine(showGetFruitAnimation(tag.needCoin));
                }
                catch (Exception e)
                {
                    Debug.Log("收获错误"+ e.Message);
                }

            }
        }
    }


    //动画父级
    public GameObject acceleratedWaterParentObj;
    public GameObject getGoodsParentObj;
    //加速剂 动画
    public GameObject acceleratedWaterObj;
    //加速剂浇水动画
    private Coroutine acceleratedWaterControl;
    private GameObject acceleratedWater;
    private IEnumerator showAcceleratedWaterAnimation()
    {
        GameObject.Find("Music_Background").GetComponent<MusicPlayerControl>().playColudWaterAudio();
        GetComponent<LandAnimation>().showPlayWaterAnim();
        yield return new WaitForSeconds(1.4f);
        GetComponent<LandAnimation>().closePlayWaterAnim();
        if (jiasujiTimeControl != null)
        {
            StopCoroutine(jiasujiTimeControl);
        }
        jiasujiTimeControl = StartCoroutine(jiasujiCountDown());

        isPlayJiasujiAnim = false;
        if (PlayerPrefs.GetInt("isFirstPlant") == 1)
        {
            showClockTime(1);
        }

        //加速成功 刷新菜地
        GameObject.Find("Form_land").GetComponent<FarmControl>().getLandList();
    }


    //收获果实
    public GameObject getGoodsObj;
    //收获果实动画
    private Coroutine getFruitAnimControl;
    private GameObject getGoodsSpr;
    //收获果实
    private IEnumerator showGetFruitAnimation(int coinNum)
    {
        Debug.Log("收获5  ：" + coinNum);
        GetComponent<LandAnimation>().showPlayGetGoodsAnim(fruit_id, coinNum);
        Debug.Log("收获6  ：" + coinNum);
        yield return new WaitForSeconds(1.4f);
        Debug.Log("收获7  ：" + coinNum);
        GetComponent<LandAnimation>().closePlayGetGoodsAnim();
        Debug.Log("收获8  ：" + coinNum);
        isPlayGetGoodsAnim = false;
        //Destroy(getGoodsSpr);
        //加速成功 刷新菜地
        GameObject.Find("Form_land").GetComponent<FarmControl>().getLandList();
        GameObject.Find("OrderView").GetComponent<OrderControl>().getOrderData();
    }
    //展示减少时间
    public void showClockTime(int type)
    {
        if (type == 1)//加速剂
        {
            if (jiasujiTimeSecend > 0)
            {
                Vector3 v = new Vector3(0, 0, -20f);
                GameObject clock = Instantiate(flyClock, v, Quaternion.identity);
                clock.transform.SetParent(transform);
                clock.transform.localPosition = v;
                clock.GetComponent<Clock_Fly>().startMove(jiasujiTimeSecend);
            }

        }
        else
        {//云朵
            if (tempLandStatus == 2 && cloudTimeSecend > 0)
            {
                Vector3 v = new Vector3(0, 0, -20f);
                GameObject clock = Instantiate(flyClock, v, Quaternion.identity);
                clock.transform.SetParent(transform);
                clock.transform.localPosition = v;
                clock.GetComponent<Clock_Fly>().startMove(cloudTimeSecend);
            }
        }


    }
}
