import { ryw_Event } from "../../../FrameWork/Event/EventEnum";
import EventMgr from "../../../FrameWork/Event/EventMgr";
import GameMgr from "../../../FrameWork/Mgr/GameMgr";
import GameReport from "../../../FrameWork/Report/ZyZyReport";
import User from "../../../FrameWork/User/User";
import AppPlatform from "../../../FrameWork/Util/AppPlatform";

import Common5 from "../../../Platform/th/Common5";

import DayUnlock from "../../../FrameWork/Mgr/DayUnlock";
import TaskManager from "../../JuQingChat/TaskManager";


const { ccclass, property } = cc._decorator;

@ccclass
export default class LevelUnlock extends cc.Component {

    @property(cc.Node)
    private unlockall_button: cc.Node = null;
    @property(cc.RichText)
    private showtext: cc.RichText = null;

    protected onLoad(): void {
        if (AppPlatform.is_WECHAT_GAME_custom()) {
            this.unlockall_button.active = false;
            this.showtext.string = "观看视频解锁关卡"
        }
    }

    level: number = 0;
    type: number = 0;
    backBtn: cc.Node = null;
    videoBtnOne: cc.Node = null;
    videoBTN: cc.Node = null;
    isLoading = false;

    private needVideoNum = 3;
    protected start(): void {
        this.needVideoNum = 3//Common5.videoDayUnlockNum;
        this.backBtn = this.node.getChildByName("show").getChildByName("btnClose");
        this.videoBtnOne = this.node.getChildByName("show").getChildByName("btnNode").getChildByName("unlockOne");
        this.videoBTN = this.node.getChildByName("show").getChildByName("btnNode").getChildByName("unlockAll");
        Common5.btnRegister_custom(this.backBtn, () => {
            this.btnCloseFunc();
        })
        Common5.btnRegister_custom(this.videoBtnOne, () => {
            this.btnVideo();
        })
        Common5.btnRegister_custom(this.videoBTN, () => {
            this.btnVideoUnlockAll();
        })
        if (this.level == -1 && this.type == -1) {
            this.videoBtnOne.active = false;
        }
        else {
            let bundle = cc.assetManager.loadBundle(Common5.gameConfig.zmGameConfig[this.level].bundle, (err, bundle) => {
                bundle.load(Common5.gameConfig.zmGameConfig[this.level].url, cc.Prefab, () => {
                    console.log(this.level + "  preloadSuccess!")
                })
            })
            if (Common5.gameConfig.zmGameConfig[this.level].subbundle && Common5.gameConfig.zmGameConfig[this.level].subbundle != "") {
                let subbundle = cc.assetManager.loadBundle(Common5.gameConfig.zmGameConfig[this.level].subbundle, (err, subbundle) => {
                    subbundle.load(Common5.gameConfig.zmGameConfig[this.level].suburl, cc.Prefab, () => {
                    })
                })
            }
        }
        this.updateVideoTimes();
    }
    updateVideoTimes() {
        this.node.getChildByName("show").getChildByName("btnNode").getChildByName("unlockAll").getChildByName("label").getComponent(cc.Label).string = DayUnlock.getDayUnlockVideoAllNum().toString() + "/" + 3;
    }
    setLevel(levelIn, typeIn) {
        this.level = levelIn;
        this.type = typeIn;
    }

    btnVideo() {
        let tab = {
            onClose: (finish) => {
                if (finish) {


                    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-总裁训练营解锁本关`)
                    }


                    Common5.wordGameType = this.type;
                    this.isLoading = true;
                    User.setLevelAryy_custom(this.level);
                    // User.saveDataToServer()
                    EventMgr.emitEvent_custom(ryw_Event.unLockLevel, this.level);
                }
                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("广告未观看完");
                }
                this.node.removeFromParent();
            }, onFailed: () => {
                this.node.removeFromParent();
            }
        }
        AppPlatform.playVideo_custom(tab);
    }
    btnVideoUnlockAll() {
        let tab = {
            onClose: (finish) => {
                if (finish) {

                    if (Common5.selectGameInfo) {
                        Common5.ReportDY("inLevel", Common5.selectGameInfo.title2 + "-AD-解锁全部");
                    }


                    if (AppPlatform.is_TT_GAME_custom()) {
                        window["tt"].reportAnalytics('watchAd', {
                            type: '看视频无限体力',
                        });
                    }
                    let num = DayUnlock.getDayUnlockVideoAllNum();
                    num = num + 1;
                    if (DayUnlock.getDayUnlockVideoAllTime() == 0) {
                        if (num == 1) {
                            DayUnlock.setDayUnlocVideoAllNum(num);
                            GameMgr.getInstance_custom().saveGameData_custom();
                        } else if (num == 2) {
                            DayUnlock.setDayUnlocVideoAllNum(num);
                            GameMgr.getInstance_custom().saveGameData_custom();
                        }
                    }

                    if (num >= this.needVideoNum) {
                        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-总裁训练营解锁全部`)
                        }
                        Common5.isNoPower = false;
                        num = this.needVideoNum;
                        DayUnlock.setDayUnlocVideoAllNum(num);
                        this.updateVideoTimes();
                        DayUnlock.setDayUnlocVideoAllNum(0);
                        DayUnlock.addNewDayUnlocksicalAll();
                        GameMgr.getInstance_custom().saveGameData_custom();
                        this.scheduleOnce(() => {
                            this.btnCloseFunc();
                        }, 0.3)
                        EventMgr.emitEvent_custom(ryw_Event.unLockAllLevelTmp);
                    } else {
                        let bundle = Common5.selectGameInfo.bundle
                        if (bundle.includes('Level')) {
                            GameReport.ADReport(Common5.selectGameInfo.title2 + '解锁全部', 0, "失物招领")
                        } else {
                            GameReport.ADReport(Common5.selectGameInfo.title2 + '解锁全部', 0, "总裁训练营")
                        }
                        DayUnlock.setDayUnlocVideoAllNum(num);
                        this.updateVideoTimes();
                        GameMgr.getInstance_custom().saveGameData_custom();
                    }

                    // User.saveDataToServer()

                }
            }, onFailed: () => {

            }
        }
        AppPlatform.playVideo_custom(tab)
    }
    btnCloseFunc() {
        this.node.removeFromParent();
    }
}