import User from "../../FrameWork/User/User";
import Common5 from "../../Platform/th/Common5";
import GameBaseScript from "../GameRoomRes/GameBaseScript";
import TaskManager, { MainTaskIdEnum } from "../JuQingChat/TaskManager";
import UserManager from "../Manager/UserManager";
import NewGuideScript from "../NewGuide/NewGuideScript";
import PrefabManage, { GameType } from "../PrefabManager/PrefabManage";
import GetAward from "../SCommon/GetAward";
import DHBase from "./DHBase";
const { ccclass, property, disallowMultiple, menu } = cc._decorator;

@ccclass
@disallowMultiple()
@menu('对话剧情/DH_35')
export default class DH_35 extends DHBase {
    chatConfig = [
        { effectUrl: 'DH/DH_35/sound/这?汉升哥哥,我们店被人砸了。', delayTime: 2.94, str: '这?汉升哥哥,我们店被人砸了。', posi: 'qipao1' },
        { effectUrl: 'DH/DH_35/sound/混蛋,肯定是柳家做的,他们这是彻底疯狂了。', delayTime: 3.93, str: '混蛋,肯定是柳家做的,他们这是彻底疯狂了。', posi: '-1' },
        { effectUrl: 'DH/DH_35/sound/那怎么办,他们不会再冲我们人来报复吧。', delayTime: 4.19, str: '那怎么办,他们不会再冲我们人来报复吧。', posi: 'qipao1' },
        { effectUrl: 'DH/DH_35/sound/瑶瑶你先回家,我来收拾,等我收拾好了,你再出门。', delayTime: 4.51, str: '瑶瑶你先回家,我来收拾,等我收拾好了,你再出门。', posi: '-1' },
        { effectUrl: 'DH/DH_35/sound/说,是不是你派人把我的店砸了?', delayTime: 2.68, str: '说,是不是你派人把我的店砸了?', posi: '-1' },
        { effectUrl: 'DH/DH_35/sound/说话要讲证据啊,小心我告你诽谤。', delayTime: 3.78, str: '说话要讲证据啊,小心我告你诽谤。', posi: 'qipao2' },
        { effectUrl: 'DH/DH_35/sound/不承认是吧,你给我等着,我会找到证据把你送进去的。', delayTime: 4.43, str: '不承认是吧,你给我等着,我会找到证据把你送进去的。', posi: '-1' },
        { effectUrl: 'DH/DH_35/sound/哪来的疯子,保安,赶紧将这个疯子赶出去。', delayTime: 5.03, str: '哪来的疯子,保安,赶紧将这个疯子赶出去。', posi: 'qipao2' },
        { effectUrl: 'DH/DH_35/sound/滚开,我自己走。', delayTime: 1.63, str: '滚开,我自己走。', posi: '-1' },
    ]

    @property(sp.Skeleton)
    nanzhu: sp.Skeleton = null
    @property(sp.Skeleton)
    suyao: sp.Skeleton = null

    @property(sp.Skeleton)
    nanzhu_bangongshi: sp.Skeleton = null
    @property(sp.Skeleton)
    liujiahao: sp.Skeleton = null

    @property(cc.Node)
    m_bg: cc.Node = null
    @property(cc.Node)
    MoveNodes: cc.Node = null

    start() {
        Common5.playRemoteAudioMusic('remotesound/effect/伤感-10秒', true)
        Common5.preLoadRemoteAudioByChatConfig(this.chatConfig);
        super.start();
        this.continueStory();
        // this.scheduleOnce(() => {
        //     PrefabManage.preloadPrefabByType(GameType.DH_359)
        // })
    }

