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_16')
export default class DH_16 extends DHBase {
    chatConfig = [
        { effectUrl: 'DH/DH_16/sound/我说话不好使是吧。没报备还敢私自开业?', delayTime: 4.43, str: '我说话不好使是吧。没报备还敢私自开业?', posi: 'qipao2' },
        { effectUrl: 'DH/DH_16/sound/我已经报备了,再说你也没有权利封我的门店。', delayTime: 3.2, str: '我已经报备了,再说你也没有权利封我的门店。', posi: '-1' },
        { effectUrl: 'DH/DH_16/sound/没有跟我们报备就不算。', delayTime: 2.47, str: '没有跟我们报备就不算。', posi: 'qipao2' },
        // { effectUrl: 'DH/DH_16/sound/爸,他们这是屡教不改,得罚款了吧。', delayTime: 3.57, str: '爸,他们这是屡教不改,得罚款了吧。', posi: 'qipao2' },
        // { effectUrl: 'DH/DH_16/sound/嗯,屡教不改,罚款2亿。', delayTime: 2.99, str: '嗯,屡教不改,罚款2亿。', posi: 'qipao4' },
        { effectUrl: 'DH/DH_16/sound/我现在也是协会会员!', delayTime: 1.45, str: '我现在也是协会会员!', posi: '-1' },
        { effectUrl: 'DH/DH_16/sound/胆子不小啊,还伪造证件。', delayTime: 4.19, str: '胆子不小啊,还伪造证件。', posi: 'qipao2' },
        { effectUrl: 'DH/DH_16/sound/谁告诉你的证件假的,还敢丢了,你好大的胆子。', delayTime: 6.0, str: '谁告诉你的证件假的,还敢丢了,你好大的胆子。', posi: 'qipao1' },
        { effectUrl: 'DH/DH_16/sound/哪里来的糟老头子,哪凉快哪呆着去。', delayTime: 3.88, str: '哪里来的糟老头子,哪凉快哪呆着去。', posi: 'qipao2' },
        { effectUrl: 'DH/DH_16/sound/您是魏会长?刚太远没认出您,真是不好意思。', delayTime: 4.77, str: '您是魏会长?刚太远没认出您,真是不好意思。', posi: 'qipao5' },
        { effectUrl: 'DH/DH_16/sound/你眼里还有协会?都敢封店、罚款?谁给你的权利。', delayTime: 6.34, str: '你眼里还有协会?都敢封店、罚款?谁给你的权利。', posi: 'qipao1' },
        { effectUrl: 'DH/DH_16/sound/魏会长,我错了,我保证以后不会了。', delayTime: 3.65, str: '魏会长,我错了,我保证以后不会了。', posi: 'qipao5' },
        { effectUrl: 'DH/DH_16/sound/不要跟我道歉,跟执法人员解释吧,你已经被开除协会了。', delayTime: 6.44, str: '不要跟我道歉,跟执法人员解释吧,你已经被开除协会了。', posi: 'qipao1' },
        { effectUrl: 'DH/DH_16/sound/不要啊,魏会长,我们知道错了,求您放过我吧。', delayTime: 4.46, str: '不要啊,魏会长,我们知道错了,求您放过我吧。', posi: 'qipao6' },
    ]

