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

@ccclass
@disallowMultiple()
@menu('对话剧情/DH_31')
export default class DH_31 extends DHBase {
    chatConfig = [
        { effectUrl: 'DH/DH_31/sound/汉升,恭喜你成为厨师协会的副会长。', delayTime: 4.3, str: '汉升,恭喜你成为厨师协会的副会长。', posi: 'qipao5' },
        { effectUrl: 'DH/DH_31/sound/慢着,我不同意,这小子资历太浅,当不了副会长。', delayTime: 3.67, str: '慢着,我不同意,这小子资历太浅,当不了副会长。', posi: 'qipao6' },
        { effectUrl: 'DH/DH_31/sound/魏会长,我认为年轻人还是应该再历练几年。', delayTime: 4.3, str: '魏会长,我认为年轻人还是应该再历练几年。', posi: 'qipao2' },
        { effectUrl: 'DH/DH_31/sound/是啊,魏会长,就怕到时有流言说、他是靠您的关系当上的副会长呀。', delayTime: 8.79, str: '是啊,魏会长,就怕到时有流言说、他是靠您的关系当上的副会长呀。', posi: 'qipao4' },
        { effectUrl: 'DH/DH_31/sound/师傅,您别生气,我自己来。', delayTime: 2.73, str: '师傅,您别生气,我自己来。', posi: '-1' },
        { effectUrl: 'DH/DH_31/sound/那我们切磋下吧,协会有规定赢了副会长就够资格吧。', delayTime: 4.35, str: '那我们切磋下吧,协会有规定赢了副会长就够资格吧。', posi: '-1' },
        { effectUrl: 'DH/DH_31/sound/切磋厨艺?输了滚出协会,怎么样敢不敢?', delayTime: 7.01, str: '切磋厨艺?输了滚出协会,怎么样敢不敢?', posi: 'qipao4' },
        { effectUrl: 'DH/DH_31/sound/那要是你们输了呢?', delayTime: 1.14, str: '那要是你们输了呢?', posi: '-1' },
        { effectUrl: 'DH/DH_31/sound/我们怎么可能会输,我们输了将省城的门店转给你。', delayTime: 5.45, str: '我们怎么可能会输,我们输了将省城的门店转给你。', posi: 'qipao4' },
        { effectUrl: 'DH/DH_31/sound/好,落字为据。', delayTime: 1.61, str: '好,落字为据。', posi: '-1' },


        { effectUrl: 'DH/DH_31/sound/那你们和我徒弟切磋下厨艺吧,谁赢了谁当副会长。', delayTime: 6.1, str: '那你们和我徒弟切磋下厨艺吧,谁赢了谁当副会长。', posi: '-1' },
        { effectUrl: 'DH/DH_31/sound/行啊,我们输了将省城的门店也转给你。', delayTime: 5.0, str: '行啊,我们输了将省城的门店也转给你。', posi: '-1' },
        { effectUrl: 'DH/DH_31/sound/好,落字为据。', delayTime: 1.61, str: '好,落字为据。', posi: '-1' },
    ]

    @property(sp.Skeleton)
    nanzhu: sp.Skeleton = null
    @property(sp.Skeleton)
    weihuizhang: sp.Skeleton = null
    @property(sp.Skeleton)
    liujiahao: sp.Skeleton = null
    @property(sp.Skeleton)
    liuhongfu: sp.Skeleton = null
    @property(sp.Skeleton)
    xieyi: sp.Skeleton = null
    @property(sp.Skeleton)
    yinxiao: sp.Skeleton = null

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

    @property(cc.Node)
    kuang: cc.Node = null
    @property(cc.Node)
    lamianbg: cc.Node = null

    start() {
        Common5.playRemoteAudioMusic('remotesound/effect/紧张氛围-10秒', true)
        Common5.preLoadRemoteAudioByChatConfig(this.chatConfig);
        super.start();
        this.continueStory();
        // this.scheduleOnce(() => {
        //     PrefabManage.preloadPrefabByType(GameType.DH_319)
        // })
    }

