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

public class LandDataControl : MonoBehaviour
{
    private LandEntity mLandData = null;
    private LandShowControl landView;
    public LandRewardControl landReward;

    public int position = -1;
    //public LandDataControl nextLandDataContol=null;
    private void Awake()
    {
        landView = GetComponent<LandShowControl>();
        //mLandData = LandListDataControl.GetIns().getLandData(position);
        updateMyLandData();
    }

    private void Start()
    {
        //mLandData = LandListDataControl.GetIns().getLandData(position);
    }

    public void updateMyLandData()
    {
        mLandData = LandListDataControl.GetIns().getLandData(position);
        updateMyLand();
    }

    public void setLandDataTime(int time)
    {
        mLandData.countDownSecond = 0;
    }
    public LandEntity getMyLandEntity
    {
        get { return mLandData; }
    }

    //待解锁数据处理
    public bool setLockLandData(int index)
    {
        if (mLandData == null)
        {
            Debug.Log("setLockLandData数据为空");
            return false;
        }
        //金币数小于目标解锁价格
        if (CoinNumControl.GetIns().getMyCoin < mLandData.unlockPrice)
        {
            return false;
        }
        if (mLandData.status == 4)//待解锁
        {
            mLandData.status = 1;//待解锁变为未种植
            GameObject.Find("Form_land").GetComponent<FarmControl>().updateNextLand(index);//处理下一块地
        }
        else
        {
            updateMyLand();
            return false;
        }
        CoinNumControl.GetIns().payMyCoin(mLandData.unlockPrice);//解锁地块消耗金币
        return true;
    }
    //看视频 待解锁数据处理
    public bool setAdVideoLockLandData(int index)
    {
        if (mLandData == null)
        {
            Debug.Log("setAdVideoLockLandData数据为空");
            return false;
        }
        if (mLandData.status == 4)//待解锁
        {
            mLandData.status = 1;//待解锁变为未种植
            GameObject.Find("Form_land").GetComponent<FarmControl>().updateNextLand(index);//处理下一块地
        }
        else
        {
            updateMyLand();
            return false;
        }
        return true;
    }

    //买种子
    public bool buySeed(int index)
    {
        if (mLandData == null)
        {
            Debug.Log("buySeed数据为空");
            return false;
        }
        if (mLandData.status == 1)//未种植
        {
            mLandData.status = 2;//变为已种植
            if (index == 2)
            {
                GameObject.Find("guideParent").GetComponent<UserGuide>().setStep6(false);
            }
        }
        else
        {
            updateMyLand();
            return false;
        }
        return true;
    }
    //种植数据处理
    public bool setPlantFruitData(int index)
    {
        if (mLandData == null)
        {
            Debug.Log("setPlantFruitData数据为空");
            return false;
        }
        //landListJson = json;
        if (mLandData.seedNum <= 0)//种子数量不足
        {
            Debug.Log("种子数量不足 ID" + index);
            return false;
        }
        if (mLandData.status == 1)//未种植
        {
            mLandData.status = 2;//变为已种植
            --mLandData.seedNum;
        }
        else
        {
            updateMyLand();
            return false;
        }
        return true;
    }
    //加速水果变待收获数据处理
    public bool setSpeedFruitData(int index)
    {
        if (mLandData == null)
        {
            Debug.Log("setSpeedFruitData数据为空");
            return false;
        }
        if (mLandData.status == 2)//已种植未成熟
        {
            //加速时间
            mLandData.status = 3;//未成熟变成熟
        }
        else
        {
            updateMyLand();
            return false;
        }
        return true;
    }
    //收获水果数据处理
    public bool setRewardFruitData(int index)
    {
        if (mLandData == null)
        {
            Debug.Log("setRewardFruitData数据为空");
            return false;
        }
        //landListJson = json;
        if (mLandData.status == 3)//已成熟
        {
            mLandData.status = 1;//变为未种植
        }
        else
        {
            updateMyLand();
            return false;
        }
        CoinNumControl.GetIns().addMyCoin(mLandData.profit);//收获加金币
        return true;
    }
    //设置生虫时间
    public int setCreatWormTime()
    {
        if (mLandData == null)
        {
            return 0;
        }
        if (LandListDataControl.GetIns().isCreatWorm(position))
        {
            mLandData.stopSecond = setStopTime();
        }
        else
        {
            mLandData.stopSecond = 0;
        }
        return mLandData.stopSecond;
    }

    private int totalTime = 0;//总时长
    private int setStopTime()
    {
        int tempNextTime = 0;
        for (int i = 0; i < mLandData.growList.Length; i++)
        {
            if (landReward.getFruitNum < mLandData.growList[i].start)
            {
                totalTime = mLandData.growList[i].countdown;
                tempNextTime = 1;
                break;
            }
        }
        if (tempNextTime <= 0)
        {
            totalTime = mLandData.growList[mLandData.growList.Length - 1].countdown;
        }
        return Random.Range(0, (int)(totalTime * LandListDataControl.GetIns().getTimeRate));
    }

    public void updateMyLand()
    {
        StartCoroutine(updateUILand());
    }

    //刷新
    private IEnumerator updateUILand()
    {
        yield return null;
        if (landView != null)
        {
            landView.updateLand2(mLandData);
        }
        //transform.BroadcastMessage("updateLand", SendMessageOptions.DontRequireReceiver);
    }
}
