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 ChaDianListScript2 from "../WenZiRes/WenZiCommon/ChaDianListScript2";
import logPrefabScript from "../WenZiRes/WenZiCommon/logPrefabScript2";
const { ccclass, property } = cc._decorator;

let ChadianConfig = {
    chadianIconList: [
        {
            bandleName: "GameStory14",
            name: "茬点1",
            url: "texture/茬点1",
            isCheck: false
        },
        {
            bandleName: "GameStory14",
            name: "茬点2",
            url: "texture/茬点2",
            isCheck: false
        },
        {
            bandleName: "GameStory14",
            name: "茬点3",
            url: "texture/茬点3",
            isCheck: false
        },
        {
            bandleName: "GameStory14",
            name: "茬点4",
            url: "texture/茬点4",
            isCheck: false
        }
    ],
    chadianLog: [
        {
            str: "老板,你要为我做主啊",
            qiPaoPos: 1,
            delay: 2.7,
            effectUrl: "GameStoryRes/sound/GameStory14/老板,你要为我做主啊"
        },
        {
            str: "老板,门是刘伟砸坏的",
            qiPaoPos: 1,
            delay: 3,
            effectUrl: "GameStoryRes/sound/GameStory14/老板,门是刘伟砸坏的"
        },
        {
            str: "刘伟的手机,摔坏了?",
            qiPaoPos: -1,
            delay: 2.1,
            effectUrl: "GameStoryRes/sound/GameStory14/刘伟的手机,摔坏了?"
        },
        {
            str: "她诬陷我!修好手机我再给你解释",
            qiPaoPos: 0,
            delay: 3.5,
            effectUrl: "GameStoryRes/sound/GameStory14/她诬陷我!修好手机我再给你解释"
        }
    ]
}

@ccclass
export default class GameStory14 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('GameStory14', '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("mainNode").getChildByName("chadian4").active = true;
        }
    }

    normalTouchCallback(targetNode) {
        let sec = this.node.getChildByName("mainNode").getChildByName("秘书");
        let faxiao = this.node.getChildByName("mainNode").getChildByName("发小");
        switch (targetNode.name) {
            case "chadian1":
                EventMgr.emitEvent_custom(ryw_Event.touchChaDianCheck, { touchIndex: 0 });
                sec.getComponent(sp.Skeleton).setAnimation(0, "说话", true);
                this.showQiPao(ChadianConfig.chadianLog[0], () => {
                    sec.getComponent(sp.Skeleton).setAnimation(0, "待机", true);
                    this.gameChaDianCallback();
                });
                break;
            case "chadian2":
                EventMgr.emitEvent_custom(ryw_Event.touchChaDianCheck, { touchIndex: 1 });
                sec.getComponent(sp.Skeleton).setAnimation(0, "说话", true);
                this.showQiPao(ChadianConfig.chadianLog[1], () => {
                    sec.getComponent(sp.Skeleton).setAnimation(0, "待机", true);
                    this.gameChaDianCallback();
                });
                break;
            case "chadian3":
                EventMgr.emitEvent_custom(ryw_Event.touchChaDianCheck, { touchIndex: 2 });
                this.showQiPao(ChadianConfig.chadianLog[2], this.gameChaDianCallback.bind(this));
                break;
            case "chadian4":
                EventMgr.emitEvent_custom(ryw_Event.touchChaDianCheck, { touchIndex: 3 });
                faxiao.getComponent(sp.Skeleton).setAnimation(0, "发小说话", true);
                this.showQiPao(ChadianConfig.chadianLog[3], () => {
                    faxiao.getComponent(sp.Skeleton).setAnimation(0, "发小", true);
                    this.node.getChildByName("chadianIconList").active = false;
                    this.node.getChildByName("mainNode").getChildByName("手机").active = false;
                    cc.tween(this.node.getChildByName("mainNode").getChildByName("发小"))
                        .to(1, { opacity: 0 }).delay(2).call(() => {
                            TaskManager.finishCurMainTask(MainTaskIdEnum.MainTask_1401);
                            TaskManager.setCurUnLockMainTaskInfo(MainTaskIdEnum.MainTask_1402);
                            EventMgr.emitEvent_custom(ryw_Event.RefreshJuQingDuiHua);
                            this.node.removeFromParent();
                            // Common5.releaseBundleAssets('GameStory14')
                        }).hide().start();
                });
                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)
                .set({ active: false })
                .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)
    }
}