    @property(sp.Skeleton)
    spine: sp.Skeleton = null
    @property(sp.Skeleton)
    weihuizhang: 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_169)
        // })
    }

    // 继续剧情
    continueStory() {
        this.scheduleOnce(() => {
            PrefabManage.preloadPrefabByType(GameType.DH_17)
        })
        this.m_bg.getChildByName("namenodes").active = true
        this.spine.setAnimation(0, "待机", true)
        this.weihuizhang.node.active = false
        const dialogEvents = [
            // (func) => {
            //     this.runBg(1, 1.5, this.MoveNodes.getChildByName("韩龙"), () => {
            //         func()
            //     })
            // },
            (func) => {
                this.showQiPao(this.chatConfig[this.dialogIndex++], func, () => {
                    this.spine.setAnimation(0, "龙说话", true)
                });
            },
            (func) => {
                this.spine.setAnimation(0, "待机", true)
                // this.runBg(1, 3, this.MoveNodes.getChildByName("男主"), () => {
                func()
                // })
            },
            (func) => {
                this.showClickDialog(this.chatConfig[this.dialogIndex++], func, () => {
                    this.spine.setAnimation(0, "男主说话", true)
                });
            },
            (func) => {
                // this.runBg(1, 1.5, this.MoveNodes.getChildByName("韩龙"), () => {
                func()
                // })
            },
            (func) => {
                this.showQiPao(this.chatConfig[this.dialogIndex++], func, () => {
                    this.spine.setAnimation(0, "龙说话", true)
                });
            },
            // (func) => {
            //     this.showQiPao(this.chatConfig[this.dialogIndex++], func, () => {
            //         this.spine.setAnimation(0, "龙转头说话", true)
            //     });
            // },
            // (func) => {
            //     this.showQiPao(this.chatConfig[this.dialogIndex++], func, () => {
            //         this.spine.setAnimation(0, "龙转头韩说话", true)
            //     });
            // },
            (func) => {
                this.spine.setAnimation(0, "待机", true)
                // this.runBg(1, 1, this.node, () => {
                func()
                // })
            },
            (func) => {
                this.showClickDialog(this.chatConfig[this.dialogIndex++], func, () => {
                    this.spine.setAnimation(0, "男主说话", true)
                });
            },
            (func) => {
                this.spine.setAnimation(0, "男主拿证件", false)
                this.spine.setCompleteListener(() => {
                    this.spine.setCompleteListener(null)
                    func()
                })
            },
            (func) => {
                Common5.playRemoteAudioEffect("remotesound/effect/丢东西")
                this.spine.setAnimation(0, "龙摔证件", false)
                this.spine.setCompleteListener(() => {
                    this.spine.setCompleteListener(null)
                    func()
                })
            },
            (func) => {
                this.showQiPao(this.chatConfig[this.dialogIndex++], func, () => {
                    this.spine.setAnimation(0, "龙说话", true)
                });
            },
            (func) => {
                this.spine.setAnimation(0, "待机", true)
                PrefabManage.loadPrefabByType(GameType.selectButtons, this.node, (node) => {
                    let strs = ['请出师傅', '自己处理']
                    let actives = [true, true]
                    let funcs = [
                        () => {
                            func()
                        },
                        () => {
                            PrefabManage.loadPrefabByType(GameType.commonTips, null, (node) => {
                                node.getComponent(CommonTips).init(`需要扣除当前所有积蓄方能解锁门店`, () => {
                                    UserManager.subMoney(User.getMoney())
                                    this.goTask_1()
                                }, '确认', '直接通过', false, true, () => {
                                    this.goTask_1()
                                })
                            })
                        },
                    ]
                    let isvideo = [true, false]
                    node.getComponent(selectButtons).init(strs, actives, funcs, isvideo)
                })
            },
            (func) => {
                this.spine.setAnimation(0, "待机", true)
                let position = this.weihuizhang.node.position.clone()
                cc.tween(this.weihuizhang.node)
                    .sequence(
                        cc.tween().by(0, { position: cc.v3(600, position.y, 0) }),
                        cc.tween().set({ active: true }),
                        cc.tween().to(0.5, { position: position }),
                        cc.tween().call(() => {
                            this.showQiPao(this.chatConfig[this.dialogIndex++], func, () => {
                                this.weihuizhang.setAnimation(0, "魏会长说话", true)
                            });
                        })
                    )
                    .start()
            },
            (func) => {
                this.weihuizhang.setAnimation(0, "魏会长待机", true)
                this.showQiPao(this.chatConfig[this.dialogIndex++], func, () => {
                    this.spine.setAnimation(0, "龙说话", true)
                });
            },
            (func) => {
                this.spine.setAnimation(0, "韩扒开龙", false)
                this.spine.setCompleteListener(() => {
                    this.spine.setCompleteListener(null)
                    this.showQiPao(this.chatConfig[this.dialogIndex++], func, () => {
                        this.spine.setAnimation(0, "韩扒鞠躬说话", true)
                    });
                })
            },
            (func) => {
                this.spine.setAnimation(0, "韩扒鞠躬待机", true)
                this.showQiPao(this.chatConfig[this.dialogIndex++], func, () => {
                    this.weihuizhang.setAnimation(0, "魏会长说话", true)
                });
            },
            (func) => {
                this.weihuizhang.setAnimation(0, "魏会长待机", true)
                this.showQiPao(this.chatConfig[this.dialogIndex++], func, () => {
                    this.spine.setAnimation(0, "韩扒鞠躬说话", true)
                });
            },
            (func) => {
                this.spine.setAnimation(0, "韩扒鞠躬待机", true)
                this.showQiPao(this.chatConfig[this.dialogIndex++], func, () => {
                    this.weihuizhang.setAnimation(0, "魏会长说话", true)
                });
            },
            (func) => {
                this.spine.setAnimation(0, "龙韩跪下", false)
                this.spine.setCompleteListener(() => {
                    this.spine.setCompleteListener(null)
                    this.showQiPao(this.chatConfig[this.dialogIndex++], func, () => {
                        this.spine.setAnimation(0, "龙韩跪下说话", true)
                    });
                })
            },
            (func) => {
                this.spine.setAnimation(0, "龙韩跪下待机", true)
                func()
            },
        ]
        const dialogCallFunc = (logEvents: any[]) => {
            logEvents.shift()(() => {
                if (logEvents.length > 0) {
                    dialogCallFunc(logEvents);
                } else {
                    this.goTask()
                    // 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_169")
                    //         // UserManager.clearAndaddStructureFlash("cf_FengRenYuan")
                    //     });
                    // });
                    // TaskManager.finishCurMainTask(MainTaskIdEnum.MainTask_202)
                    // TaskManager.setCurUnLockMainTaskInfo(MainTaskIdEnum.MainTask_203)
                    // this.node.destroy()
                }
            });
        }
        dialogCallFunc(dialogEvents);
    }

    goTask() {
        TaskManager.finishCurMainTask(MainTaskIdEnum.MainTask_517)
        TaskManager.setCurTask(MainTaskIdEnum.MainTask_518_1)
        PrefabManage.loadPrefabByType(GameType.commonTips, null, (node) => {
            node.getComponent(CommonTips).init(`镇中心美食店已解封`, () => {
                PrefabManage.showBlackGuoDu(() => {
                    PrefabManage.loadPrefabByType(GameType.DH_17)
                    this.node.removeFromParent()
                    this.node.destroy()
                })
            })
        })
    }

    goTask_1() {
        TaskManager.finishCurMainTask(MainTaskIdEnum.MainTask_517)
        TaskManager.setCurTask(MainTaskIdEnum.MainTask_518_1)
        PrefabManage.showBlackGuoDu(() => {
            PrefabManage.loadPrefabByType(GameType.DH_17)
            this.node.removeFromParent()
            this.node.destroy()
        })
    }

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