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: "GameStory5",
            name: "陈天霸",
            url: "texture/陈天霸",
            isCheck: false
        },
        {
            bandleName: "GameStory5",
            name: "牙膏",
            url: "texture/牙膏",
            isCheck: false
        },
        {
            bandleName: "GameStory5",
            name: "手机",
            url: "texture/手机",
            isCheck: false
        }
    ],
    chadianLog: [
        {
            str: "员工中毒他还笑得这么开心",
            qiPaoPos: -1,
            delay: 3,
            effectUrl: "GameStoryRes/sound/GameStory5/员工中毒他还笑得这么开心"
        },
        {
            str: "吐的泡沫,是牙膏沫?",
            qiPaoPos: -1,
            delay: 2.5,
            effectUrl: "GameStoryRes/sound/GameStory5/吐的泡沫,是牙膏沫?"
        }
    ]
}

@ccclass
export default class GameStory5 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('GameStory5', '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 >= 3) {
            this.node.getChildByName("chadianIconList").active = false;
            this.node.getChildByName("mainNode").getChildByName("鸡毛掸子box").active = true;
            this.node.getChildByName("mainNode").getChildByName("手指箭头").active = true;
        }
    }

    normalTouchCallback(targetNode) {
        switch (targetNode.name) {
            case "xiaorong":
                if (targetNode.getChildByName("手指箭头")) {
                    targetNode.getChildByName("手指箭头").active = false
                }
                EventMgr.emitEvent_custom(ryw_Event.touchChaDianCheck, { touchIndex: 0 })
                this.showQiPao(ChadianConfig.chadianLog[0], this.gameChaDianCallback.bind(this));
                break;
            case "yagao":
                if (targetNode.getChildByName("手指箭头")) {
                    targetNode.getChildByName("手指箭头").active = false
                }
                EventMgr.emitEvent_custom(ryw_Event.touchChaDianCheck, { touchIndex: 1 })
                this.showQiPao(ChadianConfig.chadianLog[1], this.gameChaDianCallback.bind(this));
                break;
            case "shouji":
                if (targetNode.getChildByName("手指箭头")) {
                    targetNode.getChildByName("手指箭头").active = false
                }
                EventMgr.emitEvent_custom(ryw_Event.touchChaDianCheck, { touchIndex: 2 });
                this.gameChaDianCallback();
                break;
            case "鸡毛掸子":
                this.node.getChildByName("mainNode").getChildByName("手指箭头").active = false;
                let danzi = this.node.getChildByName("mainNode").getChildByName("一段");
                let fuwuyuan1 = this.node.getChildByName("mainNode").getChildByName("服务员倒地笑");
                let fuwuyuan2 = this.node.getChildByName("mainNode").getChildByName("倒地吐");
                danzi.active = true;
                danzi.getComponent(sp.Skeleton).setAnimation(0, "一段", false);
                this.scheduleOnce(() => {
                    Common5.playEffectCustom('GameStory5', 'sound/闺蜜笑');
                }, 1)
                this.scheduleOnce(() => {
                    fuwuyuan1.active = true;
                    fuwuyuan2.active = false;
                    this.scheduleOnce(() => {
                        danzi.active = false;
                        TaskManager.finishCurMainTask(MainTaskIdEnum.MainTask_502);
                        TaskManager.setCurUnLockMainTaskInfo(MainTaskIdEnum.MainTask_503);
                        EventMgr.emitEvent_custom(ryw_Event.RefreshJuQingDuiHua);
                        this.node.removeFromParent();
                        // Common5.releaseBundleAssets('GameStory5')
                    }, 2);
                }, 1);
                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)
    }
}