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

public class UnityToAndroid : MonoBehaviour, GameListenerHelper.IAddCardListener
{

    //Android
    private AndroidJavaObject mainActivity;
    public AnimationClip startFlyBoxAnimation, stopFlyBoxAnimation;
    private int sid;
    public Text tv_puzzle_num;
    private void Awake()
    {
        AndroidJavaClass jc = new AndroidJavaClass("com.unity3d.player.UnityPlayer");
        mainActivity = jc.GetStatic<AndroidJavaObject>("currentActivity");
    }
    private void Start()
    {
        Screen.sleepTimeout = SleepTimeout.NeverSleep;
        guideOverPlayPlyBox();
        getCardList();
    }
    private void Update()
    {

    }

    public void guideOverPlayPlyBox()
    {
        if (!LandListDataControl.GetIns().getIsShowGuide)
        {
            Invoke("playFly", 3f);
        }
        //if (PlayerPrefs.GetInt("isFirstPlant", 0) != 0)
        //{
        //    Invoke("playFly", 3f);
        //}
    }
    //头像跳转设置
    public void onclickHeadImg()
    {
        try
        {
            onEventUtil("click_setting", "设置入口点击");
            GameObject.Find("Music_Background").GetComponent<MusicPlayerControl>().playBtnAudio();
            mainActivity.Call("onclickHead");
        }
        catch (Exception e)
        {
            Debug.Log("onclickHeadImg ERROR" + e.Message);
        }
    }
    //跳转小满入口
    public void onClickGotoXiaoMan()
    {
        try
        {
            GameObject.Find("Music_Background").GetComponent<MusicPlayerControl>().playBtnAudio();
            mainActivity.Call("gotoXiaoManActivity");
        }
        catch (Exception e)
        {
            Debug.Log("gotoXiaoManActivity ERROR" + e.Message);
        }
    }

    //跳转提现页
    public void onClickWithDraw(int type)
    {
        try
        {
            GameObject.Find("Music_Background").GetComponent<MusicPlayerControl>().playBtnAudio();
            mainActivity.Call("gotoWithdrawPage", type);
        }
        catch (Exception e)
        {
            Debug.Log("onClickWithDraw ERROR" + e.Message);
        }

    }

    //跳转上榜赚钱
    public void onClickRanklist()
    {
        try
        {
            GameObject.Find("Music_Background").GetComponent<MusicPlayerControl>().playBtnAudio();
            mainActivity.Call("ranklistClick");
        }
        catch (Exception e)
        {
            Debug.Log("onClickRanklist ERROR" + e.Message);
        }

    }

    //跳转每日福利
    public void onClickEveryDayWelfare()
    {
        try
        {
            onEventUtil("click_welfare", "福利入口点击");
            GameObject.Find("Music_Background").GetComponent<MusicPlayerControl>().playBtnAudio();
            mainActivity.Call("oneveryDayWelfareClick");
        }
        catch (Exception e)
        {
            Debug.Log("onClickEveryDayWelfare ERROR" + e.Message);
        }

    }
    //跳转拼图红包
    public void onClickPuzzle()
    {
         onEventUtil("click_pintu", "拼图入口点击");
        try
        {
            GameObject.Find("Music_Background").GetComponent<MusicPlayerControl>().playBtnAudio();
            mainActivity.Call("onPuzzleClick");
        }
        catch (Exception e)
        {
            Debug.Log("onClickPuzzle ERROR" + e.Message);
        }

    }

    //跳转大富翁
    public void onClickMonopoly()
    {
        onEventUtil("clickMonopoly", "大富翁入口点击");
        try
        {
            GameObject.Find("Music_Background").GetComponent<MusicPlayerControl>().playBtnAudio();
            mainActivity.Call("onMonopolyClick");
        }
        catch (Exception e)
        {
            Debug.Log("onMonopolyClick ERROR" + e.Message);
        }

    }



    //跳转打卡提现
    public void onClickClockInMakeMoney()
    {
        try
        {
            onEventUtil("click_attendance", "打卡入口点击");
            GameObject.Find("Music_Background").GetComponent<MusicPlayerControl>().playBtnAudio();
            mainActivity.Call("clockInMakeMoney");
        }
        catch (Exception e)
        {
            Debug.Log("onClickClockInMakeMoney ERROR" + e.Message);
        }
    }
    //跳转兑换现金
    public void onClickCashExchange()
    {
        onEventUtil("click_duihuan_cash", "兑换现金入口点击");
        GameObject.Find("guideParent").GetComponent<UserGuide>().setStep4();
        try
        {
            GameObject.Find("Music_Background").GetComponent<MusicPlayerControl>().playBtnAudio();
            mainActivity.Call("cashExchange");
        }
        catch (Exception e)
        {
            Debug.Log("onClickCashExchange ERROR" + e.Message);
        }
    }

    //跳转订单弹窗
    public void onClickShowOrderList()
    {
        try
        {
            GameObject.Find("Music_Background").GetComponent<MusicPlayerControl>().playBtnAudio();
            mainActivity.Call("jumpOrderDialog");
        }
        catch (Exception e)
        {
            Debug.Log("onClickClockInMakeMoney ERROR" + e.Message);
        }
    }


