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

public class LandListEvent : MonoBehaviour
{
    private AndroidJavaObject mainActivity;
    private void Awake()
    {
        //string json = "{\"landList\":[{\"id\":1,\"name\":\"白菜\",\"unlockPrice\":0,\"seedPrice\":5,\"profit\":7,\"second\":60,\"countDownSecond\":0,\"status\":4,\"seedNum\":0,\"cloudSecond\":10,\"acceleratorsSecond\":5,\"guideStatus\":0,\"growList\":[{\"start\":1,\"countdown\":60},{\"start\":31,\"countdown\":120},{\"start\":41,\"countdown\":300},{\"start\":46,\"countdown\":480},{\"start\":51,\"countdown\":600},{\"start\":71,\"countdown\":720},{\"start\":81,\"countdown\":900},{\"start\":91,\"countdown\":1200},{\"start\":96,\"countdown\":1800}],\"fruitNum\":45,\"acceleratorsNum\":0},{\"id\":2,\"name\":\"番茄\",\"unlockPrice\":5,\"seedPrice\":10,\"profit\":15,\"second\":120,\"countDownSecond\":0,\"status\":1,\"seedNum\":0,\"cloudSecond\":10,\"acceleratorsSecond\":5,\"guideStatus\":0,\"growList\":[{\"start\":1,\"countdown\":120},{\"start\":31,\"countdown\":240},{\"start\":41,\"countdown\":360},{\"start\":46,\"countdown\":480},{\"start\":51,\"countdown\":600},{\"start\":71,\"countdown\":900},{\"start\":81,\"countdown\":1200},{\"start\":91,\"countdown\":1200},{\"start\":96,\"countdown\":1800}],\"fruitNum\":47,\"acceleratorsNum\":0},{\"id\":3,\"name\":\"南瓜\",\"unlockPrice\":10,\"seedPrice\":20,\"profit\":32,\"second\":240,\"countDownSecond\":0,\"status\":1,\"seedNum\":0,\"cloudSecond\":10,\"acceleratorsSecond\":5,\"guideStatus\":0,\"growList\":[{\"start\":1,\"countdown\":240},{\"start\":31,\"countdown\":360},{\"start\":41,\"countdown\":480},{\"start\":46,\"countdown\":600},{\"start\":51,\"countdown\":600},{\"start\":71,\"countdown\":720},{\"start\":81,\"countdown\":900},{\"start\":91,\"countdown\":1200},{\"start\":96,\"countdown\":1800}],\"fruitNum\":38,\"acceleratorsNum\":0},{\"id\":4,\"name\":\"萝卜\",\"unlockPrice\":50,\"seedPrice\":30,\"profit\":51,\"second\":480,\"countDownSecond\":0,\"status\":3,\"seedNum\":0,\"cloudSecond\":10,\"acceleratorsSecond\":5,\"guideStatus\":0,\"growList\":[{\"start\":1,\"countdown\":480},{\"start\":31,\"countdown\":480},{\"start\":41,\"countdown\":600},{\"start\":46,\"countdown\":600},{\"start\":51,\"countdown\":720},{\"start\":71,\"countdown\":900},{\"start\":81,\"countdown\":1200},{\"start\":91,\"countdown\":1500},{\"start\":96,\"countdown\":1800}],\"fruitNum\":34,\"acceleratorsNum\":0},{\"id\":5,\"name\":\"玉米\",\"unlockPrice\":100,\"seedPrice\":50,\"profit\":90,\"second\":600,\"countDownSecond\":0,\"status\":3,\"seedNum\":0,\"cloudSecond\":10,\"acceleratorsSecond\":5,\"guideStatus\":0,\"growList\":[{\"start\":1,\"countdown\":600},{\"start\":31,\"countdown\":720},{\"start\":41,\"countdown\":900},{\"start\":46,\"countdown\":1200},{\"start\":51,\"countdown\":1500},{\"start\":71,\"countdown\":1800},{\"start\":81,\"countdown\":2700},{\"start\":91,\"countdown\":3600},{\"start\":96,\"countdown\":7200}],\"fruitNum\":38,\"acceleratorsNum\":0},{\"id\":6,\"name\":\"茄子\",\"unlockPrice\":200,\"seedPrice\":80,\"profit\":144,\"second\":720,\"countDownSecond\":0,\"status\":3,\"seedNum\":0,\"cloudSecond\":10,\"acceleratorsSecond\":5,\"guideStatus\":0,\"growList\":[{\"start\":1,\"countdown\":720},{\"start\":31,\"countdown\":900},{\"start\":41,\"countdown\":1200},{\"start\":46,\"countdown\":1500},{\"start\":51,\"countdown\":1800},{\"start\":71,\"countdown\":2400},{\"start\":81,\"countdown\":3600},{\"start\":91,\"countdown\":7200},{\"start\":96,\"countdown\":10800}],\"fruitNum\":32,\"acceleratorsNum\":0},{\"id\":7,\"name\":\"西瓜\",\"unlockPrice\":300,\"seedPrice\":100,\"profit\":180,\"second\":900,\"countDownSecond\":0,\"status\":3,\"seedNum\":0,\"cloudSecond\":10,\"acceleratorsSecond\":5,\"guideStatus\":0,\"growList\":[{\"start\":1,\"countdown\":900},{\"start\":31,\"countdown\":1200},{\"start\":41,\"countdown\":1500},{\"start\":46,\"countdown\":1800},{\"start\":51,\"countdown\":2400},{\"start\":71,\"countdown\":3600},{\"start\":81,\"countdown\":5400},{\"start\":91,\"countdown\":7200},{\"start\":96,\"countdown\":10800}],\"fruitNum\":20,\"acceleratorsNum\":0},{\"id\":8,\"name\":\"葡萄\",\"unlockPrice\":500,\"seedPrice\":200,\"profit\":360,\"second\":1200,\"countDownSecond\":0,\"status\":3,\"seedNum\":0,\"cloudSecond\":10,\"acceleratorsSecond\":5,\"guideStatus\":0,\"growList\":[{\"start\":1,\"countdown\":1200},{\"start\":31,\"countdown\":1500},{\"start\":41,\"countdown\":1800},{\"start\":46,\"countdown\":2400},{\"start\":51,\"countdown\":3600},{\"start\":71,\"countdown\":5400},{\"start\":81,\"countdown\":7200},{\"start\":91,\"countdown\":10800},{\"start\":96,\"countdown\":14400}],\"fruitNum\":18,\"acceleratorsNum\":0},{\"id\":9,\"name\":\"草莓\",\"unlockPrice\":1000,\"seedPrice\":300,\"profit\":540,\"second\":1500,\"countDownSecond\":0,\"status\":3,\"seedNum\":0,\"cloudSecond\":10,\"acceleratorsSecond\":5,\"guideStatus\":0,\"growList\":[{\"start\":1,\"countdown\":1500},{\"start\":31,\"countdown\":1800},{\"start\":41,\"countdown\":2400},{\"start\":46,\"countdown\":3600},{\"start\":51,\"countdown\":5400},{\"start\":71,\"countdown\":7200},{\"start\":81,\"countdown\":10800},{\"start\":91,\"countdown\":14400},{\"start\":96,\"countdown\":21600}],\"fruitNum\":16,\"acceleratorsNum\":0}]}";
        //LandListDataControl.GetIns().saveLandListJson(json);
        AndroidJavaClass jc = new AndroidJavaClass("com.unity3d.player.UnityPlayer");
        mainActivity = jc.GetStatic<AndroidJavaObject>("currentActivity");
    }

