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 JuQingManager from "../JuQingChat/JuQingManager";
import TaskManager, { MainTaskIdEnum } from "../JuQingChat/TaskManager";
import NewGuideScript from "../NewGuide/NewGuideScript";
import PrefabManage, { GameType } from "../PrefabManager/PrefabManage";
import ChaDianListScript2 from "../WenZiRes/WenZiCommon/ChaDianListScript2";
import logPrefabScript from "../WenZiRes/WenZiCommon/logPrefabScript2";
const { ccclass, property } = cc._decorator;

let ChadianConfig = {
    chadianIconList: [
        {
            bandleName: "GameStory4",
            name: "地上的棒棒糖",
            url: "texture/地上的棒棒糖",
            isCheck: false
        },
        {
            bandleName: "GameStory4",
            name: "几排脚印",
            url: "texture/几排脚印",
            isCheck: false
        },
        {
            bandleName: "GameStory4",
            name: "餐厅工作牌",
            url: "texture/餐厅工作牌",
            isCheck: false
        },
        {
            bandleName: "GameStory4",
            name: "桌上的纸条",
            url: "texture/桌上的纸条",
            isCheck: false
        }
    ],
    chadianLog: [
        {
            str: "女儿的棒棒糖都丢了?",
            qiPaoPos: -1,
            delay: 1.6,
            effectUrl: "GameStoryRes/sound/GameStory4/女儿的棒棒糖都丢了?"
        },
        {
            str: "看着不止一个人",
            qiPaoPos: -1,
            delay: 1.9,
            effectUrl: "GameStoryRes/sound/GameStory4/看着不止一个人"
        },
        {
            str: "难道是陈天霸这个畜生!",
            qiPaoPos: -1,
            delay: 1.8,
            effectUrl: "GameStoryRes/sound/GameStory4/难道是陈天霸这个畜生!"
        },
        {
            str: "老婆的纸条?",
            qiPaoPos: -1,
            delay: 2.1,
            effectUrl: "GameStoryRes/sound/GameStory4/老婆的纸条?"
        }
    ]
}

@ccclass
export default class GameStory4 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() {
        //Common5.playMusicCustom('GameStory4', 'sound/海浪');
        let chadianIconList = this.node.getChildByName('chadianIconList');
        let scr: ChaDianListScript2 = chadianIconList.getComponent(ChaDianListScript2);
        scr.setChaDianData(ChadianConfig.chadianIconList);
        chadianIconList.active = true;
        this.chadianStep = 0;
    }

    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)
        }
    }

    gameChaDianCallback() {
        this.chadianStep++;
        if (this.chadianStep >= 4) {
            this.scheduleOnce(() => {
                TaskManager.finishCurMainTask(MainTaskIdEnum.MainTask_401);
                TaskManager.setCurUnLockMainTaskInfo(MainTaskIdEnum.MainTask_402);
                JuQingManager.unLockNewJuQing('WX_401');
                EventMgr.emitEvent_custom(ryw_Event.RefreshJuQingDuiHua);
                this.node.removeFromParent();
                // Common5.releaseBundleAssets('GameStory4')
            }, 2);
        }
    }

    normalTouchCallback(targetNode) {
        switch (targetNode.name) {
            case "bangbangtang":
                EventMgr.emitEvent_custom(ryw_Event.touchChaDianCheck, { touchIndex: 0 });
                this.showQiPao(ChadianConfig.chadianLog[0], this.gameChaDianCallback.bind(this));
                break;
            case "jiaoyin":
                EventMgr.emitEvent_custom(ryw_Event.touchChaDianCheck, { touchIndex: 1 });
                this.showQiPao(ChadianConfig.chadianLog[1], this.gameChaDianCallback.bind(this));
                break;
            case "gongpai":
                EventMgr.emitEvent_custom(ryw_Event.touchChaDianCheck, { touchIndex: 2 });
                this.showQiPao(ChadianConfig.chadianLog[2], this.gameChaDianCallback.bind(this));
                break;
            case "zhitiao":
                EventMgr.emitEvent_custom(ryw_Event.touchChaDianCheck, { touchIndex: 3 });
                this.showQiPao(ChadianConfig.chadianLog[3], this.gameChaDianCallback.bind(this));
                break;
            default:
                break;
        }
    }

    //展示气泡
    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);
            if (curLog.effectUrl && curLog.effectUrl.length > 0) {
                Common5.playRemoteAudioEffect(curLog.effectUrl);
            }
        }

    }

    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)
    }
}