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

public class LandUnLockControl : MonoBehaviour
{
    public Text unLockPriceTv;
    public Text unLockNameTv;
    public Image videoIconImg;
    public int position = -1;
    private LandListEvent landEvent;
    private FarmControl landFarm;
    public LandDataControl mLandData;
    public LandAnimationControl landAnim;
    private void Awake()
    {
        landEvent = GameObject.Find("Form_land")?.GetComponent<LandListEvent>();
        landFarm = GameObject.Find("Form_land")?.GetComponent<FarmControl>();
    }

    private void Start()
    {
        CoinNumControl.GetIns().OnVariableChange += coinChange;
        if (position != -1)
        {
            //解锁价格
            //unLockPriceTv.text = LandListDataControl.GetIns().getLandListDataPos(position).unlockPrice.ToString();
            //unLockPriceTv.text = mLandData.getMyLandEntity.unlockPrice.ToString();
            if (mLandData.getMyLandEntity.targetVideoNum > 0)
            {
                unLockNameTv.text = System.Environment.NewLine + "解锁" + mLandData.getMyLandEntity.name;
            }
            else
            {
                videoIconImg.gameObject.SetActive(false);//隐藏看视频图标
                unLockNameTv.text = "解锁" + mLandData.getMyLandEntity.name;
            }
            coinChange(CoinNumControl.GetIns().getMyCoin);
        }
        landAnim.playUnLockTipAnim();
    }
    //监听金币数量变化
    private void coinChange(int num)
    {
        //if (position != -1)
        //{
        //    //解锁价格
        //    if (CoinNumControl.GetIns().getMyCoin > mLandData.getMyLandEntity.unlockPrice)
        //    {
        //        landAnim.playUnLockTipAnim();
        //    }
        //    else
        //    {
        //        landAnim.stopUnLockTipAnim();
        //    }
        //}
    } 

    //解锁地块
    public void onClickUnLockPlant()
    {
        if (position != -1)
        {
            landFarm.setOnclickLandId = position;
            GameObject.Find("Music_Background").GetComponent<MusicPlayerControl>().playBtnAudio();

            //需要看视频解锁
            if (mLandData.getMyLandEntity.targetVideoNum > 0)
            {   //看视频
                landEvent.lookAdVideoUnlockLand();
            }
            else
            {   //不需要看视频解锁 直接解锁
                setUnLockLand();
            }



            //if (mLandData.setLockLandData(position))
            //{
            //    Debug.Log("解锁成功");
            //    landEvent.updateLandList(1, position);
            //    mLandData.updateMyLand();
            //    GameObject.Find("dialog").GetComponent<DialogControl>().showJieSuoDiKuai(position+1);
            //}
            //else
            //{
            //    Debug.Log("解锁失败 弹出金币不足弹窗");
            //    GameObject.Find("dialog").GetComponent<DialogControl>().showCoinNotEnough();
            //}
            GameObject.Find("GameManager").GetComponent<UnityToAndroid>().onEventUtil("unlock_" + (position+1), "点击解锁土地_" + (position + 1));
        }
    }

    //解锁土地
    public void setUnLockLand()
    {
        if (mLandData.setAdVideoLockLandData(position))
        {
            Debug.Log("解锁成功" + position);
            landEvent.updateLandList(1, position, 0);
            mLandData.updateMyLand();
            GameObject.Find("dialog").GetComponent<DialogControl>().showJieSuoDiKuai(position + 1);
        }
        else
        {
            Debug.Log("解锁失败" + position);
        }
    }

}