    // 继续剧情
    continueStory() {
        this.scheduleOnce(() => {
            PrefabManage.preloadPrefabByType(GameType.DH_32)
        })
        this.nanzhu.setAnimation(0, "男主待机", true)
        this.weihuizhang.setAnimation(0, "会长给聘书", true)
        this.liujiahao.node.active = false
        this.liuhongfu.node.active = false
        const dialogEvents = [
            // (func) => {
            //     this.runBg(0, 3, this.MoveNodes.getChildByName("男主会长"), () => {
            //         func()
            //     })
            // },
            (func) => {
                this.showQiPao(this.chatConfig[this.dialogIndex++], func, () => {
                    // this.nanzhu.setAnimation(0, "说话", true)
                });
            },
            (func) => {
                this.yinxiao.node.active = true
                this.showQiPao(this.chatConfig[this.dialogIndex++], func, () => {
                    // this.liuhongfu.setAnimation(0, "柳洪福说话", true)
                });
                // this.runBg(1, 1, this.node, () => {
                //     func()
                // })
            },
            (func) => {
                this.yinxiao.node.active = false
                this.weihuizhang.setAnimation(0, "会长", true)
                this.liujiahao.node.active = true
                this.liuhongfu.node.active = true
                this.liujiahao.setAnimation(0, "柳加豪待机", true)
                this.liuhongfu.setAnimation(0, "柳洪福", true)
                func()
                // this.showQiPao(this.chatConfig[this.dialogIndex++], func, () => {
                //     this.liuhongfu.setAnimation(0, "柳洪福说话", true)
                // });
            },
            // (func) => {
            //     this.runBg(1, 1, this.node, () => {
            //         func()
            //     })
            // },
            (func) => {
                this.showQiPao(this.chatConfig[this.dialogIndex++], func, () => {
                    // this.nanzhu.setAnimation(0, "说话", true)
                });
            },
            (func) => {
                this.liuhongfu.setAnimation(0, "柳洪福", true)
                this.showQiPao(this.chatConfig[this.dialogIndex++], func, () => {
                    this.liujiahao.setAnimation(0, "柳加豪说话", true)
                });
            },
            (func) => {
                PrefabManage.loadPrefabByType(GameType.selectButtons, this.node, (node) => {
                    let strs = ['上前自己处理', '让师傅处理']
                    let actives = [true, true]
                    let funcs = [
                        () => {
                            func()
                        },
                        () => {
                            this.liujiahao.setAnimation(0, "柳加豪待机", true)
                            this.showQiPao(this.chatConfig[10], () => {
                                this.weihuizhang.setAnimation(0, "会长", true)
                                this.showQiPao(this.chatConfig[11], () => {
                                    this.liujiahao.setAnimation(0, "柳加豪待机", true)
                                    this.showClickDialog(this.chatConfig[12], () => {
                                        this.xieyi.node.active = true
                                        this.xieyi.setAnimation(0, "静止", true)
                                        this.scheduleOnce(() => {
                                            this.xieyi.setAnimation(0, "盖章", false)
                                            this.xieyi.setCompleteListener(() => {
                                                this.xieyi.setCompleteListener(null)
                                                this.xieyi.setAnimation(0, "静止2", true)
                                                this.kuang.active = true
                                            })
                                        }, 0.5)
                                    }, () => {
                                        this.nanzhu.setAnimation(0, "男主说话", true)
                                    });
                                }, () => {
                                    this.liujiahao.setAnimation(0, "柳加豪说话", true)
                                });
                            }, () => {
                                this.weihuizhang.setAnimation(0, "会长说话", true)
                            });
                        },
                    ]
                    let isvideo = [true, false]
                    node.getComponent(selectButtons).init(strs, actives, funcs, isvideo)
                })
            },
            (func) => {
                this.weihuizhang.setAnimation(0, "会长指人", true)
                this.liujiahao.setAnimation(0, "柳加豪待机", true)
                this.showClickDialog(this.chatConfig[this.dialogIndex++], func, () => {
                    this.weihuizhang.node.active = false
                    this.nanzhu.setAnimation(0, "男主拨开会长", false)
                    this.nanzhu.setCompleteListener(() => {
                        this.nanzhu.setCompleteListener(null)
                        this.nanzhu.setAnimation(0, "男主拨开会长说话", true)
                    })
                });
            },
            (func) => {
                this.nanzhu.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.liujiahao.setAnimation(0, "柳加豪说话", true)
                });
            },
            (func) => {
                this.liujiahao.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.liujiahao.setAnimation(0, "柳加豪说话", true)
                });
            },
            (func) => {
                this.liujiahao.setAnimation(0, "柳加豪待机", true)
                this.showClickDialog(this.chatConfig[this.dialogIndex++], func, () => {
                    this.nanzhu.setAnimation(0, "男主拨开会长说话", true)
                });
            },
            (func) => {
                this.xieyi.node.active = true
                this.xieyi.setAnimation(0, "静止", true)
                this.scheduleOnce(() => {
                    this.xieyi.setAnimation(0, "盖章", false)
                    this.xieyi.setCompleteListener(() => {
                        this.xieyi.setCompleteListener(null)
                        this.xieyi.setAnimation(0, "静止2", true)
                        this.kuang.active = true
                    })
                }, 0.5)
            },
        ]

        const dialogCallFunc = (logEvents: any[]) => {
            logEvents.shift()(() => {
                if (logEvents.length > 0) {
                    dialogCallFunc(logEvents);
                } else {
                    // TaskManager.finishCurMainTask(MainTaskIdEnum.MainTask_540)
                    // TaskManager.setCurTask(MainTaskIdEnum.MainTask_541)
                    // PrefabManage.showBlackGuoDu(() => {
                    //     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_319")
                    //         // 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()
    }


    runSchedule() {
        let touxiang1 = this.lamianbg.getChildByName("touxiang1")
        let touxiang2 = this.lamianbg.getChildByName("touxiang2")
        // touxiang1.setPosition(cc.v2(touxiang1.position.x + 31, touxiang1.position.y))
        touxiang2.setPosition(cc.v2(touxiang2.position.x + 30, touxiang2.position.y))
        if (touxiang2.position.x >= 300) {
            touxiang2.setPosition(cc.v3(300, touxiang2.position.y, 0))
            this.unschedule(this.runSchedule)
            PrefabManage.loadPrefabByType(GameType.taskState, this.node, (node) => {
                node.getComponent(taskState).showFail("挑战失败", () => {
                    this.onTouchJoin();
                }, () => {
                    touxiang1.position.x = 300
                    this.unschedule(this.runSchedule)
                    PrefabManage.showTextTips("挑战成功")
                    let ChuShiXieHuiLevel = User.getChuShiXieHuiLevel()
                    // UserManager.subMoney(this.config.moneystr)
                    ChuShiXieHuiLevel[4] = 1
                    User.setChuShiXieHuiLevel(ChuShiXieHuiLevel)

                    TaskManager.finishCurMainTask(MainTaskIdEnum.MainTask_540)
                    TaskManager.setCurTask(MainTaskIdEnum.MainTask_541_1)
                    PrefabManage.loadPrefabByType(GameType.taskState, this.node, (node) => {
                        node.getComponent(taskState).showSuccess("挑战成功,成为协会副会长", () => {
                            PrefabManage.showBlackGuoDu(() => {
                                this.node.removeFromParent()
                                this.node.destroy()
                            })
                        })
                    })
                })
            })
            return
        }
        if (touxiang1.position.x >= 300) {
            touxiang1.position.x = 300
            this.unschedule(this.runSchedule)
            PrefabManage.showTextTips("挑战成功")
            let ChuShiXieHuiLevel = User.getChuShiXieHuiLevel()
            // UserManager.subMoney(this.config.moneystr)
            ChuShiXieHuiLevel[4] = 1
            User.setChuShiXieHuiLevel(ChuShiXieHuiLevel)

            TaskManager.finishCurMainTask(MainTaskIdEnum.MainTask_540)
            TaskManager.setCurTask(MainTaskIdEnum.MainTask_541_1)
            PrefabManage.loadPrefabByType(GameType.taskState, this.node, (node) => {
                node.getComponent(taskState).showSuccess("挑战成功,成为协会副会长", () => {
                    PrefabManage.showBlackGuoDu(() => {
                        this.node.removeFromParent()
                        this.node.destroy()
                    })
                })
            })
        }
    }

    onTouchJoin() {
        this.kuang.active = false
        this.lamianbg.active = true
        let touxiang1 = this.lamianbg.getChildByName("touxiang1")
        let touxiang2 = this.lamianbg.getChildByName("touxiang2")
        touxiang1.setPosition(cc.v3(-312, touxiang1.position.y, 0))
        touxiang2.setPosition(cc.v3(-312, touxiang1.position.y, 0))
        this.lamianbg.getChildByName("拉面柳洪福").getComponent(sp.Skeleton).setAnimation(0, "揉面", true)
        this.lamianbg.getChildByName("拉面男主").getComponent(sp.Skeleton).setAnimation(0, "揉面", true)
        this.schedule(this.runSchedule, 1, cc.macro.REPEAT_FOREVER, 1)
    }

    onTouchLaMian() {
        let touxiang1 = this.lamianbg.getChildByName("touxiang1")
        touxiang1.setPosition(cc.v2(touxiang1.position.x + 50, touxiang1.position.y))
        if (touxiang1.position.x >= 300) {
            touxiang1.setPosition(cc.v3(300, touxiang1.position.y, 0))
        }
    }
}