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 CommonTips from "../SCommon/CommonTips";
import GetAward from "../SCommon/GetAward";
import DHBase from "./DHBase";
import selectButtons from "./selectButtons";
const { ccclass, property, disallowMultiple, menu } = cc._decorator;

@ccclass
@disallowMultiple()
@menu('对话剧情/DH_14')
export default class DH_14 extends DHBase {
    chatConfig = [
        { effectUrl: 'DH/DH_14/sound/小子,混得不错啊,竟然到镇中心来开店铺了。', delayTime: 5.5, str: '小子,混得不错啊,竟然到镇中心来开店铺了。', posi: 'qipao2' },
        { effectUrl: 'DH/DH_14/sound/哟!龙哥来了啊,来里面请。', delayTime: 2.68, str: '哟!龙哥来了啊,来里面请。', posi: '-1' },
        { effectUrl: 'DH/DH_14/sound/小子,开店有跟龙哥打招呼吗?', delayTime: 2.57, str: '小子,开店有跟龙哥打招呼吗?', posi: 'qipao4' },
        { effectUrl: 'DH/DH_14/sound/龙哥是这的镇长?那真不好意思,有眼不识泰山了。', delayTime: 4.4, str: '龙哥是这的镇长?那真不好意思,有眼不识泰山了。', posi: '-1' },
        { effectUrl: 'DH/DH_14/sound/胡说八道什么呢,韩伯父是厨师协会的会员,开店必须到龙哥这报备。', delayTime: 6.05, str: '胡说八道什么呢,韩伯父是厨师协会的会员,开店必须到龙哥这报备。', posi: 'qipao4' },
        { effectUrl: 'DH/DH_14/sound/厨师协会啊,那我报备了啊。', delayTime: 2.34, str: '厨师协会啊,那我报备了啊。', posi: '-1' },
        { effectUrl: 'DH/DH_14/sound/放屁,没找我报备不准开业,给我把店封了。', delayTime: 4.61, str: '放屁,没找我报备不准开业,给我把店封了。', posi: 'qipao2' },
        { effectUrl: 'DH/DH_14/sound/小子,要想在这开店,来找我来报备。', delayTime: 4.69, str: '小子,要想在这开店,来找我来报备。', posi: 'qipao2' },
    ]

    @property(sp.Skeleton)
    nanzhu: sp.Skeleton = null
    @property(sp.Skeleton)
    longge: sp.Skeleton = null
    @property(sp.Skeleton)
    xiaodi: sp.Skeleton = null

    @property(sp.Skeleton)
    texie: 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_149)
        // })
    }

    // 继续剧情
    continueStory() {
        this.scheduleOnce(() => {
            PrefabManage.preloadPrefabByType(GameType.DH_4)
        })
        this.nanzhu.setAnimation(0, "男主待机", true)
        this.longge.setAnimation(0, "龙哥待机", true)
        this.xiaodi.setAnimation(0, "小弟待机", true)
        const dialogEvents = [
            // (func) => {
            //     this.runBg(1, 2, this.MoveNodes.getChildByName("龙哥小弟"), () => {
            //         func()
            //     })
            // },
            (func) => {
                this.showQiPao(this.chatConfig[this.dialogIndex++], func, () => {
                    this.longge.setAnimation(0, "龙哥说话", true)
                });
            },
            (func) => {
                this.longge.setAnimation(0, "龙哥待机", true)
                // this.runBg(1, 3, this.MoveNodes.getChildByName("男主"), () => {
                func()
                // })
            },
            (func) => {
                this.showClickDialog(this.chatConfig[this.dialogIndex++], func, () => {
                    this.nanzhu.setAnimation(0, "男主请人说话", true)
                });
            },
            (func) => {
                this.nanzhu.setAnimation(0, "男主待机", true)
                // this.runBg(1, 1, this.node, () => {
                func()
                // })
            },
            (func) => {
                this.showQiPao(this.chatConfig[this.dialogIndex++], func, () => {
                    this.xiaodi.setAnimation(0, "小弟指人说话", true)
                });
            },
            (func) => {
                this.xiaodi.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.xiaodi.setAnimation(0, "小弟说话", true)
                });
            },
            (func) => {
                this.xiaodi.setAnimation(0, "小弟待机", true)
                this.showClickDialog(this.chatConfig[this.dialogIndex++], func, () => {
                    this.nanzhu.setAnimation(0, "男主说话", true)
                });
            },
            (func) => {
                this.nanzhu.setAnimation(0, "男主待机", true)
                this.xiaodi.setAnimation(0, "小弟拿封条待机", true)
                this.showQiPao(this.chatConfig[this.dialogIndex++], func, () => {
                    this.longge.setAnimation(0, "龙哥说话", true)
                });
            },
            (func) => {
                PrefabManage.loadPrefabByType(GameType.selectButtons, this.node, (node) => {
                    let strs = ['上前阻拦', '无动于衷']
                    let actives = [true, true]
                    let funcs = [
                        () => {
                            func()
                        },
                        () => {
                            this.nanzhu.node.active = false
                            this.xiaodi.setAnimation(0, "小弟贴封条待机", true)
                            TaskManager.finishCurMainTask(MainTaskIdEnum.MainTask_515)
                            TaskManager.setCurTask(MainTaskIdEnum.MainTask_516_1)
                            PrefabManage.loadPrefabByType(GameType.commonTips, null, (node) => {
                                node.getComponent(CommonTips).init(`镇中心门店因龙哥找茬门店被封`, () => {
                                    PrefabManage.showBlackGuoDu(() => {
                                        PrefabManage.loadPrefabByType(GameType.DH_15)
                                        this.node.removeFromParent()
                                        this.node.destroy()
                                    })
                                })
                            })
                        },
                    ]
                    let isvideo = [true, false]
                    node.getComponent(selectButtons).init(strs, actives, funcs, isvideo)
                })
            },
            (func) => {
                this.nanzhu.node.active = false
                this.longge.setAnimation(0, "龙哥待机", true)
                this.xiaodi.setAnimation(0, "小弟贴封条", false)
                Common5.playRemoteAudioEffect("remotesound/effect/踢人声")
                this.texie.node.active = true
                this.texie.setAnimation(0, "撞击", false)
                this.texie.setCompleteListener(() => {
                    this.texie.setCompleteListener(null)
                    this.texie.node.active = false
                    this.xiaodi.setAnimation(0, "小弟贴封条待机", true)
                    func()
                })
            },
            (func) => {
                this.showQiPao(this.chatConfig[this.dialogIndex++], func, () => {
                    this.longge.setAnimation(0, "龙哥说话", true)
                });
            },
        ]
        const dialogCallFunc = (logEvents: any[]) => {
            logEvents.shift()(() => {
                if (logEvents.length > 0) {
                    dialogCallFunc(logEvents);
                } else {
                    TaskManager.finishCurMainTask(MainTaskIdEnum.MainTask_515)
                    TaskManager.setCurTask(MainTaskIdEnum.MainTask_516_1)
                    PrefabManage.loadPrefabByType(GameType.commonTips, null, (node) => {
                        node.getComponent(CommonTips).init(`镇中心门店因龙哥找茬门店被封`, () => {
                            PrefabManage.showBlackGuoDu(() => {
                                PrefabManage.loadPrefabByType(GameType.DH_15)
                                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_149")
                    //         // 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()
    }
}