import { ryw_Event } from "../../FrameWork/Event/EventEnum";
import EventMgr from "../../FrameWork/Event/EventMgr";
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_7')
export default class DH_7 extends DHBase {
    chatConfig = [
        { effectUrl: 'DH/DH_7/sound/臭丫头,快开门,我知道你在里面。', delayTime: 5.0, str: '臭丫头,快开门,我知道你在里面。', posi: 'qipao2' },

        { effectUrl: 'DH/DH_7/sound/那就别怪我不客气了,只能将你爸送进去了。', delayTime: 3.98, str: '那就别怪我不客气了,只能将你爸送进去了。', posi: 'qipao3' },
        { effectUrl: 'DH/DH_7/sound/我求你了,再宽限我一段时间吧,我赚到钱就还你。', delayTime: 4.74, str: '我求你了,再宽限我一段时间吧,我赚到钱就还你。', posi: 'qipao7' },
        { effectUrl: 'DH/DH_7/sound/你做梦!乖乖跟我走吧,嘿嘿!', delayTime: 4.38, str: '你做梦!乖乖跟我走吧,嘿嘿!', posi: 'qipao6' },
        { effectUrl: 'DH/DH_7/sound/还钱可以,把欠条拿来,我给你钱。', delayTime: 2.57, str: '还钱可以,把欠条拿来,我给你钱。', posi: '-1' },
        { effectUrl: 'DH/DH_7/sound/50万可不够,她老子现在欠我500万,想救她?拿500万来。', delayTime: 6.88, str: '50万可不够,她老子现在欠我500万,想救她?拿500万来。', posi: 'qipao6' },
        { effectUrl: 'DH/DH_7/sound/就50万,爱要不要', delayTime: 1.61, str: '就50万,爱要不要', posi: '-1' },
    ]

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

    @property(sp.Skeleton)
    spine: sp.Skeleton = null

    @property(sp.Skeleton)
    diuqian: sp.Skeleton = null

