using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class NewLandControl : MonoBehaviour
{
    private int clickType = -1;
    private int fruit_id = 0;

    public Image paiziImg;
    public Text unLockPriceTv,unLockNameTv,onClickViewTv;
    public GameObject floatCodeObj;

    public void updateLand(LandEntity land)
    {
        Debug.Log("2222222");
        if (land.name == transform.tag)
        {
            fruit_id = land.id;
            Debug.Log(land.status+"name" + land.name);
            //0 未解锁 1 未种植 2 已种植（显示倒计时）3 已成熟 4 待解锁
            paiziImg.gameObject.SetActive(false);
            switch (land.status)
            {
                case 0:
                    setLandType(0);
                    setLandFruitType(land.id - 1,false);
                    setFloatCode(-2, 0);
                    clickType = -1;
                    break;
                case 1:
                    setLandType(1);
                    setLandFruitType(land.id - 1, false);
                    setFloatCode(0, land.id);
                    clickType = 2;
                    break;
                case 2:
                    setLandType(2);
                    setLandFruitType(land.id - 1, true);
                    setFloatCode(1, land.id);
                    clickType = 4;
                    break;
                case 3:
                    setLandType(2);
                    setLandFruitType(land.id - 1, true);
                    setFloatCode(2, land.id);
                    clickType = 3;
                    break;
                case 4:
                    paiziImg.gameObject.SetActive(true);
                    setLandType(0);
                    setLandFruitType(land.id - 1, false);
                    setFloatCode(-2,0);
                    unLockNameTv.text = "解锁" + land.name;
                    unLockPriceTv.text = land.unlockPrice.ToString();
                    clickType = 1;
                    break;
                default:
                    setLandType(0);
                    setLandFruitType(land.id - 1, false);
                    setFloatCode(-2, 0);
                    clickType = -1;
                    Debug.Log("default" + clickType);
                    break;
            }
            Debug.Log(transform.tag+"clickType" + clickType);
        }
    }


    //点击事件 解锁地块传1 种植种子传2 收获果实传3 （加速剂 4 另一个接口）
    public void onClickLanType()
    {
        Debug.Log(fruit_id + "clickType" + clickType);
        if (clickType != -1)
        {
            if (clickType == 4)
            {
                GameObject.Find("Form_land").GetComponent<FarmControl>().onClickUnlockLand(fruit_id, 4);
            }
            else
            {
                GameObject.Find("Form_land").GetComponent<FarmControl>().onClickUnlockLand(fruit_id, clickType);
            }
            clickType = -1;
        }else{
        
		}
        
    }

    [SerializeField]
    public Image landType,fruitType;
    //设置土地 0 绿色 1 未种植 2 已种植
    public void setLandType(int type)
    {
        Debug.Log(transform.tag + "设置土地" + type+"path"+ ResourcesUtil.getRes().getLandRes(type));
        try
        {
            landType.overrideSprite = Resources.Load(ResourcesUtil.getRes().getLandRes(type), typeof(Sprite)) as Sprite;
        }
        catch (Exception e)
        {
            Debug.Log(transform.tag + "设置土地Error" + e.Message);
        }
    }
    //设置农作物
    public void setLandFruitType(int type,bool isShow)
    {
        //Debug.Log(transform.tag + "设置农作物" + type);
        //fruitType.GetComponent<Image>().overrideSprite = Resources.Load(ResourcesUtil.getRes().getChengshuRes(type), typeof(Sprite)) as Sprite;
        //fruitType.GetComponent<Image>().SetNativeSize();
        //if (isShow)
        //{
        //    fruitType.color = new Color(1, 1, 1, 255);
        //}
        //else
        //{
        //    fruitType.color = new Color(1, 1, 1, 0);
        //}
    }
    private GameObject tempFloatCode;
    public void setFloatCode(int type,int id)
    {//点击事件可以放在浮动的脚本里去设置数据在通过点击获取transfarm获取
        Debug.Log("setFloatCodeid" + id);
        //if (type == -2)
        //{
        //    if (tempFloatCode != null)
        //    {
        //        Destroy(tempFloatCode);
        //    }
        //}
        //else
        //{
        //    if (tempFloatCode != null)
        //    {
        //        Destroy(tempFloatCode);
        //    }
        //    tempFloatCode = Instantiate(floatCodeObj, new Vector2(0, 0), Quaternion.identity);
        //    tempFloatCode.transform.SetParent(onClickViewTv.transform);
        //    //floatCode.transform.localPosition = new Vector2(landItemX[i], landItemY[i]);
        //    tempFloatCode.GetComponent<float_code>().setFloatImage(type, id);
        //}
       
    }
}