    //跳转幸运转盘
    public void onClickLuckyTurntable()
    {
        onEventUtil("click_lucky_turntable", "转盘入口点击");
        try
        {
            GameObject.Find("Music_Background").GetComponent<MusicPlayerControl>().playBtnAudio();
            mainActivity.Call("luckyTurntable");
        }
        catch (Exception e)
        {
            Debug.Log("onLuckyTurntable ERROR" + e.Message);
        }
    }
    //领加速剂弹窗
    public void onClickGetSpeedWater()
    {
        onEventUtil("click_jiasu_task", "领加速剂任务点击");
        try
        {
            GameObject.Find("Music_Background").GetComponent<MusicPlayerControl>().playBtnAudio();
            mainActivity.Call("getSpeedWater");
        }
        catch (Exception e)
        {
            Debug.Log("onClickGetSpeedWater ERROR" + e.Message);
        }
    }
    //领金币
    public void onClickGetCoin()
    {
        onEventUtil("click_renwu", "金币任务入口点击");
        try
        {
            GameObject.Find("guideParent").GetComponent<UserGuide>().setStep7(true);
            GameObject.Find("Music_Background").GetComponent<MusicPlayerControl>().playBtnAudio();
            mainActivity.Call("getCoin");
        }
        catch (Exception e)
        {
            Debug.Log("onClickGetCoin ERROR" + e.Message);
        }
    }
    //领加速剂任务
    //public void onClickGetAccelerateTask()
    //{
    //    onEventUtil("click_jiasu_task", "领加速剂任务点击");
    //    try
    //    {
    //        GameObject.Find("Music_Background").GetComponent<MusicPlayerControl>().playBtnAudio();
    //        mainActivity.Call("showWareHouseDialog");
    //    }
    //    catch (Exception e)
    //    {
    //        Debug.Log("onClickGetAccelerateTask ERROR" + e.Message);
    //    }
    //}

    //打开飞的宝箱
    public void openFlyBox()
    {
        onEventUtil("click_flyBox", "飞行宝箱入口点击");
        GameObject.Find("Music_Background").GetComponent<MusicPlayerControl>().playBtnAudio();
        stopflyBox();

        GameObject.Find("dialog").GetComponent<DialogControl>().showGetBox();

    }

    //获得宝箱点击观看视频
    public void onclickAdFlyBox()
    {
        GameObject.Find("Music_Background").GetComponent<MusicPlayerControl>().playBtnAudio();
        int num = GameObject.Find("TitleView").GetComponent<TitleControl>().getBoxNum;
        if (num > 0)
        {
            GameObject.Find("dialog").GetComponent<DialogControl>().closeGetBox();
            try
            {
                onEventUtil("click_flyBox_video_btn", "飞行宝箱视频");
                mainActivity.Call("adFlyBox");
            }
            catch (Exception e)
            {
                Debug.Log("onFlyBox ERROR" + e.Message);
            }
        }
        else
        {
            "视频次数已用完，明天再来吧~".showAsToast();
        }

    }
    public void onClickAdRedPackPlan()
    {
        GameObject.Find("Music_Background").GetComponent<MusicPlayerControl>().playBtnAudio();
        try
        {
            onEventUtil("click_chaihongbao", "点击拆红包");
            mainActivity.Call("adRedPackPlan");
        }
        catch (Exception e)
        {
            Debug.Log("onClickAdRedPackPlan ERROR" + e.Message);
        }
    }




    private Coroutine flyPlay;
    private void playFly()
    {
        flyPlay = StartCoroutine(flyBox());
    }

    public IEnumerator flyBox()
    {
        Animator a = GameObject.Find("fly_box").GetComponent<Animator>();
        if (a != null)
        {
            a.Play(startFlyBoxAnimation.name);
        }
        yield return new WaitForSeconds(startFlyBoxAnimation.length);
        stopflyBox();


    }
    public void stopflyBox()
    {
        if (flyPlay != null)
        {
            StopCoroutine(flyPlay);
        }
        Animator a = GameObject.Find("fly_box").GetComponent<Animator>();
        if (a != null)
        {
            // a.speed = 0f;
            a.Play(stopFlyBoxAnimation.name);
        }
        Invoke("playFly", 3f);
    }

    public void onEventUtil(string key, string value)
    {
        try
        {
            mainActivity.Call("GameEvent", key, value);
        }
        catch (Exception e)
        {
            //Debug.Log("onEventUtil===============================================================================================key" + key+"value"+ value);
            Debug.Log("onEventUtil ERROR" + e.Message);
        }
    }
    //获得碎片
    public void getCard(int id)
    {
        sid = id;
        try
        {

            if (mainActivity != null)
            {
                mainActivity.Call("addCard", new GameListenerHelper.ICallBackListener(this));
            }

        }
        catch (Exception e)
        {
            Debug.Log("addCard" + e.Message);

        }
    }
    //查询首页碎片角标
    public void getCardList()
    {
        try
        {

            if (mainActivity != null)
            {
                mainActivity.Call("getCardList", new GameListenerHelper.ICallBackListener(this));
            }

        }
        catch (Exception e)
        {
            Debug.Log("getCardList" + e.Message);

        }
    }
    //获取碎片结果
    public void onAddCardResult(AddCardEntity entity)
    {
        Debug.Log("碎片结果" + entity.cardIndex);
        if (entity.cardIndex > 0)
        {
            if (!LandListDataControl.GetIns().getIsShowGuide)
            {
                GameObject.Find("Form_land").GetComponent<FarmControl>().getPuzzleCode(sid);
            }
   
        }
           
        tv_puzzle_num.text = entity.totalCards.ToString();

    }
    //查询首页碎片数量结果
    public void onGetCardListResult(PuzzleEntity puzzle)
    {
        tv_puzzle_num.text = puzzle.totalCards.ToString();
    }

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

    public void duihuanclick(int id,string cashstr)
    {
        onEventUtil("click_keduihuan_" + id, id + "满了可兑换现金弹窗点击");
        try
        {

            if (mainActivity != null)
            {
                mainActivity.Call("duihuan", id,cashstr);
            }

        }
        catch (Exception e)
        {
            Debug.Log("duihuan" + e.Message);

        }
    }
}
