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

public class Order_item : MonoBehaviour
{
    //样式
    public enum SeedType
    {
        //果实
        BAI_CAI,
        FAN_QIE,
        NAN_GUA,
        LUO_BO,
        YU_MI,
        QIE_ZI,
        XI_GUA,
        PU_TAO,
        CAO_MEI
    }
    [System.Serializable]
    public struct SeedSprite
    {
        public SeedType seedType;
        public Sprite seedsprite;
    }

    public SeedSprite[] Sprites;
    public Dictionary<SeedType, Sprite> ColorDic;
    public SeedType Spriteflower;

    public SpriteRenderer spriteCompent;
    //title order   
    public Text orderNumTv,orderprice,orderunit;
    public Image orderProGreed,orderProRed,orderProGray;
    //dialog order
    public Text orderNameDialogTv, orderNumDialogTv;
    public Image orderProDialogGreed;

    private void Awake()
    {
        //spriteCompent = transform.GetComponent<SpriteRenderer>();
        ColorDic = new Dictionary<SeedType, Sprite>();

        for (int i = 0; i < Sprites.Length; i++)
        {
            if (!ColorDic.ContainsKey(Sprites[i].seedType))
            {
                ColorDic.Add(Sprites[i].seedType, Sprites[i].seedsprite);
            }
        }
        setOrderItemDissmiss();
    }

    private void OnEnable()
    {
       // LandEntity[] order_list = GameObject.Find("OrderView").GetComponent<OrderControl>().getOrderItem;
        //if(order_list!=null){
         //   if (CompareTag("order_1"))
         //   {
         //       updateOrderItem(order_list[0]);
        //    }
        //    if (CompareTag("order_2"))
        //    {
        //        updateOrderItem(order_list[1]);
        //    }
        //    if (CompareTag("order_3"))
         //   {
         //       updateOrderItem(order_list[2]);
         //   }
         //   if (CompareTag("order_4"))
         //   {
         //       updateOrderItem(order_list[3]);
         //   }
	//	}
        
    }

    public void updateOrderItem(HomeOrderEntity order)
    {
        if (order.name == transform.tag)
        {
            Debug.Log("OnEnable" + transform.tag);
            setSeedType(order.id);
            setOrderNum(order.ownNum, order.targetNum);
            setprice(order.cashStr);
        }
    }

    public void setOrderItemDissmiss(){
         spriteCompent.color = new Color(1, 1, 1, 0);
         orderNumTv.text="";
         orderProGray.fillAmount=0;
         orderProGreed.fillAmount=0;
         orderProRed.fillAmount=0;
	}

    public void setOrderItemShow(){
         spriteCompent.color = new Color(1, 1, 1, 255);
         //orderProGray.fillAmount=1;
         //orderProGreed.fillAmount=1;
         //orderProRed.fillAmount=1
	}

    public void setprice(string cashStr)
    {
        orderprice.text = cashStr;
        orderunit.text = "元";
    }
    //种子
    public void setSeedType(int id)
    {
        SeedType type;
        switch (id)
        {
            case 1:
                type = SeedType.BAI_CAI;
                break;
            case 2:
                type = SeedType.FAN_QIE;
                break;
            case 3:
                type = SeedType.NAN_GUA;
                break;
            case 4:
                type = SeedType.LUO_BO;
                break;
            case 5:
                type = SeedType.YU_MI;
                break;
            case 6:
                type = SeedType.QIE_ZI;
                break;
            case 7:
                type = SeedType.XI_GUA;
                break;
            case 8:
                type = SeedType.PU_TAO;
                break;
            case 9:
                type = SeedType.CAO_MEI;
                break;
            default:
                type = SeedType.BAI_CAI;
                break;
        }
        Spriteflower = type;
        if (ColorDic.ContainsKey(type))
        {
            spriteCompent.sprite = ColorDic[type];
        }
    }
    //title数量
    public void setOrderNum(int ownNum,int targetNum)
    {
        if (ownNum >= targetNum)//绿色 完成
        {
            orderNumTv.text =  ownNum  +"/"+ targetNum ;
            orderProGreed.gameObject.SetActive(true);
            orderProRed.gameObject.SetActive(false);
             orderProGray.fillAmount=1;
              orderProGreed.fillAmount=1;
              orderProRed.fillAmount=0;
        }
        else//红色 未达成
        {
            orderNumTv.text = "<color=#AF774F>" + ownNum + "</color><color=#AF774F>/" + targetNum + "</color>";
            orderProGreed.gameObject.SetActive(false);
            orderProRed.gameObject.SetActive(true);
             orderProGray.fillAmount=1;
            orderProGreed.fillAmount=0;
            orderProRed.fillAmount = ownNum / (float)targetNum;
        }
    }

    //dialog数量
    public void setOrderDialogListNum(int ownNum, int targetNum, string name)
    {
        orderNameDialogTv.text = name;
        orderNumDialogTv.text = ownNum + "/" + targetNum;
        orderProDialogGreed.fillAmount = ownNum / (float)targetNum;
    }
}
