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

public class DialogControl : MonoBehaviour, GameListenerHelper.IDialogListener
{

    //云朵加速剩余次数
    private int cloudSpeedNum;

    //云朵加速剩余次数
    public int CloudSpeedNum
    {
        get { return cloudSpeedNum; }
        set { cloudSpeedNum = value; }
    }

    ////加速剂不足
    //private int jiasujiNum = 0;
    ////种子不足
    //public int getJiasujiNum
    //{
    //    get { return jiasujiNum; }
    //    set { jiasujiNum = value; }
    //}
    //种子不足
    private int seedId = 0;
    private int seedVideoNum = 0;
    private int seedBuyPrice = 0;
    //种子不足
    public int SeedId
    {
        get { return seedId; }
        set { seedId = value; }
    }
    //种子不足弹窗
    public int SeedVideoNum
    {
        get { return seedVideoNum; }
        set { seedVideoNum = value; }
    }
    //种子不足
    public int SeedBuyPrice
    {
        get { return seedBuyPrice; }
        set { seedBuyPrice = value; }
    }

    //解锁土地
    private int unLockLandId = 0;

    //解锁土地
    public int UnLockLandId
    {
        get { return unLockLandId; }
        set { unLockLandId = value; }
    }



    public GameObject dialog;
    public GameObject get_coin;//获得金币
    public GameObject jiasu;//获取加速剂 不足
    public GameObject jiesuodikuai;//解锁地块
    public GameObject coin_not_enough;//金币不足
    public GameObject use_speed_water;//使用加速剂
    public GameObject cloud_speed;//云朵加速弹窗
    public GameObject homeWare;//仓库
    public GameObject seed_not_enough;//种子不足弹窗
    public GameObject seed_not_enough2;//种子不足弹窗2
    public GameObject order_success;//订单完成弹窗
    public GameObject order_fail;//订单未完成弹窗
    //public GameObject getGoodsDialog;//获得单个物品弹窗
    public GameObject orderReceiveDialog;//订单领取奖励弹窗
    public GameObject redPackPlan;//不可拆弹窗
    //sprite
    public GameObject goodsUnlockSprit;//解锁地块农作物
    public GameObject seedNoSprite;//种子不足弹窗显示物
    //Text
    public Text cloudSpeedNumTv;//云朵加速剩余次数

    public Text videoBuySeedPriceTv;//种子不足购买价格
    public Text videoBuySeedNumTv;//种子不足看视频剩余次数

    public Text jiasujiNumTv;//拥有加速剂的数量
    public Text jiasuNumTv;//加速剂不足可加速次数

    public Text orderRedPackMoneyTv;//订单红包领取金额
    public Text orderRedPackMoneyBtnTv;//订单红包按钮文案

    public GameObject get_box;//打开宝箱弹窗
    public Text boxnum;//宝箱剩余次数
    public Text jiasuGetNumtv;
    public Text jiasuGetNumBtntv;
    //果实种满时弹窗
    public GameObject ordergoods;
    public GameObject closeImg;
    public Text shouhuotv;
    public Text duihuantv;

    //Android
    private AndroidJavaObject mainActivity;
    private void OnGUI()
    {
        //cloudSpeedNumTv.text = "每天晚上0点重置视频次数(剩余" + cloudSpeedNum + "次)";
    }

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

