import { ryw_Event } from "../../../FrameWork/Event/EventEnum";
import EventMgr from "../../../FrameWork/Event/EventMgr";
import GameReport, { ENTERTYPE, OUTTYPE } from "../../../FrameWork/Report/ZyZyReport";
import User from "../../../FrameWork/User/User";

import AppPlatform from "../../../FrameWork/Util/AppPlatform";

import Common5 from "../../../Platform/th/Common5";
import TTAPI from "../../../Platform/tt/TTAPI";
import WXAPI from "../../../Platform/weixin/WXAPI";
import TaskManager from "../../JuQingChat/TaskManager";
import UserManager from "../../Manager/UserManager";
import Game from "./Game";

const { ccclass, property } = cc._decorator;

@ccclass
export default class GameFailUI extends cc.Component {

    protected showNode: cc.Node = null;
    protected btnRestart: cc.Node = null;
    protected btnBack: cc.Node = null;
    protected btnShare: cc.Node = null;
    protected fireworksNode: cc.Node = null;
    protected fuhuoBtn: cc.Node = null;

    @property(cc.RichText)
    timeLabel: cc.RichText = null;
    fuhuoCall = null

    protected onLoad(): void {
        this.fuhuoCall = null
    }

    protected start(): void {
        Common5.showInterstitialAd()
        TTAPI.stopRecord_custom();
        // Common5.stopMusic();

        if (!Common5.isNoMusic) {
            Common5.playEffect("fail");
        }
        Common5.stopMusic();
        // Common5.playMusic("bg");
        this.showNode = this.node.getChildByName("game");
        this.btnRestart = this.showNode.getChildByName("btnRestart");
        this.btnBack = this.showNode.getChildByName("btnBack");
        this.btnShare = this.showNode.getChildByName("btnShare");

        this.btnRestart.on("click", () => {
            //GameReport.EnterReport(ENTERTYPE.CHONGWAN,Common5.selectGameInfo.title2)        

            let bundle = Common5.selectGameInfo.bundle
            if (bundle.includes('Level')) {

            } else {
                Game.ins.onRestartCallFunc();
            }


            this.node.removeFromParent();

        }, this);

        this.btnBack.on("click", () => {
            GameReport.OUTReport(OUTTYPE.SHIBAI, Common5.selectGameInfo.title2)
            this.btnBackFunc2();
        }, this);

        this.btnShare.on("click", () => {
            console.log("share");

            WXAPI.share_custom((finish) => {
                if (finish) {
                    //this.btnBackFunc();
                } else {
                    //this.btnBackFunc();
                }
            }, '', '');


            WXAPI.share_custom((finish) => {
            }, '', '');

            TTAPI.shareRecord_custom(() => {
                //this.btnBackFunc();
                // cc.director.loadScene("MainScene");
            }, () => {
                Common5.showTips_custom("录屏分享失败,请稍后再试!");
                //this.btnBackFunc();
                // cc.director.loadScene("MainScene");
            })
        }, this);
        if (this.timeLabel != null) {
            this.timeLabel.string = "还差一点就成功了<br>看个视频立即加<color=#ff0000>" + Common5.selectGameInfo.timeInit + "</c>秒"
        }
    }

    btnBackFunc2() {

        let bundle = Common5.selectGameInfo.bundle
        if (bundle.includes('Level')) {
            //GameZC.ins.exitGame()
        } else {
            Game.ins.exitGame()
        }

        this.node.removeFromParent();
    }

    btnBackFunc() {
        let bundle = Common5.selectGameInfo.bundle
        if (bundle.includes('Level')) {
            //GameZC.ins.clickBack(null,true);
        } else {
            Game.ins.clickBack(null, true);
        }


        this.node.removeFromParent();
    }


    setFuhuoActive() {
        let showNode = this.node.getChildByName("game");
        let fuhuoBtn = showNode.getChildByName("复活");
        let btnRestart = showNode.getChildByName("btnRestart");
        let btnShare = showNode.getChildByName("btnShare");
        btnShare.active = false
        fuhuoBtn.active = true
        btnRestart.active = true
    }

    btnClickEventFuHuoFunc() {
        let tab = {
            onClose: (finish) => {
                if (finish) {
                    if (Common5.selectGameInfo) {


                        let bundle = Common5.selectGameInfo.bundle
                        if (bundle.includes('Level')) {
                            GameReport.ADReport(Common5.selectGameInfo.title2 + '复活', 1, "失物招领")
                            // Common5.ReportDY("inLevel", '失物招领-AD-复活')
                            User.setShowAdNum(User.getShowAdNum() + 1)
                            Common5.ReportDY("inLevel", `任务${TaskManager.getCurUnLockMainTaskId()}-AD-失物招领复活`)
                        } else {
                            GameReport.ADReport(Common5.selectGameInfo.title2 + '复活', 1, "总裁训练营")
                            // Common5.ReportDY("inLevel", '总裁训练营-AD-复活')
                            User.setShowAdNum(User.getShowAdNum() + 1)
                            Common5.ReportDY("inLevel", `任务${TaskManager.getCurUnLockMainTaskId()}-AD-总裁训练营复活`)

                        }


                    }
                    EventMgr.emitEvent_custom(ryw_Event.WordGameRevive);
                    this.node.removeFromParent();
                } else {
                    let bundle = Common5.selectGameInfo.bundle
                    if (bundle.includes('Level')) {
                        GameReport.ADReport(Common5.selectGameInfo.title2 + '复活', 0, "失物招领")
                    } else {
                        GameReport.ADReport(Common5.selectGameInfo.title2 + '复活', 0, "总裁训练营")
                    }
                    Common5.showTips_custom("广告未看完");
                }

            }, onFailed: () => {

            }
        }
        AppPlatform.playVideo_custom(tab);


    }

}