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

public class OrderControl : MonoBehaviour, GameListenerHelper.IOrderListener
{

    public Text orderLevelTv;
    public Text getMoneyBtnTv;
    public Text orderFailTitleTv;
    //title
    public GameObject orderGrid;
    public GameObject order_item;
    //订单弹窗
    public GameObject order_List_GridDialog;
    public GameObject order_List_itemDialog;
    public Image[] orderItemFruits;//果实图片
    public Image[] orderItemProGreen;//绿色进度条
    public Image[] orderItemProRed;//红色进度条
    public Text[] orderItemDesc;//进度描述
    private string dialogOpenKey = "NewPersionWithDraw";//新人提现弹窗引导只引导一次
    public GameObject[] orderListGame;
    private string[] orderTag = { "order_1", "order_2", "order_3","order_4" };
    private float[] wareItemX = { -170f, -55f, 60f, 170f };
    private float[] wareItemY = { 20f, 20f, 20f, 20f };
    //dialogY
    private float[] orderListItemY = { 150f, -50f, 50f, -150f };

    private bool isUpdateSuccess = false;
    private HomeListEntity homeListEntity;
    //Android
    private AndroidJavaObject mainActivity;
    private void Awake()
    {
        AndroidJavaClass jc = new AndroidJavaClass("com.unity3d.player.UnityPlayer");
        mainActivity = jc.GetStatic<AndroidJavaObject>("currentActivity");
    }

    void Start()
    {
        getOrderData();
    }


    private void Update()
    {
        //if (isUpdateSuccess)
        //{
        //    updateOrderList(homeListEntity.homeList);
        //    isUpdateSuccess = false;
        //}
    }

    private int type = 1;//  1 已完成  0 未完成
    private int orderLevelNum;
    private int orderMoneyNum;
    public void onClickOrderDialog()
    {
        try
        {
            if (type == 1)
            {
                GameObject.Find("GameManager").GetComponent<UnityToAndroid>().onEventUtil("show_complete_order_level_" + orderLevelNum, "完成" + orderLevelNum + "级订单");
                // GameObject.Find("dialog").GetComponent<DialogControl>().showOrderSuccess(orderLevelNum);
            }
            else
            {
                GameObject.Find("GameManager").GetComponent<UnityToAndroid>().onClickShowOrderList();
                //GameObject.Find("dialog").GetComponent<DialogControl>().showOrderFailDialog();
            }
        }
        catch (Exception e)
        {
            Debug.Log("onClickOrderDialog Error" + e.Message);
        }
    }

    //获取订单列表
    public void getOrderData()
    {
        try
        {
            mainActivity.Call("getOrderData", new GameListenerHelper.ICallBackListener(this));
        }
        catch (Exception e)
        {
            Debug.Log("getOrderData ERROR" + e.Message);
            //string resu2 = "{\"orderLevel\":0,\"farmOrderDataList\":[ {\"id\":1,\"ownNum\":1,\"targetNum\":3,\"imgUrl\":null},{\"id\":2,\"ownNum\":2,\"targetNum\":1,\"imgUrl\":null},{\"id\":3,\"ownNum\":0,\"targetNum\":1,\"imgUrl\":null},{\"id\":4,\"ownNum\":3,\"targetNum\":1,\"imgUrl\":null}],\"awardCash\":5}";
            // string resu3 = "{\"orderLevel\":0,\"farmOrderDataList\":[ {\"id\":3,\"ownNum\":0,\"targetNum\":1,\"imgUrl\":null},{\"id\":4,\"ownNum\":3,\"targetNum\":1,\"imgUrl\":null}],\"awardCash\":5}";
           // string resu4 = "{\"homeList\":[ {\"id\":1,\"ownNum\":0,\"targetNum\":2,\"imgUrl\":null},{\"id\":2,\"ownNum\":3,\"targetNum\":11,\"imgUrl\":null},{\"id\":3,\"ownNum\":3,\"targetNum\":10,\"imgUrl\":null},{\"id\":4,\"ownNum\":3,\"targetNum\":10,\"imgUrl\":null}]}";
           // //JsonReader fr = new JsonReader(resu2);
           // //LandListEntity bean = JsonMapper.ToObject<LandListEntity>(fr);
           // //LandListEntity bean1 = JsonUtility.FromJson<LandListEntity>(resu2);
           // //onGetOrderListResult(bean1);
           // //LandListEntity bean2 = JsonUtility.FromJson<LandListEntity>(resu2);
           // //onGetOrderListResult(bean2);
           // HomeListEntity bean1 = JsonUtility.FromJson<HomeListEntity>(resu4);
           //onGetOrderListResult(bean1);
        }
    }