    public void onClickTest()
    {
        //if (LandListDataControl.GetIns().setLockLandData(5))
        //{
        //    Debug.Log("解锁成功");
        //    transform.BroadcastMessage("updateLand", SendMessageOptions.DontRequireReceiver);
        //}
        //else
        //{
        //    Debug.Log("解锁失败");
        //}
    }
    ////解锁地块
    //public void onClickUnLockPlant(int id)
    //{
    //    if (LandListDataControl.GetIns().setLockLandData(id))
    //    {
    //        Debug.Log("解锁成功");
    //        updateLandList();
    //    }
    //    else
    //    {
    //        Debug.Log("解锁失败");
    //    }
    //}
    //刷新 1 解锁地块 2 倒计时到了 3 收获 4 种植 5解锁倒计时（除虫） -2加速
    public void updateLandList(int action, int id, int time)
    {
        switch (action)
        {
            case 1:
                syncServiceData(id + 1, 1, time);
                break;
            case 2:
                break;
            case 3:
                syncServiceData(id + 1, 3, time);
                //GameObject.Find("GameManager").GetComponent<UnityToAndroid>().getCard(id);
                GameObject.Find("OrderView").GetComponent<OrderControl>().editHomeList(id);
                break;
            case 4:
                syncServiceData(id + 1, 2, time);
                //GameObject.Find("GameManager").GetComponent<UnityToAndroid>().getCard(id);
                break;
            case 5:
                //syncServiceData(id + 1, 4, time);
                lookAdCleanWorm(id + 1);
                break;
            case -2://点击加速的时候
                //GameObject.Find("GameManager").GetComponent<UnityToAndroid>().getCard(id);
                syncServiceAccelerateData(id + 1);
                break;
            case -3://买种子，看视频得种子
                //GameObject.Find("GameManager").GetComponent<UnityToAndroid>().getCard(id);
                break;
            default:
                break;
        }

    }
    ////刷新
    //private IEnumerator updateLandList2(int id, int type)
    //{
    //    yield return null;
    //    transform.BroadcastMessage("updateLand", SendMessageOptions.DontRequireReceiver);
    //}
    //解锁地块/种植种子/收获果实
    private void syncServiceData(int id, int type, int time)
    {
        try
        {
            Debug.Log("同步解锁地块/种植种子/收获果实ID" + id + "TYPE" + type);
            mainActivity.Call("getUnlockLandNew", id, type, time);
        }
        catch (Exception e)
        {
            Debug.Log("getUnlockLand ERROR" + e.Message);
        }
    }

    //使用加速剂同步
    private void syncServiceAccelerateData(int id)
    {
        try
        {
            Debug.Log("使用加速剂ID" + id);
            mainActivity.Call("getUseAcceleratorsSync", id);
        }
        catch (Exception e)
        {
            Debug.Log("getUseAcceleratorsSync ERROR" + e.Message);
        }
    }

    //看广告除虫
    private void lookAdCleanWorm(int id)
    {
        try
        {
            bool isShow = LandListDataControl.GetIns().getKillWormIsShowAd(id - 1);
            mainActivity.Call("lookAdVideoCleanWorm", id, isShow);
        }
        catch (Exception e)
        {
            Debug.Log("lookAdVideoCleanWorm ERROR" + e.Message);
        }
    }

    //看广告视频解锁地块
    public void lookAdVideoUnlockLand()
    {
        try
        {
            mainActivity.Call("lookAdVideoUnLockLand");
        }
        catch (Exception e)
        {
            Debug.Log("lookAdVideoUnlockLand ERROR" + e.Message);
            //lookAdVideoUnLockLandSuccess();
        }
    }
    //看广告视频解锁成功
    public void lookAdVideoUnLockLandSuccess()
    {
        StartCoroutine(sycnUI());
    }
    private IEnumerator sycnUI()
    {
        yield return null;
        transform.root.BroadcastMessage("setUnLockLand", SendMessageOptions.DontRequireReceiver);
    }

}
