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

@ccclass
@disallowMultiple()
@menu('对话剧情/DH_20')
export default class DH_20 extends DHBase {
    chatConfig = [
        { effectUrl: 'DH/DH_20/sound/小子,我父亲准备收你为徒,还不快跪下拜师。', delayTime: 4.12, str: '小子,我父亲准备收你为徒,还不快跪下拜师。', posi: 'qipao2' },
        { effectUrl: 'DH/DH_20/sound/柳少爷,你们高看了,我一个摆摊的小贩受不起啊。', delayTime: 4.59, str: '柳少爷,你们高看了,我一个摆摊的小贩受不起啊。', posi: '-1' },
        { effectUrl: 'DH/DH_20/sound/给脸不要脸,还敢拒绝,小心让你吃不了兜着走。', delayTime: 4.74, str: '给脸不要脸,还敢拒绝,小心让你吃不了兜着走。', posi: 'qipao2' },
        { effectUrl: 'DH/DH_20/sound/哼!不识抬举,我们走!', delayTime: 2.31, str: '哼!不识抬举,我们走!', posi: 'qipao3' },
        { effectUrl: 'DH/DH_20/sound/哟!好大的口气哦,让我见见是怎么吃不了兜着走。', delayTime: 5.06, str: '哟!好大的口气哦,让我见见是怎么吃不了兜着走。', posi: '-1' },
        { effectUrl: 'DH/DH_20/sound/放肆,你想干什么!', delayTime: 2.47, str: '放肆,你想干什么!', posi: 'qipao1' },
        { effectUrl: '', delayTime: 0, str: '占位', posi: '-1' },
        { effectUrl: 'DH/DH_20/sound/不就是没答应让给你冠军吗?我答应你还不行吗', delayTime: 3.59, str: '不就是没答应让给你冠军吗?我答应你还不行吗', posi: '-1' },
        { effectUrl: 'DH/DH_20/sound/真是好大的势力,在大赛作弊不成还当众打人。', delayTime: 5.87, str: '真是好大的势力,在大赛作弊不成还当众打人。', posi: 'qipao1' },
        { effectUrl: 'DH/DH_20/sound/你谁啊,敢管我的事!滚远点,小心溅你一身血。', delayTime: 5.34, str: '你谁啊,敢管我的事!滚远点,小心溅你一身血。', posi: 'qipao2' },
        { effectUrl: 'DH/DH_20/sound/魏会长,孩子年龄还小,不懂事冒犯了您,求您高抬贵手。', delayTime: 7.51, str: '魏会长,孩子年龄还小,不懂事冒犯了您,求您高抬贵手。', posi: 'qipao3' },
        { effectUrl: 'DH/DH_20/sound/会长?', delayTime: 1, str: '会长?', posi: 'qipao4' },
        { effectUrl: 'DH/DH_20/sound/汉升,你怎么想的?', delayTime: 2.29, str: '汉升,你怎么想的?', posi: 'qipao1' },
        { effectUrl: 'DH/DH_20/sound/师傅,算了吧,他也没伤到我。', delayTime: 3.04, str: '师傅,算了吧,他也没伤到我。', posi: '-1' },
        { effectUrl: 'DH/DH_20/sound/师傅,柳老爷子已经教训了柳公子,柳公子知道错了。', delayTime: 5.06, str: '师傅,柳老爷子已经教训了柳公子,柳公子知道错了。', posi: '-1' },
    ]

