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

public class GameListenerHelper
{
    private static string basePack = "com.ym.game.IUnitySendMessageCallback";

    public interface IAddCardListener
    {
        void onGetCardListResult(PuzzleEntity puzzle);
        void onAddCardResult(AddCardEntity entity);
        void onError(string message);
    }
    //title
    public interface IHomeListener
    {
        //金币数量 可提现数 打卡剩余次数 云朵加速剩余次数
        //void onCoinNumResult(int coinNum, int moneyNum,int subClockNum,int cloudNum);
        //主页title
        void onHomeResult(HomeInfoEntity home);
        //云朵加速倒计时
        //void onCloudSecondResult(int cloudSecondNum);
        //接口报错
        void onError(string message);
    }
    //farm
    public interface IFarmListener
    {
        //农场初始化
        void onInitFarmResult(LandEntity[] landList);
        //看云朵视频加速
        void onSpeedUpSuccess(StatusEntity status);
        //解锁地块/种植种子/收获果实
        void onUnLockLandSuccess(StatusEntity status);
        //加速剂使用
        void onUseAcceleratorSuccess(StatusEntity status);
        //金币购买种子成功
        void onBuySeedResult(StatusEntity status);
        //看视频获取种子
        void onVideoSeedResult(StatusEntity status);
        //看视频获取加速剂
        void onVideoAcceleratorsResult(StatusEntity status);
        //接口报错
        void onError(string message);
        //判断新人
        void onGuideSuccess(GuideEntity guide);
    }
    //仓库
    public interface IHomeWareListener
    {
        //农场初始化
        void onHomeWareListResult(LandEntity[] landList);
        //接口报错
        void onError(string message);
    }
    //订单
    public interface IOrderListener
    {
        //订单
        void onGetOrderListResult(HomeListEntity order);
        //订单领取
        void onOrderReceiveResult(StatusEntity order);
        //接口报错
        void onError(string message);
    }
    //红包
    public interface IRedPackListener
    {
        //红包列表
        void onRedPackListResult(LandEntity[] order);
        //接口报错
        void onError(string message);
    }
    //dialog
    public interface IDialogListener
    {
        //云朵加速判断
        void onGetCheckCloudListResult(GuideEntity bean);
        //接口报错
        void onError(string message);
    }

    public class ICallBackListener : AndroidJavaProxy
    {
        private readonly IHomeListener titleListener;
        private readonly IFarmListener farmListener;
        private readonly IHomeWareListener wareListener;
        private readonly IOrderListener orderListener;
        private readonly IRedPackListener redpackListener;
        private readonly IDialogListener dialogListener;
        private readonly IAddCardListener addCardListener;
        public ICallBackListener(IAddCardListener listener) : base(basePack)
        {
            this.addCardListener = listener;
        }
        public ICallBackListener(IHomeListener listener) : base(basePack)
        {
            this.titleListener = listener;
        }

        public ICallBackListener(IFarmListener listener) : base(basePack)
        {
            this.farmListener = listener;
        }

        public ICallBackListener(IHomeWareListener listener) : base(basePack)
        {
            this.wareListener = listener;
        }

        public ICallBackListener(IOrderListener listener) : base(basePack)
        {
            this.orderListener = listener;
        }

        public ICallBackListener(IRedPackListener listener) : base(basePack)
        {
            this.redpackListener = listener;
        }

        public ICallBackListener(IDialogListener listener) : base(basePack)
        {
            this.dialogListener = listener;
        }
        //主页
        void onHomeSuccess(bool isSuccess, string result)
        {

            if (isSuccess)
            {
                HomeInfoEntity home = JsonUtility.FromJson<HomeInfoEntity>(result);
                ////title 金币数
                //titleListener.onCoinNumResult(home.coins, home.cents, home.subClockNum,home.cloudNum);
                ////title 钱数
                //titleListener.onCloudSecondResult(home.cloudSecond);
                //
                titleListener.onHomeResult(home);
            }
            else
            {
                titleListener.onError(result);
            }
            GameClickControl.GetGameClick().gameClick = true;
        }

