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 GameAlllockView extends cc.Component {
    level: number = 0;
    type: number = 0;
    backBtn: cc.Node = null;
    videoBtnOne: cc.Node = null;
    videoBTN: cc.Node = null;
    isLoading = false;

    private needVideoNum = 2;
    protected start(): void {
        this.needVideoNum = Common5.videoWxtlPowerNum;

        console.log(this.needVideoNum, 'this.needVideoNum+++++++++==')
        this.backBtn = this.node.getChildByName("unlockback");
        this.videoBtnOne = this.node.getChildByName("content").getChildByName("videoBtnOne");
        this.videoBTN = this.node.getChildByName("content").getChildByName("videoBTN");
        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.preload(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, () => {
                        console.log(this.level + "  subbundle preloadSuccess!")
                    })
                })
            }
        }
        this.updateVideoTimes();
    }
    updateVideoTimes() {
        this.node.getChildByName("content").getChildByName("videoBTN").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) {
                    Common5.selectGameNum = this.level;
                    Common5.wordGameType = this.type;
                    Common5.selectGameInfo = Common5.gameConfig.zmGameConfig[this.level];
                    this.isLoading = true;
                    console.log(this.level, '解锁关卡id += this.level+++++++++==')

                    let bundle = Common5.selectGameInfo.bundle
                    if (bundle.includes('Level')) {
                        GameReport.ADReport(Common5.selectGameInfo.titleUrl + '解锁本关', 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-总裁训练营解锁本关`)

                    }
                    User.setlevelNum_Word_Array_custom(this.level);
                    // User.saveDataToServer()
                    EventMgr.emitEvent_custom(ryw_Event.unLockLevel);
                    this.node.removeFromParent();
                    return;
                }
                else {
                    let bundle = Common5.selectGameInfo.bundle
                    if (bundle.includes('Level')) {
                        GameReport.ADReport(Common5.selectGameInfo.titleUrl + '解锁本关', 0, "失物招领")
                    } else {
                        GameReport.ADReport(Common5.selectGameInfo.title2 + '解锁本关', 0, "总裁训练营")
                    }
                    Common5.showTips_custom("广告未观看完");
                }
            }, onFailed: () => {
            }
        }
        AppPlatform.playVideo_custom(tab);
    }
    btnVideoUnlockAll() {
        let tab = {
            onClose: (finish) => {
                if (finish) {
                    Common5.ReportDY("inLevel", "解锁全部-AD-解锁全部");
                    // if (AppPlatform.is_TT_GAME_custom()) {
                    //     window["tt"].reportAnalytics('watchAd', {
                    //         type: '看视频无限体力',
                    //     });
                    // }

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

                    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) {
                        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);
                        console.log("unLockAllLevelTmp222");
                    } else {
                        DayUnlock.setDayUnlocVideoAllNum(num);
                        this.updateVideoTimes();
                        GameMgr.getInstance_custom().saveGameData_custom();
                    }
                    EventMgr.emitEvent_custom(ryw_Event.unLockAllLevelUpdate);
                }
                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);
    }
    btnCloseFunc() {
        this.node.removeFromParent();
    }
}