// Learn TypeScript:
//  - https://docs.cocos.com/creator/2.4/manual/en/scripting/typescript.html
// Learn Attribute:
//  - https://docs.cocos.com/creator/2.4/manual/en/scripting/reference/attributes.html
// Learn life-cycle callbacks:
//  - https://docs.cocos.com/creator/2.4/manual/en/scripting/life-cycle-callbacks.html

import { ryw_Event } from "../../../FrameWork/Event/EventEnum";
import EventMgr from "../../../FrameWork/Event/EventMgr";
import GameReport from "../../../FrameWork/Report/ZyZyReport";
import User from "../../../FrameWork/User/User";
import AppPlatform from "../../../FrameWork/Util/AppPlatform";
import Common from "../../../FrameWork/Util/Common";
import Common5 from "../../../Platform/th/Common5";
import TaskManager from "../../JuQingChat/TaskManager";
import Game from "./Game";

const { ccclass, property } = cc._decorator;

@ccclass
export default class tipsPrefabScript extends cc.Component {


    @property(cc.Node)
    answerBtnNode: cc.Node = null;

    @property(cc.Node)
    answerNode: cc.Node = null;
    @property(cc.Node)
    guizetishiNode: cc.Node = null;

    @property(cc.Node)
    tipNode: cc.Node = null;

    @property(cc.Label)
    tipLabe: cc.Label = null;
    @property(cc.Label)
    answerLabel: cc.Label = null;
    @property(cc.Label)
    guizetishiLabel: cc.Label = null;
    @property(cc.Label)
    newTipSpeLabel: cc.Label = null;
    @property(cc.Label)
    newTipNormalLabel: cc.Label = null;
    @property(cc.Node)
    newTipNode: cc.Node = null;
    @property(cc.Node)
    newTipBtnNode: cc.Node = null;
    @property(cc.Node)
    newTipContentNode: cc.Node = null;
    @property(cc.Node)
    newTipLabel: cc.Node = null;
    @property(cc.Node)
    imganswerNode: cc.Node = null;
    @property(cc.Sprite)
    answerImg: cc.Sprite = null;


    // LIFE-CYCLE CALLBACKS:

    // onLoad () {}
    curNode = null
    curData = null
    start() {
        this.initText();
        this.refBtnStat();
        EventMgr.onEvent_custom(ryw_Event.finish_game, () => {
            this.node.removeFromParent();
            this.node.destroy();
        }, this)
    }
    refBtnStat() {
        let answer = User.getUnlockWordgameAnswer(Common5.selectGameNum);
        let answerIndex = answer[Common.subLevel];
        if (answerIndex == 1) {
            this.answerBtnNode.active = true;
            this.answerNode.active = false;
            this.guizetishiNode.active = false;
            this.imganswerNode.active = false;
            this.newTipNode.active = false;
            this.tipNode.active = true;
        }
        else if (answerIndex == 2) {
            this.answerBtnNode.active = false;
            if (Common.Type == 0) {
                this.imganswerNode.active = false;
                this.answerNode.active = true;
                this.guizetishiNode.active = false;
                this.newTipNode.active = false;
            }
            else if (Common.Type == 1) {
                this.answerNode.active = false;
                this.guizetishiNode.active = true;
                this.imganswerNode.active = false;
                this.newTipNode.active = false;
            }
            else if (Common.Type == 2) {
                this.imganswerNode.active = true;
                this.answerNode.active = false;
                this.guizetishiNode.active = false;
                this.newTipNode.active = false;
            }
            else if (Common.Type == 3) {
                this.imganswerNode.active = false;
                this.answerNode.active = false;
                this.guizetishiNode.active = false;
                this.newTipNode.active = true;
            }
            this.tipNode.active = false;
        }
    }
    answerAd() {
        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-总裁训练营答案`)
                    }

                    User.unlockWordgameAnswer(Common5.selectGameNum, Common.subLevel, 2);
                    this.refBtnStat();
                }
                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)
    }
    initText() {
        if (Common.GameSubTipConfigs) {
            let tip = Common.GameSubTipConfigs[Common.subLevel];
            if (tip) {
                this.tipLabe.string = tip;
            }
            let answer = User.getUnlockWordgameAnswer(Common5.selectGameNum);
            let tipIndex = Common.GameSubTipConfigs.length - 1;
            if (answer) {
                // this.newTipNormalLabel.string = "";
                let allUnlock = true;
                this.newTipContentNode.removeAllChildren();
                for (let i = 0; i < Common.GameSubTipConfigs.length; i++) {
                    let numStr = (i + 1).toString();
                    let tmpStr = "";
                    let tmpNode = cc.instantiate(this.newTipLabel);
                    if (i + 1 < 10) {
                        numStr = "0" + numStr;
                    }
                    let answerIndex = answer[i];
                    if (answerIndex == 2) {
                        tmpStr = numStr + ":" + Common.GameSubTipConfigs[i];
                    }
                    else {
                        tmpStr = numStr + ":未获得";
                        allUnlock = false;
                    }
                    // if (i < Common.GameSubTipConfigs.length-1){
                    //     this.newTipNormalLabel.string += "\r\n";
                    // }
                    tmpNode.getComponent(cc.Label).string = tmpStr;
                    this.newTipContentNode.addChild(tmpNode);
                    tmpNode.active = true;
                    let isFinish = false;
                    for (let j = 0; j < Game.ins.finishSub.length; j++) {
                        if (i == Game.ins.finishSub[j]) {
                            isFinish = true;
                            break;
                        }
                    }
                    if (!isFinish) {
                        tipIndex = Math.min(tipIndex, i);
                        tmpNode.color = cc.color(127, 127, 127);
                    }
                }
                if (allUnlock) {
                    this.newTipBtnNode.active = false;
                }
                else {
                    this.newTipBtnNode.active = true;
                }

            }
            if (Common.SpeTip == "") {
                console.log(tipIndex)
                this.newTipSpeLabel.string = Common.GameSubTipConfigs[tipIndex];
            }
            else {
                this.newTipSpeLabel.string = Common.SpeTip;
            }
        }
        if (Common.GameSubAnswerConfigs) {

            let answer = Common.GameSubAnswerConfigs[Common.subLevel];
            if (answer) {
                this.answerLabel.string = answer;
            }
        }
        if (Common5.selectGameInfo.answerImg && Common5.selectGameInfo.answerImg != "") {
            Common5.getSpriteFrameFromBundle(Common5.selectGameInfo.subbundle, Common5.selectGameInfo.answerImg, this.answerImg);
        }

        if (Common5.selectGameInfo.answerImgbandle && Common5.selectGameInfo.answerImgbandle != "") {
            Common5.getSpriteFrameFromBundle(Common5.selectGameInfo.bundle, Common5.selectGameInfo.answerImgbandle, this.answerImg);
        }


    }
    newTipBtn() {
        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-总裁训练营提示`)
                    }

                    if (Common.Type == 3) {
                        let index = 0;
                        let answer = User.getUnlockWordgameAnswer(Common5.selectGameNum)
                        if (answer) {
                            for (let i = 0; i < Common.GameSubTipConfigs.length; i++) {
                                if (answer[i] != 2) {
                                    index = i;
                                    break;
                                }
                            }
                        }
                        User.unlockWordgameAnswer(Common5.selectGameNum, index, 2);
                        Common.subLevel = index;
                    }
                    this.initText();
                    return;
                }
                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)
    }
    btnClose() {
        this.node.removeFromParent();
        this.node.destroy();
        EventMgr.emitEvent_custom(ryw_Event.tooltipClose);
    }
    // update (dt) {}
}