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

public class HomeWareControl : MonoBehaviour, GameListenerHelper.IHomeWareListener
{

    public GameObject wareGrid;

    public GameObject wareItem;

    public GameObject[] wareList;
    public GameObject[] tabList;


    private int clickType=1;

    private float[] wareItemX = { -220f, -70f, 70f, 220f, -220f, -70f, 70f, 220f, -220f };
    private float[] wareItemY = { 190f, 190f, 190f, 190f, 20f, 20f, 20f, 20f, -160f };
    //Android
    private AndroidJavaObject mainActivity;

    private void Awake()
    {
        AndroidJavaClass jc = new AndroidJavaClass("com.unity3d.player.UnityPlayer");
        mainActivity = jc.GetStatic<AndroidJavaObject>("currentActivity");
    }
    private void Start()
    {
        //setContentList(1);
    }

    private void OnEnable()
    {
        Debug.Log("wxw");
        setContentList(clickType);
    }

    public void setContentList(int type)
    {
        if (tabList[type - 1].GetComponent<Toggle>().isOn)
        {
            clickType = type;
            Debug.Log("tab" + type);
            try
            {
                mainActivity.Call("getWareHouseData", new GameListenerHelper.ICallBackListener(this), type);
            }
            catch (Exception e)
            {
                Debug.Log("getWareHouseData ERROR" + e.Message);

                if (type == 3)
                {
                    JsonReader fr = new JsonReader("{\"landList\":[{\"id\":1,\"ownNum\":1},{\"id\":2,\"ownNum\":33},{\"id\":3,\"ownNum\":33},{\"id\":4,\"ownNum\":33}]}");
                    LandListEntity bean = JsonMapper.ToObject<LandListEntity>(fr);
                    onHomeWareListResult(bean.landList);
                }
                else
                {
                    JsonReader fr = new JsonReader("{\"landList\":[{\"id\":1,\"ownNum\":1},{\"id\":2,\"ownNum\":33},{\"id\":3,\"ownNum\":33},{\"id\":4,\"ownNum\":33},{\"id\":5,\"ownNum\":33},{\"id\":6,\"ownNum\":33},{\"id\":7,\"ownNum\":33},{\"id\":8,\"ownNum\":33},{\"id\":9,\"ownNum\":33}]}");
                    LandListEntity bean = JsonMapper.ToObject<LandListEntity>(fr);
                    onHomeWareListResult(bean.landList);
                }

            }
        }

    }

    //private void Update()
    //{
    //    if (isUpdateSuccess)
    //    {
    //        updateWareHouseList(tempWareList);
    //        isUpdateSuccess = false;
    //    }
    //}
    private LandEntity[] tempWareList;
    private void updateWareHouseList(LandEntity[] landList)
    {
        if (landList != null)
        {
            for (int i = 0; i < wareList.Length; i++)
            {
                if (wareList[i] != null)
                {
                    wareList[i].SetActive(true);
                }

                if (landList.Length < wareList.Length)
                {
                    if (i < landList.Length)//
                    {
                        if (wareList[i] == null)
                        {
                            GameObject ware = Instantiate(wareItem, new Vector2(0, 0), Quaternion.identity);
                            ware.transform.SetParent(wareGrid.transform);
                            ware.transform.localPosition = new Vector2(wareItemX[i], wareItemY[i]);
                            switch (clickType)
                            {
                                case 1:
                                    ware.GetComponent<WareItem>().setFruitType(landList[i].id);
                                    break;
                                case 2:
                                    ware.GetComponent<WareItem>().setSeedType(landList[i].id);
                                    break;
                                case 3:
                                    ware.GetComponent<WareItem>().setJsjType(landList[i].id);
                                    break;
                            }
                            ware.GetComponent<WareItem>().setWareNum(landList[i].ownNum);
                            wareList[i] = ware;
                        }
                        else
                        {
                            switch (clickType)
                            {
                                case 1:
                                    wareList[i].GetComponent<WareItem>().setFruitType(landList[i].id);
                                    break;
                                case 2:
                                    wareList[i].GetComponent<WareItem>().setSeedType(landList[i].id);
                                    break;
                                case 3:
                                    wareList[i].GetComponent<WareItem>().setJsjType(landList[i].id);
                                    break;
                            }
                            wareList[i].GetComponent<WareItem>().setWareNum(landList[i].ownNum);
                        }
                    }
                    else
                    {
                        if (wareList != null)
                        {
                            wareList[i].SetActive(false);
                        }
                    }
                }
                else
                {
                    if (wareList[i] == null)
                    {
                        GameObject ware = Instantiate(wareItem, new Vector2(0, 0), Quaternion.identity);
                        ware.transform.SetParent(wareGrid.transform);
                        ware.transform.localPosition = new Vector2(wareItemX[i], wareItemY[i]);
                        switch (clickType)
                        {
                            case 1:
                                ware.GetComponent<WareItem>().setFruitType(landList[i].id);
                                break;
                            case 2:
                                ware.GetComponent<WareItem>().setSeedType(landList[i].id);
                                break;
                            case 3:
                                ware.GetComponent<WareItem>().setJsjType(landList[i].id);
                                break;
                        }
                        ware.GetComponent<WareItem>().setWareNum(landList[i].ownNum);
                        wareList[i] = ware;
                    }
                    else
                    {
                        switch (clickType)
                        {
                            case 1:
                                wareList[i].GetComponent<WareItem>().setFruitType(landList[i].id);
                                break;
                            case 2:
                                wareList[i].GetComponent<WareItem>().setSeedType(landList[i].id);
                                break;
                            case 3:
                                wareList[i].GetComponent<WareItem>().setJsjType(landList[i].id);
                                break;
                        }
                        wareList[i].GetComponent<WareItem>().setWareNum(landList[i].ownNum);
                    }
                }
            }
        }
    }

    public void onHomeWareListResult(LandEntity[] landList)
    {
        tempWareList = landList;
        Debug.Log("onHomeWareListResult" + landList.Length);
        updateWareHouseList(landList);
        //int nCount = wareGrid.transform.childCount;
        //for (int i = 0; i < nCount; i++)
        //{
        //    GameObject ware_items = wareGrid.transform.GetChild(i).gameObject;
        //    Destroy(ware_items);
        //}

    }

    public void onError(string message)
    {
        Debug.Log("tab" + message);
    }
}