    @property(sp.Skeleton)
    nanzhu: sp.Skeleton = null
    @property(sp.Skeleton)
    liujiahao: sp.Skeleton = null
    @property(sp.Skeleton)
    liuhongfu: sp.Skeleton = null
    @property(sp.Skeleton)
    weihuizhang: 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_209)
        // })
    }

    // 继续剧情
    continueStory() {
        this.scheduleOnce(() => {
            PrefabManage.preloadPrefabByType(GameType.DH_21)
        })
        this.nanzhu.setAnimation(0, "男主", true)
        this.liujiahao.setAnimation(0, "豪待机", true)
        this.liuhongfu.setAnimation(0, "柳鸿福", true)
        this.weihuizhang.node.active = false
        const dialogEvents = [
            (func) => {
                this.showQiPao(this.chatConfig[0], func, () => {
                    this.liujiahao.setAnimation(0, "豪说话", true)
                });
            },
            (func) => {
                PrefabManage.loadPrefabByType(GameType.selectButtons, this.node, (node) => {

                    let strs = ['接受', '不接受']
                    let actives = [true, true]
                    let funcs = [
                        () => {
                            PrefabManage.loadPrefabByType(GameType.taskState, this.node, (node) => {
                                node.getComponent(taskState).showFail("任务失败,因你接受了柳鸿福收徒,得罪了协会会长,被驱逐出厨师协会", () => {
                                    this.continueStory();
                                }, () => {
                                    this.showQiPao(this.chatConfig[1], func, () => {
                                        this.nanzhu.setAnimation(0, "男主说话", true)
                                    });
                                })
                            })
                            // PrefabManage.showTextTips("任务失败,因你接受了柳鸿福收徒,得罪了协会会长,被驱逐出厨师协会")
                        },
                        () => {
                            this.showQiPao(this.chatConfig[1], func, () => {
                                this.nanzhu.setAnimation(0, "男主说话", true)
                            });
                        },
                    ]
                    let isvideo = [false, false]
                    node.getComponent(selectButtons).init(strs, actives, funcs, isvideo)

                    // node.getComponent(selectButton).init("接收", "不接受", () => {
                    //     PrefabManage.showTextTips("任务失败,因你接受了柳鸿福收徒,得罪了协会会长,被驱逐出厨师协会")
                    //     this.continueStory();
                    //     // this.nanzhu.setAnimation(0,"男主下跪",false)
                    //     // this.nanzhu.setCompleteListener(()=>{
                    //     //     this.nanzhu.setCompleteListener(null)
                    //     //     this.nanzhu.setAnimation(0,"男主下跪",true)
                    //     //     this.continueStory();
                    //     // })
                    // }, () => {
                    //     this.showQiPao(this.chatConfig[1], func, () => {
                    //         this.nanzhu.setAnimation(0, "男主说话", true)
                    //     });
                    // })
                })
            },
            (func) => {
                this.nanzhu.setAnimation(0, "男主", true)
                this.showQiPao(this.chatConfig[2], func, () => {
                    this.liujiahao.setAnimation(0, "豪指人说话", true)
                });
            },
            (func) => {
                this.liujiahao.setAnimation(0, "豪待机", true)
                this.showQiPao(this.chatConfig[3], func, () => {
                    this.liuhongfu.setAnimation(0, "柳鸿福说话", true)
                });
            },
            (func) => {
                this.liuhongfu.setAnimation(0, "柳鸿福", true)
                PrefabManage.loadPrefabByType(GameType.selectButtons, this.node, (node) => {

                    let strs = ['让他们走,私下再教训柳家豪', '直接打脸柳家豪再教训一顿']
                    let actives = [true, true]
                    let funcs = [
                        () => {
                            cc.sys.localStorage.setItem("DH_20选择",'1')
                            PrefabManage.loadPrefabByType(GameType.commonTips, null, (node) => {
                                node.getComponent(CommonTips).init(`请前往挑战教训柳家豪`, () => {
                                    this.goTask()
                                })
                            })
                            // this.nanzhu.setAnimation(0, "男主下跪", false)
                            // this.nanzhu.setCompleteListener(() => {
                            //     this.nanzhu.setCompleteListener(null)
                            //     // PrefabManage.showTextTips("任务失败,因你跪下向柳鸿福认怂,得罪了协会会长,被驱逐出厨师协会")
                            //     this.nanzhu.setAnimation(0, "男主下跪待机", true)
                            //     PrefabManage.loadPrefabByType(GameType.taskState, this.node, (node) => {
                            //         node.getComponent(taskState).showFail("任务失败,因你跪下向柳鸿福认怂,得罪了协会会长,被驱逐出厨师协会", () => {
                            //             this.continueStory();
                            //         }, () => {
                            //             this.showQiPao(this.chatConfig[4], func, () => {
                            //                 this.nanzhu.setAnimation(0, "男主说话", true)
                            //             });
                            //         })
                            //     })
                            // })
                        },
                        () => {
                            cc.sys.localStorage.setItem("DH_20选择",'2')
                            this.showQiPao(this.chatConfig[4], func, () => {
                                this.nanzhu.setAnimation(0, "男主说话", true)
                            });
                        },
                    ]
                    let isvideo = [false, true]
                    node.getComponent(selectButtons).init(strs, actives, funcs, isvideo)

                    // node.getComponent(selectButton).init("认怂", "反击", () => {
                    //     this.nanzhu.setAnimation(0, "男主下跪", false)
                    //     this.nanzhu.setCompleteListener(() => {
                    //         this.nanzhu.setCompleteListener(null)
                    //         PrefabManage.showTextTips("任务失败,因你跪下向柳鸿福认怂,得罪了协会会长,被驱逐出厨师协会")
                    //         this.nanzhu.setAnimation(0, "男主下跪", true)
                    //         this.continueStory();
                    //     })
                    // }, () => {
                    //     this.showQiPao(this.chatConfig[4], func, () => {
                    //         this.nanzhu.setAnimation(0, "男主说话", true)
                    //     });
                    // })
                })
            },
            // (func) => {
            //     this.runBg(1, 2, this.MoveNodes.getChildByName("男主柳家豪中间"), () => {
            //         func()
            //     })
            // },
            (func) => {

                this.texie.node.active = true
                Common5.playRemoteAudioEffect('remotesound/effect/惨叫声')
                // this.nanzhu.node.active = false
                // this.liujiahao.timeScale = 4
                this.texie.setAnimation(0, "打人", false)
                this.texie.setCompleteListener(() => {
                    this.texie.setCompleteListener(null)
                    this.texie.node.active = false
                    this.nanzhu.setAnimation(0, "男主假摔待机", true)
                    // this.liujiahao.timeScale = 1
                    // this.liujiahao.setAnimation(0, "豪打男主被拦待机", true)
                    this.scheduleOnce(() => {
                        func()
                    }, 1)
                })
            },
            // (func) => {
            //     this.runBg(1, 1, this.node, () => {
            //         func()
            //     })
            // },
            (func) => {
                this.weihuizhang.node.active = true
                this.weihuizhang.setAnimation(0, "魏老入场", false)
                this.weihuizhang.setCompleteListener(() => {
                    this.weihuizhang.setCompleteListener(null)
                    this.weihuizhang.setAnimation(0, "魏老待机", true)
                    func()
                })
            },
            (func) => {
                this.showQiPao(this.chatConfig[5], func, () => {
                    this.weihuizhang.setAnimation(0, "魏老说话", true)
                });
            },
            // (func) => {
            //     this.weihuizhang.setAnimation(0, "魏老待机2", true)
            //     PrefabManage.loadPrefabByType(GameType.selectButtons, this.node, (node) => {

            //         let strs = ['隐瞒', '倒地']
            //         let actives = [true, true]
            //         let funcs = [
            //             () => {
            //                 this.showQiPao(this.chatConfig[6], () => {
            //                     PrefabManage.showTextTips("因认怂丢了魏会长的面子,被逐出师门并赶出协会。")
            //                     this.continueStory();
            //                 }, () => {
            //                     // this.weihuizhang.setAnimation(0, "魏老说话", true)
            //                 });
            //             },
            //             () => {
            //                 this.nanzhu.node.active = true
            //                 this.liujiahao.setAnimation(0, "豪待机", true)
            //                 this.nanzhu.setAnimation(0, "男主假摔", false)
            //                 Common5.playRemoteAudioEffect('remotesound/effect/惨叫声')
            //                 this.nanzhu.setCompleteListener(() => {
            //                     this.nanzhu.setCompleteListener(null)
            //                     this.nanzhu.setAnimation(0, "男主假摔待机", true)
            //                     func()
            //                 })
            //             },
            //         ]
            //         let isvideo = [false, false]
            //         node.getComponent(selectButtons).init(strs, actives, funcs, isvideo)

            //         // node.getComponent(selectButton).init("隐瞒", "倒地", () => {
            //         //     this.showQiPao(this.chatConfig[6], () => {
            //         //         PrefabManage.showTextTips("因认怂丢了魏会长的面子,被逐出师门并赶出协会。")
            //         //         this.continueStory();
            //         //     }, () => {
            //         //         // this.weihuizhang.setAnimation(0, "魏老说话", true)
            //         //     });
            //         // }, () => {
            //         //     this.nanzhu.node.active = true
            //         //     this.liujiahao.setAnimation(0, "豪待机", true)
            //         //     this.nanzhu.setAnimation(0, "男主假摔", false)
            //         //     this.nanzhu.setCompleteListener(() => {
            //         //         this.nanzhu.setCompleteListener(null)
            //         //         this.nanzhu.setAnimation(0, "男主假摔待机", true)
            //         //         func()
            //         //     })
            //         // })
            //     })
            // },
            (func) => {
                this.showQiPao(this.chatConfig[7], func, () => {
                    this.nanzhu.setAnimation(0, "男主假摔说话", true)
                });
            },
            (func) => {
                // this.nanzhu.setAnimation(0, "男主假摔待机", true)
                this.nanzhu.setAnimation(0, "男主", true)
                this.showQiPao(this.chatConfig[8], func, () => {
                    this.weihuizhang.setAnimation(0, "魏老说话2", true)
                });
            },
            (func) => {
                this.weihuizhang.setAnimation(0, "魏老待机2", true)
                this.showQiPao(this.chatConfig[9], func, () => {
                    this.liujiahao.setAnimation(0, "豪说话", true)
                });
            },
            (func) => {
                this.runBg(1, 2, this.MoveNodes.getChildByName("柳家豪挨打"), () => {
                    func()
                })
            },
            (func) => {
                Common5.playRemoteAudioEffect('remotesound/effect/连续耳光', true)
                this.liuhongfu.node.active = false
                this.liujiahao.setAnimation(0, "豪挨打", false)
                this.liujiahao.setCompleteListener(() => {
                    this.liujiahao.setCompleteListener(null)
                    cc.audioEngine.stopAllEffects()
                    this.liuhongfu.node.active = true
                    this.liujiahao.setAnimation(0, "豪挨打跪下待机", true)
                    this.runBg(1, 1, this.node, () => {
                        this.showQiPao(this.chatConfig[10], func, () => {
                            this.liuhongfu.setAnimation(0, "柳鸿福说话", true)
                        });
                    })
                })
            },
            (func) => {
                this.liuhongfu.setAnimation(0, "柳鸿福", true)
                this.showQiPao(this.chatConfig[11], func, () => {
                    // this.liujiahao.setAnimation(0, "豪说话", true)
                });
            },
            (func) => {
                // this.liujiahao.setAnimation(0, "豪待机", true)
                this.showQiPao(this.chatConfig[12], func, () => {
                    this.weihuizhang.setAnimation(0, "魏老说话2", true)
                });
            },
            (func) => {
                this.weihuizhang.setAnimation(0, "魏老待机", true)
                PrefabManage.loadPrefabByType(GameType.selectButtons, this.node, (node) => {

                    let strs = ['息事宁人', '求情', '都不选']
                    let actives = [true, true, false]
                    let funcs = [
                        () => {
                            this.showQiPao(this.chatConfig[13], () => {
                                PrefabManage.loadPrefabByType(GameType.taskState, this.node, (node) => {
                                    node.getComponent(taskState).showFail("因认怂丢了魏会长的面子,被逐出师门并赶出协会。", () => {
                                        this.continueStory();
                                    }, () => {
                                        PrefabManage.loadPrefabByType(GameType.commonTips, null, (node) => {
                                            node.getComponent(CommonTips).init(`请前往挑战教训柳家豪`, () => {
                                                func()
                                            })
                                        })
                                    })
                                })
                                // PrefabManage.showTextTips("因认怂丢了魏会长的面子,被逐出师门并赶出协会。")
                                // this.continueStory();
                            }, () => {
                                // this.weihuizhang.setAnimation(0, "魏老说话", true)
                            });
                        },
                        () => {
                            this.showQiPao(this.chatConfig[14], () => {
                                PrefabManage.loadPrefabByType(GameType.taskState, this.node, (node) => {
                                    node.getComponent(taskState).showFail("任务失败", () => {
                                        this.continueStory();
                                    }, () => {
                                        PrefabManage.loadPrefabByType(GameType.commonTips, null, (node) => {
                                            node.getComponent(CommonTips).init(`请前往挑战教训柳家豪`, () => {
                                                func()
                                            })
                                        })
                                    })
                                })
                                // PrefabManage.showTextTips("任务失败")
                                // this.continueStory();
                            }, () => {
                                // this.weihuizhang.setAnimation(0, "魏老说话", true)
                            });
                        },
                        () => {
                            PrefabManage.loadPrefabByType(GameType.commonTips, null, (node) => {
                                node.getComponent(CommonTips).init(`请前往挑战教训柳家豪`, () => {
                                    func()
                                })
                            })
                        }
                    ]
                    let isvideo = [false, false, false]
                    node.getComponent(selectButtons).init(strs, actives, funcs, isvideo)


                    // node.getComponent(selectButton).init("息事宁人", "求情", () => {
                    //     this.showQiPao(this.chatConfig[13], () => {
                    //         PrefabManage.showTextTips("因认怂丢了魏会长的面子,被逐出师门并赶出协会。")
                    //         this.continueStory();
                    //     }, () => {
                    //         // this.weihuizhang.setAnimation(0, "魏老说话", true)
                    //     });
                    // }, () => {
                    //     this.showQiPao(this.chatConfig[14], () => {
                    //         PrefabManage.showTextTips("任务失败")
                    //         this.continueStory();
                    //     }, () => {
                    //         // this.weihuizhang.setAnimation(0, "魏老说话", true)
                    //     });
                    // },
                    //     false, false, true,
                    //     () => {
                    //         PrefabManage.showTextTips("请前往挑战教训一顿柳公子。")
                    //         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_209")
                    //         // UserManager.clearAndaddStructureFlash("cf_FengRenYuan")
                    //     });
                    // });
                    // TaskManager.finishCurMainTask(MainTaskIdEnum.MainTask_202)
                    // TaskManager.setCurUnLockMainTaskInfo(MainTaskIdEnum.MainTask_203)
                    // this.node.destroy()
                }
            });
        }
        dialogCallFunc(dialogEvents);
    }

    goTask() {
        TaskManager.finishCurMainTask(MainTaskIdEnum.MainTask_520)
        TaskManager.setCurTask(MainTaskIdEnum.MainTask_521)
        PrefabManage.showBlackGuoDu(() => {
            this.node.removeFromParent()
            this.node.destroy()
            PrefabManage.loadPrefabByType(GameType.MainHall)
        })
    }

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