    //领取订单
    public void getReceiveOrder()
    {
        try
        {
            //GameObject.Find("dialog").GetComponent<DialogControl>().closeOrderSuccess();//关闭成功领取弹窗
            mainActivity.Call("getOrderReceive", new GameListenerHelper.ICallBackListener(this));
            GameObject.Find("GameManager").GetComponent<UnityToAndroid>().onEventUtil("click_receive_reward_order_level_" + orderLevelNum, "领取" + orderLevelNum + "级订单");
        }
        catch (Exception e)
        {
            Debug.Log("getOrderReceive ERROR" + e.Message);
        }
    }

    private HomeOrderEntity[] orderList;

    public HomeOrderEntity[] getOrderItem
    {
        get { return orderList; }
    }

    public void onGetOrderListResult(HomeListEntity order)
    {

        homeListEntity = order;
        StartCoroutine(UpdateOrderList(homeListEntity.homeList));
       

        // isUpdateSuccess = true;
    }



    public void onError(string message)
    {

    }

    public void onOrderReceiveResult(StatusEntity order)
    {
        getOrderData();//刷新ordertitle
        GameObject.Find("dialog").GetComponent<DialogControl>().closeOrderSuccess();//关闭成功领取弹窗
        GameObject.Find("dialog").GetComponent<DialogControl>().showOrderReceiveDialog(order.awardCash);//显示成功领取弹窗
        GameObject.Find("TitleView").GetComponent<TitleControl>().getHomeInfo();
        Debug.Log("领取订单成功" + order.awardCash);
    }


    private IEnumerator UpdateOrderList(HomeOrderEntity[] order)
    {
        // type = order.status;
        // orderLevelTv.text = "完成" + order.orderLevel + "级订单";
        //   getMoneyBtnTv.text = "获得" + order.awardCash + "元";
        //Debug.Log("订单 数据" + order.farmOrderDataList.Length);
        //  orderLevelNum = order.ownNum;
        //orderMoneyNum = order.targetNum;
        yield return null;
        Debug.Log("size" + order.Length + "");

        if (order != null)
        {
            if (order.Length < 3)
            {

                for (int i = 0; i < order.Length; i++)
                {

                    order[i].name = orderTag[i];
                    orderListGame[i].GetComponent<Order_item>().setOrderItemShow();
                    orderListGame[i].GetComponent<Order_item>().updateOrderItem(order[i]);
                }
            }
            else
            {
                for (int i = 0; i < 4; i++)
                {

                    order[i].name = orderTag[i];
                    orderListGame[i].GetComponent<Order_item>().setOrderItemShow();
                    orderListGame[i].GetComponent<Order_item>().updateOrderItem(order[i]);

                }
            }
        }
        //try
        //{
        //    if (type == 1)
        //    {
        //        //GameObject.Find("GameManager").GetComponent<UnityToAndroid>().onEventUtil("show_complete_order_level_" + order.orderLevel, "完成" + order.orderLevel + "级订单");
        //        //GameObject.Find("dialog").GetComponent<DialogControl>().showOrderSuccess(order.orderLevel);
        //    }
        //}
        //catch (Exception e)
        //{
        //    Debug.Log("onClickOrderDialog Error" + e.Message);
        //}
    }
    public void editHomeList(int ids)
    {

        if (homeListEntity != null)
        {
            int id = ids + 1;
            orderList = homeListEntity.homeList;
            for (int i = 0; i < orderList.Length; i++)
            {
                
                Debug.Log("editHomeList id" + id);
               
                if (orderList[i].id == id)
                {
                    orderList[i].ownNum++;
                   StartCoroutine(UpdateOrderList(orderList));
                    if (PlayerPrefs.GetInt(dialogOpenKey, -1) == -1)
                    {
                        if (orderList[i].ownNum >= orderList[i].targetNum)
                        {
                            onEventUtil("show_keduihuan_dialog_" + id, id + "满了可兑换现金弹窗曝光");
                            GameObject.Find("dialog").GetComponent<DialogControl>().showOrderSuccess(id, orderList[i].ownNum, orderList[i].cashStr);
                            PlayerPrefs.SetInt(dialogOpenKey, 1);
                        }
                    }
                }

            }
        }
    }
    public void onEventUtil(string key, string value)
    {
        try
        {
            mainActivity.Call("GameEvent", key, value);
        }
        catch (Exception e)
        {
            Debug.Log("onEventUtil ERROR" + e.Message);
        }
    }
}