        //showSeedNotEnough(1, 1, -1);
        //closeSeedNotEnough();
        showJieSuoDiKuai(-1);
        closeJieSuoDiKuai();
        //showOrderReceiveDialog(-1);
        //closeOrderReceiveDailog();
    }
    //不可拆红包弹窗
    public void showRedPackPlan()   
    {
        dialog.SetActive(true);
        redPackPlan.SetActive(true);
        GameObject.Find("GameManager").GetComponent<UnityToAndroid>().onEventUtil("show_chaihongbao_dialog", "展示拆红包弹窗");
    }

    public void closeRedPackPlan()
    {
        dialog.SetActive(false);
        redPackPlan.SetActive(false);
    }

    //获得金币弹窗               
    public void showGetCoin()
    {
        dialog.SetActive(true);
        get_coin.SetActive(true);
    }
    public void closeGetCoin()
    {
        dialog.SetActive(false);
        get_coin.SetActive(false);
    }

    //获得加速剂弹窗               
    public void showGetJiasu(int getnum, int num)
    {
        //jiasujiNum = num;
        dialog.SetActive(true);
        jiasu.SetActive(true);
        jiasuGetNumtv.text = "+" + getnum;
        //jiasuGetNumBtntv.text = "领取" + getnum + "瓶加速剂";
        jiasuNumTv.text = "每天晚上0点重置视频次数 (剩余" + num + "次)";
        GameObject.Find("GameManager").GetComponent<UnityToAndroid>().onEventUtil("show_free_get_accelerometer_dialog", "免费获得加速剂弹窗");
    }
    public void closeGetJiasu()
    {
        dialog.SetActive(false);
        jiasu.SetActive(false);
    }

    //解锁地块弹窗               
    public void showJieSuoDiKuai(int id)
    {
        if (id != -1)
        {
            GameObject.Find("GameManager").GetComponent<UnityToAndroid>().onEventUtil("show_unLock_farm_id_" + id, "解锁地块id_" + id);
        }
        else
        {
            id = 1;
        }
        unLockLandId = id;
        dialog.SetActive(true);
        jiesuodikuai.SetActive(true);
        if(id == 2)
        {
            GameObject.Find("guideParent").GetComponent<UserGuide>().setStep5(true);
        }
        if (id == 4)
        {
            GameObject.Find("guideParent").GetComponent<UserGuide>().setStep7(false);
        }
    }
    public void closeJieSuoDiKuai()
    {
        dialog.SetActive(false);
        jiesuodikuai.SetActive(false);
    }
    //金币不足弹窗               
    public void showCoinNotEnough()
    {
        dialog.SetActive(true);
        coin_not_enough.SetActive(true);
    }
    public void closeCoinNotEnough()
    {
        dialog.SetActive(false);
        coin_not_enough.SetActive(false);
    }

    public void closeCoinNotEnoughJump()
    {
        dialog.SetActive(false);
        coin_not_enough.SetActive(false);
        GameObject.Find("GameManager").GetComponent<UnityToAndroid>().onClickEveryDayWelfare();//跳转福利页
    }
    public void closeCoinJumpToTask()
    {
        dialog.SetActive(false);
        coin_not_enough.SetActive(false);
        GameObject.Find("GameManager").GetComponent<UnityToAndroid>().onClickGetCoin();//跳转任务页
    }

    public Button seedBtnReceive;
    //种子不足弹窗               
    public void showSeedNotEnough(int id, int buyPrice, int num)
    {
        seedId = id;
        seedBuyPrice = buyPrice;
        //dialog.SetActive(true);
        //if (id == 8 || id == 9)
        //{
        //    seedVideoNum = -1;
        //    seedBtnReceive.gameObject.SetActive(false);
        //    seed_not_enough2.SetActive(true);
        //}
        //else
        //{
        //    seedVideoNum = num;
        //    seedBtnReceive.gameObject.SetActive(true);
        //    seed_not_enough.SetActive(true);
        //}
        seedVideoNum = num;
        //seedBtnReceive.gameObject.SetActive(true);
        //seed_not_enough.SetActive(true);
        if (num != -1)
        {
            GameObject.Find("GameManager").GetComponent<UnityToAndroid>().onEventUtil("show_Insufficient_seeds_dialog", "没有种子弹窗");
            try
            {
                mainActivity.Call("seedNotEnoughDialog", id, buyPrice, num);
            }
            catch (Exception e)
            {
                Debug.Log("seedNotEnoughDialog ERROR" + e.Message);
                dialog.SetActive(true);
                seedBtnReceive.gameObject.SetActive(true);
                seed_not_enough.SetActive(true);
            }
        }
    }
    public void closeSeedNotEnough()
    {
        dialog.SetActive(false);
        seed_not_enough.SetActive(false);
        seed_not_enough2.SetActive(false);
    }
    //使用加速剂弹窗               
    public void showUseSpeedWater(int num)
    {
        dialog.SetActive(true);
        use_speed_water.SetActive(true);
        jiasujiNumTv.text = "已拥有 <color=#FF9128>" + num + "</color>";
    }
    public void closeUseSpeedWater()
    {
        dialog.SetActive(false);
        use_speed_water.SetActive(false);
    }


    //使用云朵加速弹窗
    public void showCloudSpeedDialog()
    {
        GameObject.Find("Music_Background").GetComponent<MusicPlayerControl>().playBtnAudio();
        int time = GameObject.Find("TitleView").GetComponent<TitleControl>().CloudSpeedTime;
        GameObject.Find("GameManager").GetComponent<UnityToAndroid>().onEventUtil("click_cloud_speed_up", "云朵加速入口点击");
        Debug.Log(cloudSpeedNum + "cloudSpeedNum" + time + "time");
        GameObject.Find("guideParent").GetComponent<UserGuide>().setStep6(true);
        if (cloudSpeedNum > 0 && time <= 0)
        {
            if (mainActivity != null)
            {
                try
                {
                    mainActivity.Call("checkCloud", new GameListenerHelper.ICallBackListener(this));
                }
                catch (Exception e)
                {
                    onError("CloudError");
                    Debug.Log("checkCloud ERROR" + e.Message);
                }
            }
            else
            {
                onError("CloudError");
            }
        }
        else if (cloudSpeedNum <= 0)
        {
            GameObject.FindGameObjectWithTag("toast").GetComponent<ToastManager>().CreatToast("云朵加速已用完，明天再用吧~");
            return;
        }
        else if (time > 0)
        {
            GameObject.FindGameObjectWithTag("toast").GetComponent<ToastManager>().CreatToast("稍等片刻，云朵马上就来~");
        }
    }
    public void closeCloudSpeedDialog()
    {
        dialog.SetActive(false);
        cloud_speed.SetActive(false);
    }

    //仓库弹窗
    public void showHomeWareDialog()
    {
        //GameObject.Find("GameManager").GetComponent<UnityToAndroid>().onEventUtil("show_cangku_dialog", "仓库弹窗曝光");
        //homeWare.SetActive(true);
        try
        {
            mainActivity.Call("showWareHouseDialog");
        }
        catch (Exception e)
        {
            Debug.Log("showHomeWareDialog ERROR" + e.Message);
        }
    }

    public void closeHomeWareDialog()
    {
        homeWare.SetActive(false);
    }
    public Text orderSuccessTitle;
    private int tempOrderLevel;
    //果实种满弹窗
    int did;
    string cash;
    public void showOrderSuccess(int id, int ownNum, string cashStr)
    {
        dialog.SetActive(true);
        order_success.SetActive(true);
        did = id;
        cash = cashStr;
        Debug.Log("id" + id + "ownNum" + ownNum + "" + cashStr);
        //tempOrderLevel = orderLevel;
        duihuantv.text = "可兑换 " + "<color=#E85740>" + cashStr + "元</color> " + "微信红包";
        switch (id)
        {
            case 1:
                shouhuotv.text = "已收获 " + "<color=#E85740>" + ownNum + "</color> " + "棵白菜";
                break;
            case 2:
                shouhuotv.text = "已收获 " + "<color=#E85740>" + ownNum + "</color> " + "个番茄";
                break;
            case 3:
                shouhuotv.text = "已收获 " + "<color=#E85740>" + ownNum + "</color> " + "个南瓜";
                break;
            case 4:
                shouhuotv.text = "已收获 " + "<color=#E85740>" + ownNum + "</color> " + "个萝卜";
                break;
            case 5:
                shouhuotv.text = "已收获 " + "<color=#E85740>" + ownNum + "</color> " + "个玉米";
                break;
            case 6:
                shouhuotv.text = "已收获 " + "<color=#E85740>" + ownNum + "</color> " + "个茄子";
                break;
            case 7:
                shouhuotv.text = "已收获 " + "<color=#E85740>" + ownNum + "</color> " + "个西瓜";
                break;
            case 8:
                shouhuotv.text = "已收获 " + "<color=#E85740>" + ownNum + "</color> " + "个葡萄";
                break;
            case 9:
                shouhuotv.text = "已收获 " + "<color=#E85740>" + ownNum + "</color> " + "个草莓";
                break;
            default:
                shouhuotv.text = "已收获 " + "<color=#E85740>" + ownNum + "</color> " + "棵白菜";
                break;
        }
       
        closeImg.SetActive(!LandListDataControl.GetIns().getIsShowGuide);//引导 隐藏关闭按钮
        ordergoods.GetComponent<WareItem>().setFruitType(id);

    }
    public void duihuanclick()
    {
        closeOrderSuccess();
        GameObject.Find("GameManager").GetComponent<UnityToAndroid>().duihuanclick(did, cash);
    }
    public void closeOrderSuccess()
    {
        dialog.SetActive(false);
        order_success.SetActive(false);
        GameObject.Find("OrderView").GetComponent<OrderControl>().getOrderData();
    }
    //订单未完成
    public void showOrderFailDialog()
    {
        dialog.SetActive(true);
        order_fail.SetActive(true);
    }
    public void closeOrderFailDailog()
    {
        dialog.SetActive(false);
        order_fail.SetActive(false);
    }
    private int receiveMoney;
    //订单奖励领取
    public void showOrderReceiveDialog(int money)
    {
        if (money != -1)
        {
            GameObject.Find("Music_Background").GetComponent<MusicPlayerControl>().playgetRewardAudio();
            receiveMoney = money;
            GameObject.Find("GameManager").GetComponent<UnityToAndroid>().onEventUtil("show_reward_redPack_" + tempOrderLevel, "获得红包" + tempOrderLevel + "");
        }
        dialog.SetActive(true);
        orderReceiveDialog.SetActive(true);
        if (money == -1)
        {
            money = 0;
        }
        orderRedPackMoneyTv.text = money + "<size=50>元</size>";
        if (tempOrderLevel == 1)
        {
            orderRedPackMoneyBtnTv.text = "前往提现";
        }
        else
        {
            orderRedPackMoneyBtnTv.text = "开心收下";
        }
        GameObject.Find("TitleView").GetComponent<TitleControl>().getHomeInfo();//刷新title

    }
    public void closeOrderReceiveDailog()
    {
        dialog.SetActive(false);
        orderReceiveDialog.SetActive(false);
        GameObject.Find("GameManager").GetComponent<UnityToAndroid>().onEventUtil("click_withdraw_redPack_" + tempOrderLevel, "获得红包" + tempOrderLevel + "级");
        if (tempOrderLevel == 1)
        {
            GameObject.Find("GameManager").GetComponent<UnityToAndroid>().onClickWithDraw(1);//跳转提现页
        }
    }

    public void showGetBox()
    {
        dialog.SetActive(true);
        get_box.SetActive(true);
        int num = GameObject.Find("TitleView").GetComponent<TitleControl>().getBoxNum;
        boxnum.text = "每天晚上0点重置视频次数 (剩余" + num + "次)";
        GameObject.Find("GameManager").GetComponent<UnityToAndroid>().onEventUtil("show_flyBox_dialog", "飞行宝箱弹窗");
    }
    public void closeGetBox()
    {
        dialog.SetActive(false);
        get_box.SetActive(false);
    }

    public void onGetCheckCloudListResult(GuideEntity bean)
    {
        if (bean.status == 0)
        {
            //剩余看视频次数
            mainActivity.Call("showCloudDialog", LandListDataControl.GetIns().getCloudTime(), cloudSpeedNum);
            //dialog.SetActive(true);
            //cloud_speed.SetActive(true);
            //GameObject.Find("GameManager").GetComponent<UnityToAndroid>().onEventUtil("show_cloud_speed_up_dialog", "云朵加速弹窗");
        }
        else
        {
            GameObject.FindGameObjectWithTag("toast").GetComponent<ToastManager>().CreatToast("没有土地可以加速，先去种植吧～");
        }

    }

    public void onError(string message)
    {
        GameObject.FindGameObjectWithTag("toast").GetComponent<ToastManager>().CreatToast("没有土地可以加速，先去种植吧～");
    }
}