    @property(sp.Skeleton)
    suyao_longge: 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.nanzhu.node.active = false
        this.spine.node.active = true
        this.suyao_longge.node.active = false
        this.spine.setAnimation(0, "养父龙哥待机", true)
        this.continueStory();
        // this.scheduleOnce(() => {
        //     PrefabManage.preloadPrefabByType(GameType.DH_79)
        // })
        EventMgr.onEvent_custom(ryw_Event.NormalTouchMoveCheck, (data_) => {
            let node = this.m_bg.getChildByName("moneyNode")
            let jt = this.m_bg.getChildByName("手指箭头")
            node.active = false
            jt.active = false
            this.diuqian.node.active = true
            Common5.playRemoteAudioEffect('sound/common/钱币入账')
            this.diuqian.setAnimation(0, "丢钱", false)
            this.diuqian.setCompleteListener(() => {
                this.diuqian.setCompleteListener(null)
                this.m_bg.getChildByName("门框").active = true
                this.spine.setAnimation(0, "女主开门被抓", false)
                this.spine.setCompleteListener(() => {
                    this.spine.setCompleteListener(null)
                    this.spine.node.active = false
                    this.suyao_longge.node.active = true
                    this.suyao_longge.setAnimation(0, "女主被绑待机", true)

                    this.scheduleOnce(() => {
                        Common5.playRemoteAudioEffect("remotesound/effect/紧张咚咚声")
                    }, 0.01)
                    this.showQiPao(this.chatConfig[5], () => {
                        this.suyao_longge.setAnimation(0, "女主被绑待机", true)
                        PrefabManage.loadPrefabByType(GameType.selectButtons, this.node, (node) => {

                            let strs = ['无视', '凑钱']
                            let actives = [true, true]
                            let funcs = [
                                () => {
                                    this.suyao_longge.setAnimation(0, "女主被绑待机", true)
                                    this.showQiPao(this.chatConfig[6], () => {
                                        let funcs = [
                                            () => {
                                                TaskManager.finishCurMainTask(MainTaskIdEnum.MainTask_504)
                                                TaskManager.setCurTask(MainTaskIdEnum.MainTask_505)
                                                UserManager.subMoney(500000)
                                                PrefabManage.showBlackGuoDu(() => {
                                                    this.node.removeFromParent()
                                                    this.node.destroy()
                                                    PrefabManage.loadPrefabByType(GameType.MainHall)
                                                })
                                            },
                                            () => {
                                                this.start()
                                            },
                                        ]
                                        PrefabManage.loadPrefabByType(GameType.taskState, this.node, (node) => {
                                            node.getComponent(taskState).showFail("", funcs[1], funcs[0])
                                        })
                                    }, () => {
                                        // this.spine.setAnimation(0, "男主丢钱说话", true)
                                    });
                                },
                                () => {
                                    PrefabManage.showTextTips("再去凑500万")
                                    TaskManager.finishCurMainTask(MainTaskIdEnum.MainTask_504)
                                    TaskManager.setCurTask(MainTaskIdEnum.MainTask_505)
                                    UserManager.subMoney(500000)
                                    PrefabManage.showBlackGuoDu(() => {
                                        this.node.removeFromParent()
                                        this.node.destroy()
                                        PrefabManage.loadPrefabByType(GameType.MainHall)
                                    })
                                },
                            ]
                            let isvideo = [false, false]
                            node.getComponent(selectButtons).init(strs, actives, funcs, isvideo)
                        })
                    }, () => {
                        this.suyao_longge.setAnimation(0, "女主被绑龙哥说话", true)
                        // this.spine.setAnimation(0, "女主被抓龙哥说话", true)
                    })
                })
            })

        }, this);
        // EventMgr.onEvent_custom(ryw_Event.NeedGoodFinish, () => {
        //     let node = this.m_bg.getChildByName("needGoodNode")
        //     node.active = false
        //     this.diuqian.node.active = true
        //     this.diuqian.setAnimation(0, "丢钱", false)
        //     this.diuqian.setCompleteListener(() => {
        //         this.diuqian.setCompleteListener(null)
        //         this.m_bg.getChildByName("门框").active = true
        //         this.spine.setAnimation(0, "女主被抓待机", true)
        //         this.showQiPao(this.chatConfig[5], () => {
        //             this.spine.setAnimation(0, "女主被抓待机", true)
        //             PrefabManage.loadPrefabByType(GameType.selectButtons, this.node, (node) => {

        //                 let strs = ['无视', '凑钱']
        //                 let actives = [true, true]
        //                 let funcs = [
        //                     () => {
        //                         this.spine.setAnimation(0, "女主被抓待机", true)
        //                         this.showQiPao(this.chatConfig[6], () => {
        //                             let funcs = [
        //                                 () => {
        //                                     TaskManager.finishCurMainTask(MainTaskIdEnum.MainTask_504)
        //                                     TaskManager.setCurTask(MainTaskIdEnum.MainTask_505)
        //                                     UserManager.subMoney(500000)
        //                                     PrefabManage.showBlackGuoDu(() => {
        //                                         this.node.removeFromParent()
        //                                         this.node.destroy()
        //                                     })
        //                                 },
        //                                 () => {
        //                                     this.start()
        //                                 },
        //                             ]
        //                             PrefabManage.loadPrefabByType(GameType.taskState, this.node, (node) => {
        //                                 node.getComponent(taskState).showFail("", funcs[1], funcs[0])
        //                             })
        //                             // PrefabManage.loadPrefabByType(GameType.selectButtons, this.node, (node) => {