        void onGetCardList(bool isSuccess, string result)
        {
            if (isSuccess)
            {
                PuzzleEntity puzzle = JsonUtility.FromJson<PuzzleEntity>(result);
                addCardListener.onGetCardListResult(puzzle);
            }
            else
            {
                addCardListener.onError(result);
            }
        }
        //获得卡片
        void onAddCardSuccess(bool isSuccess, string result)
        {
            if (isSuccess)
            {
                AddCardEntity addCard = JsonUtility.FromJson<AddCardEntity>(result);
                addCardListener.onAddCardResult(addCard);
            }
            else
            {
                addCardListener.onError(result);
            }
        }
        //新人引导
        void onGetGuideSuccess(bool isSuccess, string result)
        {
            if (isSuccess)
            {
                GuideEntity guide = JsonUtility.FromJson<GuideEntity>(result);
                farmListener.onGuideSuccess(guide);
            }
            else
            {
                farmListener.onError(result);
            }
            GameClickControl.GetGameClick().gameClick = true;
        }
        //云朵加速成功
        void onCloudSpeedSuccess(bool isSuccess, string result)
        {
            Debug.Log("onCloudSpeedSuccess1" + isSuccess + "result" + result);
            if (isSuccess)
            {
                Debug.Log("onCloudSpeedSuccess2");
                StatusEntity status = JsonUtility.FromJson<StatusEntity>(result);
                Debug.Log("onCloudSpeedSuccess3");
                farmListener.onSpeedUpSuccess(status);
            }
            else
            {
                Debug.Log("onCloudSpeedSuccess5");
                farmListener.onError(result);
            }
            GameClickControl.GetGameClick().gameClick = true;
        }
        //土地列表
        void onFarmLandListSuccess(bool isSuccess, string result)
        {


            if (isSuccess)
            {
                JsonReader fr = new JsonReader(result);
                LandListEntity bean = JsonMapper.ToObject<LandListEntity>(fr);
                farmListener.onInitFarmResult(bean.landList);
            }
            else
            {
                farmListener.onError(result);
            }
            GameClickControl.GetGameClick().gameClick = true;
        }
        //解锁地块
        void onLandUnLockSuccess(bool isSuccess, string result)
        {
            if (isSuccess)
            {
                JsonReader fr = new JsonReader(result);
                StatusEntity bean = JsonMapper.ToObject<StatusEntity>(fr);
                farmListener.onUnLockLandSuccess(bean);
            }
            else
            {
                farmListener.onError(result);
            }
            GameClickControl.GetGameClick().gameClick = true;
        }
        //加速剂
        void onUseAcceleratorsSuccess(bool isSuccess, string result)
        {
            if (isSuccess)
            {
                JsonReader fr = new JsonReader(result);
                StatusEntity bean = JsonMapper.ToObject<StatusEntity>(fr);
                farmListener.onUseAcceleratorSuccess(bean);
            }
            else
            {
                farmListener.onError(result);
            }
            GameClickControl.GetGameClick().gameClick = true;
        }
        //金币购买种子
        void onBuySeedSuccess(bool isSuccess, string result)
        {
            if (isSuccess)
            {
                JsonReader fr = new JsonReader(result);
                StatusEntity bean = JsonMapper.ToObject<StatusEntity>(fr);
                farmListener.onBuySeedResult(bean);
            }
            else
            {
                farmListener.onError(result);
            }
            GameClickControl.GetGameClick().gameClick = true;
        }
        //仓库
        void onHomeWareListSuccess(bool isSuccess, string result)
        {

            if (isSuccess)
            {
                JsonReader fr = new JsonReader(result);
                LandListEntity bean = JsonMapper.ToObject<LandListEntity>(fr);
                wareListener.onHomeWareListResult(bean.landList);
            }
            else
            {
                wareListener.onError(result);
            }
        }
        //订单
        void onOrderListSuccess(bool isSuccess, string result)
        {
            if (isSuccess)
            {
                JsonReader fr = new JsonReader(result);
                HomeListEntity bean = JsonMapper.ToObject<HomeListEntity>(fr);
                orderListener.onGetOrderListResult(bean);
            }
            else
            {
                orderListener.onError(result);
            }
            GameClickControl.GetGameClick().gameClick = true;

        }
        //订单领取
        void onOrderReceiveSuccess(bool isSuccess, string result)
        {
            if (isSuccess)
            {
                JsonReader fr = new JsonReader(result);
                StatusEntity bean = JsonMapper.ToObject<StatusEntity>(fr);
                orderListener.onOrderReceiveResult(bean);
            }
            else
            {
                orderListener.onError(result);
            }
            GameClickControl.GetGameClick().gameClick = true;
        }
        //看视频获得种子
        void onIncrVideoSeedSuccess(bool isSuccess, string result)
        {
            if (isSuccess)
            {
                JsonReader fr = new JsonReader(result);
                StatusEntity bean = JsonMapper.ToObject<StatusEntity>(fr);
                farmListener.onVideoSeedResult(bean);
            }
            else
            {
                farmListener.onError(result);
            }
            GameClickControl.GetGameClick().gameClick = true;
        }
        //看视频获得加速剂
        void onIncrVideoAcceleratorsSuccess(bool isSuccess, string result)
        {
            if (isSuccess)
            {
                JsonReader fr = new JsonReader(result);
                StatusEntity bean = JsonMapper.ToObject<StatusEntity>(fr);
                farmListener.onVideoAcceleratorsResult(bean);

            }
            else
            {
                farmListener.onError(result);
            }
            GameClickControl.GetGameClick().gameClick = true;
        }

        //红包列表
        void onRedPackListSuccess(bool isSuccess, string result)
        {
            Debug.Log("onRedPackListSuccess1" + isSuccess + "result" + result);
            if (isSuccess)
            {
                Debug.Log("onRedPackListSuccess2");
                JsonReader fr = new JsonReader(result);
                Debug.Log("onRedPackListSuccess3");
                LandListEntity bean = JsonMapper.ToObject<LandListEntity>(fr);
                Debug.Log("onRedPackListSuccess4");
                redpackListener.onRedPackListResult(bean.redPackList);
                Debug.Log("onRedPackListSuccess5");
                //mainThreadSynContext.Send(new SendOrPostCallback((ss) =>
                //{
                //    Debug.Log("onRedPackListSuccess5");
                //    redpackListener.onRedPackListResult(bean.redPackList);

                //}), null);

            }
            else
            {
                redpackListener.onError(result);
            }

        }

        //检查云朵加速
        void onGetCheckCloud(bool isSuccess, string result)
        {
            if (isSuccess)
            {
                GuideEntity bean = JsonUtility.FromJson<GuideEntity>(result);
                dialogListener.onGetCheckCloudListResult(bean);
            }
            else
            {
                dialogListener.onError(result);
            }
        }


    }
}