    // 继续剧情
    continueStory() {
        this.scheduleOnce(() => {
            PrefabManage.preloadPrefabByType(GameType.DH_36)
        })
        this.m_bg.getChildByName("办公室场景").active = false
        this.nanzhu.setAnimation(0, "男主", true)
        this.suyao.setAnimation(0, "苏瑶", true)
        const dialogEvents = [
            (func) => {
                this.scheduleOnce(()=>{
                    this.runBg(1, 1.3, this.MoveNodes.getChildByName("男主苏瑶"), () => {
                        func()
                    })
                },2)
            },
            (func) => {
                this.showQiPao(this.chatConfig[this.dialogIndex++], func, () => {
                    this.suyao.setAnimation(0, "苏瑶说话", true)
                });
            },
            (func) => {
                this.suyao.setAnimation(0, "苏瑶", true)
                this.showClickDialog(this.chatConfig[this.dialogIndex++], func, () => {
                    this.nanzhu.setAnimation(0, "男主皱眉说话", true)
                });
            },
            (func) => {
                this.nanzhu.setAnimation(0, "男主皱眉", true)
                this.showQiPao(this.chatConfig[this.dialogIndex++], func, () => {
                    // this.suyao.setAnimation(0, "苏瑶颤抖说话", true)
                    this.suyao.setAnimation(0, "苏瑶说话", true)
                });
            },
            (func) => {
                this.suyao.node.active = false
                this.nanzhu.setAnimation(0, "男主搂苏瑶", false)
                this.runBg(1, 2, this.MoveNodes.getChildByName("男主抱苏瑶"), () => {
                })
                this.nanzhu.setCompleteListener(() => {
                    this.nanzhu.setCompleteListener(null)
                    this.nanzhu.setAnimation(0, "男主搂苏瑶待机", true)
                    this.showClickDialog(this.chatConfig[this.dialogIndex++], func, () => {
                        // this.nanzhu.setAnimation(0, "说话", true)
                        this.nanzhu.setAnimation(0, "男主搂苏瑶男说话", true)
                    });
                })
            },
            (func) => {
                PrefabManage.showBlackGuoDu(() => {
                    this.runBg(0, 1, this.node, () => {
                        // func()
                    })
                    this.m_bg.getChildByName("办公室场景").active = true
                    cc.audioEngine.stopMusic()
                    Common5.playRemoteAudioMusic('remotesound/effect/紧张氛围-10秒', true)
                }, () => {
                    func()
                })
            },
            // (func) => {
            //     this.runBg(0, 1, this.node, () => {
            //         func()
            //     })
            // },
            (func) => {
                this.liujiahao.setAnimation(0, "柳加豪", true)
                this.nanzhu_bangongshi.setAnimation(0, "男主", true)
                this.showClickDialog(this.chatConfig[this.dialogIndex++], func, () => {
                    this.nanzhu_bangongshi.setAnimation(0, "男主说话", true)
                });
            },
            (func) => {
                this.nanzhu_bangongshi.setAnimation(0, "男主", true)
                this.showQiPao(this.chatConfig[this.dialogIndex++], func, () => {
                    this.liujiahao.setAnimation(0, "柳加豪说话", true)
                });
            },
            (func) => {
                this.liujiahao.setAnimation(0, "柳加豪冷笑", true)
                this.showClickDialog(this.chatConfig[this.dialogIndex++], func, () => {
                    this.nanzhu_bangongshi.setAnimation(0, "男主说话", true)
                });
            },
            (func) => {
                this.nanzhu_bangongshi.setAnimation(0, "男主", true)
                this.showQiPao(this.chatConfig[this.dialogIndex++], func, () => {
                    this.liujiahao.setAnimation(0, "柳加豪说话", true)
                });
            },
            (func) => {
                this.liujiahao.setAnimation(0, "柳加豪冷笑", true)
                this.showClickDialog(this.chatConfig[this.dialogIndex++], func, () => {
                    // this.nanzhu_bangongshi.setAnimation(0, "男主说话", true)
                    this.nanzhu_bangongshi.timeScale = 0.5
                    this.nanzhu_bangongshi.setAnimation(0, "男主被拦走", false)
                });
            },
            // (func) => {
            //     this.nanzhu_bangongshi.setCompleteListener(() => {
            //         this.nanzhu_bangongshi.setCompleteListener(null)
            //         func()
            //     })
            // },
        ]
        const dialogCallFunc = (logEvents: any[]) => {
            logEvents.shift()(() => {
                if (logEvents.length > 0) {
                    dialogCallFunc(logEvents);
                } else {
                    TaskManager.finishCurMainTask(MainTaskIdEnum.MainTask_546)
                    TaskManager.setCurTask(MainTaskIdEnum.MainTask_547_1)
                    PrefabManage.showBlackGuoDu(() => {
                        PrefabManage.loadPrefabByType(GameType.DH_36)
                        this.node.removeFromParent()
                        this.node.destroy()
                    })
                    // PrefabManage.loadPrefabByType(GameType.GetAward, null, (prefabNode) => {
                    //     let goods = [{ goodId: 1733, goodNum: 1 }];
                    //     prefabNode.getComponent(GetAward).initView(goods, () => {
                    //         // TaskManager.finishCurMainTask(MainTaskIdEnum.MainTask_530)
                    //         // User.setRoomGoodStatus("DH_359")
                    //         // UserManager.clearAndaddStructureFlash("cf_FengRenYuan")
                    //     });
                    // });
                    // TaskManager.finishCurMainTask(MainTaskIdEnum.MainTask_202)
                    // TaskManager.setCurUnLockMainTaskInfo(MainTaskIdEnum.MainTask_203)
                    // this.node.destroy()
                }
            });
        }
        dialogCallFunc(dialogEvents);
    }

    guideView(nodeArray, opacity = -1) {
        PrefabManage.loadPrefabByType(GameType.GuideMskNode, this.node, (prefab) => {
            // prefab.zIndex = 199
            let guideNodeArray = nodeArray
            let firstNode = guideNodeArray.shift()
            prefab.getComponent(NewGuideScript).setBindNode(firstNode, guideNodeArray)
            if (opacity != -1) {
                prefab.getComponent(NewGuideScript).setOpacityMaskNode(opacity)
            }
        })
    }

    runBg(time, scale, endnode, callFunc) {
        cc.tween(this.m_bg)
            .to(time, { scale: scale })
            .start()
        cc.tween(this.m_bg)
            .sequence(
                cc.tween().to(time, { position: cc.v2(-endnode.position.x * scale, -endnode.position.y * scale) }),
                cc.tween().call(() => {
                    callFunc()
                })
            )
            .start()
    }
}