        //                             //     let strs = ['直接通关', '重新开始']
        //                             //     let actives = [true, true]
        //                             //     let funcs = [
        //                             //         () => {
        //                             //             TaskManager.finishCurMainTask(MainTaskIdEnum.MainTask_504)
        //                             //             TaskManager.setCurTask(MainTaskIdEnum.MainTask_505)
        //                             //             UserManager.subMoney(500000)
        //                             //             PrefabManage.showBlackGuoDu(() => {
        //                             //                 this.node.removeFromParent()
        //                             //                 this.node.destroy()
        //                             //             })
        //                             //         },
        //                             //         () => {
        //                             //             this.start()
        //                             //         },
        //                             //     ]
        //                             //     let isvideo = [true, false]
        //                             //     node.getComponent(selectButtons).init(strs, actives, funcs, isvideo)
        //                             // })
        //                         }, () => {
        //                             // this.spine.setAnimation(0, "男主丢钱说话", true)
        //                         });
        //                     },
        //                     () => {
        //                         PrefabManage.showTextTips("再去凑500万")
        //                         TaskManager.finishCurMainTask(MainTaskIdEnum.MainTask_504)
        //                         TaskManager.setCurTask(MainTaskIdEnum.MainTask_505)
        //                         UserManager.subMoney(500000)
        //                         PrefabManage.showBlackGuoDu(() => {
        //                             this.node.removeFromParent()
        //                             this.node.destroy()
        //                         })
        //                     },
        //                 ]
        //                 let isvideo = [false, false]
        //                 node.getComponent(selectButtons).init(strs, actives, funcs, isvideo)

        //                 // node.getComponent(selectButton).init("无视", "凑钱", () => {
        //                 //     this.spine.setAnimation(0, "女主被绑待机", true)
        //                 //     this.showQiPao(this.chatConfig[6], () => {
        //                 //         PrefabManage.loadPrefabByType(GameType.selectButton, this.node, (node) => {
        //                 //             node.getComponent(selectButton).init("直接通关", "重新开始", () => {
        //                 //                 TaskManager.finishCurMainTask(MainTaskIdEnum.MainTask_504)
        //                 //                 TaskManager.setCurTask(MainTaskIdEnum.MainTask_505)
        //                 //                 UserManager.subMoney(500000)
        //                 //                 PrefabManage.showBlackGuoDu(() => {
        //                 //                     this.node.removeFromParent()
        //                 //                     this.node.destroy()
        //                 //                 })
        //                 //             }, () => {
        //                 //                 this.start()
        //                 //             }, true)
        //                 //         })
        //                 //     }, () => {
        //                 //         this.spine.setAnimation(0, "男主丢钱说话", true)
        //                 //     });
        //                 // }, () => {
        //                 //     PrefabManage.showTextTips("再去凑500万")
        //                 //     TaskManager.finishCurMainTask(MainTaskIdEnum.MainTask_504)
        //                 //     TaskManager.setCurTask(MainTaskIdEnum.MainTask_505)
        //                 //     UserManager.subMoney(500000)
        //                 //     PrefabManage.showBlackGuoDu(() => {
        //                 //         this.node.removeFromParent()
        //                 //         this.node.destroy()
        //                 //     })

        //                 // })
        //             })
        //         }, () => {
        //             this.spine.setAnimation(0, "女主被抓龙哥说话", true)
        //         })
        //     })

