import { ryw_Event } from "../../FrameWork/Event/EventEnum";
import EventMgr from "../../FrameWork/Event/EventMgr";
import Common5 from "../../Platform/th/Common5";
import GameBaseScript from "../GameRoomRes/GameBaseScript";
import TaskManager, { MainTaskIdEnum } from "../JuQingChat/TaskManager";
import NewGuideScript from "../NewGuide/NewGuideScript";
import PrefabManage, { GameType } from "../PrefabManager/PrefabManage";
import CccGame from "../WenZiRes/WenZiCommon/CccGame";
import ChaDianListScript2 from "../WenZiRes/WenZiCommon/ChaDianListScript2";
import logPrefabScript from "../WenZiRes/WenZiCommon/logPrefabScript2";
const { ccclass, property } = cc._decorator;

let ChadianConfig = {
    chadianIconList: [
        {
            bandleName: "GameStory22",
            name: "茬点1",
            url: "texture/茬点1",
            isCheck: false
        }
    ],
    chadianLog: [
        {
            str: "老板,你可要为我做主啊",
            qiPaoPos: 1,
            delay: 3,
            effectUrl: "sound/老板,你可要为我做主啊"
        }
    ]
}

@ccclass
export default class GameStory22 extends cc.Component {

    // LIFE-CYCLE CALLBACKS:
    chadianStep: number = 0
    maskGuideNode: cc.Node = null

    onLoad() {
        //GameBaseScript.preLoadRemoteAudio(ChadianConfig);
        EventMgr.onEvent_custom(ryw_Event.NormalTouchEndCheck, (data_) => {
            this.normalTouchCallback(data_.targetNode);
        }, this);
        EventMgr.onEvent_custom(ryw_Event.NormalTouchMoveCheck, (data_) => {
            this.normalTouchCallback(data_.targetNode);
        }, this);
    }

    start() {
        this.chadianStep = 0;
        this.registerItemCollideEvent();
    }

    // 注册道具碰撞事件
    registerItemCollideEvent() {
        let mainNode = this.node.getChildByName("mainNode");
        let propIconList = this.node.getChildByName("propIconList");
        let items = propIconList.getChildByName("content").children;

        for (let index = 0; index < items.length; index++) {
            let target = items[index].children[1];
            let checkNode = mainNode.getChildByName(target.name);
            let callFuncs = CccGame.onNodeTouchCheckEvent(target, checkNode, { touchIndex: index }, propIconList);
            callFuncs.setSuccessListener((data_) => {
                let targetNode = data_.targetNode;
                items[targetNode.touchIndex].active = false;
                mainNode.getChildByName(targetNode.name + "2").active = true;
                this.gameChaDianCallback();
                callFuncs.closeTouchEvent();
            });
        }
    }


    gameChaDianCallback() {
        this.chadianStep++;
        if (this.chadianStep >= 5) {
            this.scheduleOnce(() => {
                TaskManager.finishCurMainTask(MainTaskIdEnum.MainTask_2204);
                TaskManager.setCurUnLockMainTaskInfo(MainTaskIdEnum.MainTask_2205);
                EventMgr.emitEvent_custom(ryw_Event.RefreshJuQingDuiHua);
                this.node.removeFromParent();
                // Common5.releaseBundleAssets('GameStory22')
            }, 2);
        }
    }

    normalTouchCallback(targetNode) {
        switch (targetNode.name) {
            case "chadian1":
                EventMgr.emitEvent_custom(ryw_Event.touchChaDianCheck, { touchIndex: 0 });
                this.showQiPao(ChadianConfig.chadianLog[0], () => {
                    this.gameChaDianCallback();
                });
                break;
            case "chadian2":
            default:
                break;
        }
    }

    //引导节点
    guideView(nodeArray) {
        if (this.maskGuideNode == null) {
            PrefabManage.loadPrefabByType(GameType.GuideMskNode, null, (prefab) => {
                let guideNodeArray = nodeArray
                this.maskGuideNode = prefab
                let firstNode = guideNodeArray.shift()
                prefab.getComponent(NewGuideScript).setBindNode(firstNode, guideNodeArray)
            })
        } else {
            this.maskGuideNode.active = true
            let guideNodeArray = nodeArray
            let firstNode = guideNodeArray.shift()
            this.maskGuideNode.getComponent(NewGuideScript).setBindNode(firstNode, guideNodeArray)
        }
    }

    //展示气泡
    showQiPao(curLog, func?) {
        console.log("curLog==", curLog)
        if (!curLog) {
            console.log("xxxxxx")
            return;
        }
        let string_ = curLog.str
        let qiPaoPos_ = curLog.qiPaoPos

        if (qiPaoPos_ != -1) {
            if (curLog.effectUrl && curLog.effectUrl.length > 0) {
                Common5.playRemoteAudioEffect(curLog.effectUrl);
            }

            let node = this.node
            let qiPao = node.getChildByName("qiPao").getChildByName("qiPao_" + qiPaoPos_);
            qiPao.stopAllActions()
            this.showDialogStr(string_, qiPao.getChildByName("str"))
            // qiPao.getChildByName("str").getComponent(cc.Label).string = string_
            qiPao.active = true
            qiPao.scale = 0
            cc.tween(qiPao)
                .to(0.2, { scale: 1 })
                .delay(curLog.delay)
                .call(() => {
                    qiPao.active = false;
                    if (func) {
                        func();
                    }
                })
                .start();
        }
        else {
            let logPrefab = this.node.getChildByName('logPrefab');
            let script_: logPrefabScript = logPrefab.getComponent('logPrefabScript2');
            script_.setDailogShow(string_, curLog.delay);
            this.scheduleOnce(() => { func && func(); }, curLog.delay);
        }
    }

    showDialogStr(str, dialogStr, finishFunc?) {
        let curStr = ""
        let curIndex = 0
        let callFunc = () => {
            curStr += str[curIndex++]
            dialogStr.getComponent(cc.Label).string = curStr
            if (curIndex >= str.length) {
                finishFunc && finishFunc()
            }
        }
        this.schedule(callFunc, 0.07, str.length - 1)
    }
}