        // }, this)
    }

    // 继续剧情
    continueStory() {
        this.scheduleOnce(() => {
            PrefabManage.preloadPrefabByType(GameType.DH_8)
        })
        let node = this.m_bg.getChildByName("moneyNode")
        node.getChildByName("money").active = true
        this.diuqian.node.active = false
        this.m_bg.getChildByName("门框").active = false
        const dialogEvents = [
            (func) => {
                Common5.playRemoteAudioEffect("remotesound/effect/敲门")
                this.spine.setAnimation(0, "养父龙哥撞门", false)
                this.spine.setCompleteListener(() => {
                    this.spine.setCompleteListener(null)
                    func()
                })
            },
            (func) => {
                this.showQiPao(this.chatConfig[0], 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 = [
                        () => {
                            this.spine.setAnimation(0, "女主开门被抓", false)
                            this.scheduleOnce(() => {
                                Common5.playRemoteAudioEffect("remotesound/effect/开门声")
                            }, 0.8)
                            this.spine.setCompleteListener(() => {
                                this.spine.setCompleteListener(null)
                                // PrefabManage.showTextTips("任务失败,重新开始")
                                PrefabManage.loadPrefabByType(GameType.taskState, this.node, (node) => {
                                    node.getComponent(taskState).showFail("任务失败,重新开始", () => {
                                        this.start()
                                    }, () => {
                                        this.spine.setAnimation(0, "养父龙哥待机", true)
                                        this.showQiPao(this.chatConfig[1], func, () => {
                                            // this.spine.setAnimation(0, "养父说话", true)
                                        });
                                    })
                                })

                            })
                        },
                        () => {
                            this.showQiPao(this.chatConfig[1], func, () => {
                                // this.spine.setAnimation(0, "养父说话", true)
                            });
                        },
                    ]
                    let isvideo = [false, false]
                    node.getComponent(selectButtons).init(strs, actives, funcs, isvideo)



                    // node.getComponent(selectButtons).init("开门", "不开门", () => {
                    //     this.spine.setAnimation(0, "女主开门被抓", false)
                    //     this.spine.setCompleteListener(() => {
                    //         this.spine.setCompleteListener(null)
                    //         PrefabManage.showTextTips("任务失败,重新开始")
                    //         this.start()
                    //     })
                    // }, () => {
                    //     this.showQiPao(this.chatConfig[1], func, () => {
                    //         // this.spine.setAnimation(0, "养父说话", true)
                    //     });
                    // })
                })
            },
            (func) => {
                PrefabManage.loadPrefabByType(GameType.selectButtons, this.node, (node) => {

                    let strs = ['让苏瑶自己处理', '帮苏瑶还钱']
                    let actives = [true, true]
                    let funcs = [
                        () => {
                            this.spine.setAnimation(0, "女主开门被抓", false)
                            this.spine.setCompleteListener(() => {
                                this.spine.setCompleteListener(null)
                                this.spine.setAnimation(0, "女主被抓待机", true)
                                this.showQiPao(this.chatConfig[2], () => {
                                    this.showQiPao(this.chatConfig[3], () => {
                                        this.m_bg.getChildByName("门框").active = true
                                        this.spine.node.active = false
                                        this.suyao_longge.node.active = true
                                        this.suyao_longge.setAnimation(0, "女主被绑待机", true)
                                        let config = TaskManager.getTaskConfigById(MainTaskIdEnum.MainTask_505)
                                        PrefabManage.loadPrefabByType(GameType.commonTips, null, (node) => {
                                            node.getComponent(CommonTips).init(`苏瑶被龙哥绑走,救苏瑶需要${Common5.getNumberChangeHanzi(config.taskNum)}赎金`, () => {
                                                TaskManager.finishCurMainTask(MainTaskIdEnum.MainTask_504)
                                                TaskManager.setCurTask(MainTaskIdEnum.MainTask_505)
                                                UserManager.subMoney(500000)
                                                PrefabManage.showBlackGuoDu(() => {
                                                    this.node.removeFromParent()
                                                    this.node.destroy()
                                                    PrefabManage.loadPrefabByType(GameType.MainHall)
                                                })
                                            },'前往凑钱')
                                        })
                                        // PrefabManage.loadPrefabByType(GameType.taskState, this.node, (node) => {
                                        //     node.getComponent(taskState).showFail("", () => {
                                        //         this.start()
                                        //     }, () => {
                                        //         TaskManager.finishCurMainTask(MainTaskIdEnum.MainTask_504)
                                        //         TaskManager.setCurTask(MainTaskIdEnum.MainTask_505)
                                        //         UserManager.subMoney(500000)
                                        //         PrefabManage.showBlackGuoDu(() => {
                                        //             this.node.removeFromParent()
                                        //             this.node.destroy()
                                        //             PrefabManage.loadPrefabByType(GameType.MainHall)
                                        //         })
                                        //     })
                                        // })
                                    }, () => {
                                        this.spine.setAnimation(0, "女主被抓龙哥说话", true)
                                    })
                                }, () => {
                                    this.spine.setAnimation(0, "女主被抓说话", true)
                                });
                            })
                        },
                        () => {
                            // this.nanzhu.node.active = true
                            // this.nanzhu.setAnimation(0, "男主入场", false)
                            // this.nanzhu.setCompleteListener(() => {
                            //     this.nanzhu.setCompleteListener(null)
                            //     this.nanzhu.node.active = false
                            //     this.showQiPao(this.chatConfig[4], () => {
                            //         this.spine.setAnimation(0, "男主丢钱待机", true)
                            let node = this.m_bg.getChildByName("moneyNode")
                            let jt = this.m_bg.getChildByName("手指箭头")
                            node.active = true
                            jt.active = true
                            // this.guideView([node])
                            //     }, () => {
                            //         this.spine.setAnimation(0, "男主丢钱前说话", true)
                            //     });
                            // })
                        },
                    ]
                    let isvideo = [false, true]
                    node.getComponent(selectButtons).init(strs, actives, funcs, isvideo)

                    // node.getComponent(selectButton).init("让苏瑶自己处理", "帮苏瑶还钱", () => {
                    //     this.spine.setAnimation(0, "女主开门被抓", false)
                    //     this.spine.setCompleteListener(() => {
                    //         this.spine.setCompleteListener(null)
                    //         this.spine.setAnimation(0, "女主被抓待机", true)
                    //         this.showQiPao(this.chatConfig[2], () => {
                    //             this.showQiPao(this.chatConfig[3], () => {
                    //                 PrefabManage.loadPrefabByType(GameType.selectButton, this.node, (node) => {
                    //                     node.getComponent(selectButton).init("直接通关", "重新开始", () => {
                    //                         TaskManager.finishCurMainTask(MainTaskIdEnum.MainTask_504)
                    //                         TaskManager.setCurTask(MainTaskIdEnum.MainTask_505)
                    //                         UserManager.subMoney(500000)
                    //                         PrefabManage.showBlackGuoDu(() => {
                    //                             this.node.removeFromParent()
                    //                             this.node.destroy()
                    //                         })
                    //                     }, () => {
                    //                         this.start()
                    //                     }, true)
                    //                 })
                    //             }, () => {
                    //                 this.spine.setAnimation(0, "女主被抓龙哥说话", true)
                    //             })
                    //         }, () => {
                    //             this.spine.setAnimation(0, "女主被抓说话", true)
                    //         });
                    //     })
                    // }, () => {
                    //     this.nanzhu.node.active = true
                    //     this.nanzhu.setAnimation(0, "男主入场", false)
                    //     this.nanzhu.setCompleteListener(() => {
                    //         this.nanzhu.setCompleteListener(null)
                    //         this.nanzhu.node.active = false
                    //         this.showQiPao(this.chatConfig[4], () => {
                    //             this.spine.setAnimation(0, "男主丢钱待机", true)
                    //             let node = this.m_bg.getChildByName("needGoodNode")
                    //             node.active = true
                    //             this.guideView([node])
                    //         }, () => {
                    //             this.spine.setAnimation(0, "男主丢钱前说话", true)
                    //         });
                    //     })

                    // })
                })
            },
        ]
        const dialogCallFunc = (logEvents: any[]) => {
            logEvents.shift()(() => {
                if (logEvents.length > 0) {
                    dialogCallFunc(logEvents);
                } else {
                    // 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_79")
                    //         // 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()
    }
}