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_40')
export default class DH_40 extends DHBase {
    chatConfig = [
        { effectUrl: 'DH/DH_40/sound/恭喜陈会长,陈会长真是年轻有为啊。', delayTime: 3.75, str: '恭喜陈会长,陈会长真是年轻有为啊。', posi: 'qipao3' },
        { effectUrl: 'DH/DH_40/sound/哪里哪里,贵公子才是年轻有为啊,相信要不了多久就能到特一级了。', delayTime: 6.28, str: '哪里哪里,贵公子才是年轻有为啊,相信要不了多久就能到特一级了。', posi: '-1' },
        { effectUrl: 'DH/DH_40/sound/不能比,不能比,犬子我了解,他没那个天赋。', delayTime: 6.67, str: '不能比,不能比,犬子我了解,他没那个天赋。', posi: 'qipao3' },
        { effectUrl: 'DH/DH_40/sound/是没那个天赋!但是手段多啊,说不定还能夺得厨神呢?', delayTime: 4.61, str: '是没那个天赋!但是手段多啊,说不定还能夺得厨神呢?', posi: '-1' },
        { effectUrl: 'DH/DH_40/sound/陈会长,我替犬子向您道歉,希望您大人大量。', delayTime: 5.89, str: '陈会长,我替犬子向您道歉,希望您大人大量。', posi: 'qipao3' },
        { effectUrl: 'DH/DH_40/sound/(真是个老狐狸,这会还能拉下脸赔罪了)', delayTime: 3.98, str: '(真是个老狐狸,这会还能拉下脸赔罪了)', posi: '-1' },
        { effectUrl: 'DH/DH_40/sound/哦?让他在这自己扇自己认错给我道歉吧', delayTime: 3.46, str: '哦?让他在这自己扇自己认错给我道歉吧', posi: '-1' },
        { effectUrl: 'DH/DH_40/sound/陈会长,我已经知道错了,请您原谅!', delayTime: 4.04, str: '陈会长,我已经知道错了,请您原谅!', posi: 'qipao2' },
        { effectUrl: 'DH/DH_40/sound/(看来这柳家豪心生恶念了,不能再等了,得找证据将他们都送进去)', delayTime: 6.28, 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)
    liujiahao_texie: sp.Skeleton = null
    @property(sp.Skeleton)
    liuhongfu: sp.Skeleton = null

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

    start() {
        Common5.playRemoteAudioMusic('remotesound/effect/颁奖-10秒', false)
        Common5.preLoadRemoteAudioByChatConfig(this.chatConfig);
        super.start();
        this.continueStory();
        // this.scheduleOnce(() => {
        //     PrefabManage.preloadPrefabByType(GameType.DH_409)
        // })
    }

    // 继续剧情
    continueStory() {
        this.scheduleOnce(() => {
            PrefabManage.preloadPrefabByType(GameType.DH_41)
        })
        this.nanzhu.setAnimation(0, "男主待机", true)
        this.weihuizhang.setAnimation(0, "会长颁奖", true)
        this.liujiahao.node.active = false
        this.liuhongfu.node.active = false
        this.liujiahao_texie.node.active = false
        const dialogEvents = [
            // (func) => {
            //     this.runBg(0, 3, this.MoveNodes.getChildByName("男主"), () => {
            //         func()
            //     })
            // },
            (func) => {
                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)
                this.runBg(0, 1, this.MoveNodes, () => {
                    func()
                })
            },
            (func) => {
                this.showQiPao(this.chatConfig[0], func, () => {
                    this.liuhongfu.setAnimation(0, "柳洪福说话", true)
                });
            },
            (func) => {
                this.liuhongfu.setAnimation(0, "柳洪福", true)
                this.showClickDialog(this.chatConfig[1], func, () => {
                    this.nanzhu.setAnimation(0, "男主说话", true)
                });
            },
            (func) => {
                this.nanzhu.setAnimation(0, "男主待机", true)
                this.showQiPao(this.chatConfig[2], func, () => {
                    this.liuhongfu.setAnimation(0, "柳洪福说话", true)
                });
            },
            (func) => {
                this.liuhongfu.setAnimation(0, "柳洪福", true)
                this.showClickDialog(this.chatConfig[3], func, () => {
                    this.nanzhu.setAnimation(0, "男主说话", true)
                });
            },
            (func) => {
                this.nanzhu.setAnimation(0, "男主待机", true)
                this.showQiPao(this.chatConfig[4], func, () => {
                    this.liuhongfu.setAnimation(0, "柳洪福说话", true)
                });
            },
            (func) => {
                this.liuhongfu.setAnimation(0, "柳洪福", true)
                this.showClickDialog(this.chatConfig[5], func, () => {
                    // this.nanzhu.setAnimation(0, "说话", true)
                });
            },
            (func) => {
                PrefabManage.loadPrefabByType(GameType.selectButtons, this.node, (node) => {
                    let strs = ['直接打脸', '息事宁人']
                    let actives = [true, true]
                    let funcs = [
                        () => {
                            this.showQiPao(this.chatConfig[6], () => {
                                func()
                            }, () => {
                                this.nanzhu.setAnimation(0, "男主说话", true)
                            });
                        },
                        () => {
                            PrefabManage.loadPrefabByType(GameType.taskState, this.node, (node) => {
                                node.getComponent(taskState).showFail("都已经当会长了,还这么怂,还当什么会长,回家卖红薯吧。", () => {
                                    this.continueStory()
                                }, () => {
                                    this.showQiPao(this.chatConfig[6], () => {
                                        func()
                                    }, () => {
                                        this.nanzhu.setAnimation(0, "男主说话", true)
                                    });
                                })
                            })
                            // PrefabManage.showTextTips("都已经当会长了,还这么怂,还当什么会长,回家卖红薯吧。")
                            // this.continueStory()
                        },
                    ]
                    let isvideo = [false, false]
                    node.getComponent(selectButtons).init(strs, actives, funcs, isvideo)
                })
            },
            (func) => {
                Common5.playRemoteAudioEffect('remotesound/effect/打耳光')
                this.liujiahao.setAnimation(0, "柳加豪扇耳光", false)
                this.liujiahao.setCompleteListener(() => {
                    this.liujiahao.setCompleteListener(null)
                    this.liujiahao.setAnimation(0, "柳加豪鞠躬待机", true)
                    this.liujiahao_texie.node.active = true
                    this.liujiahao_texie.setAnimation(0, "柳加豪特写", false)
                    this.liujiahao_texie.setCompleteListener(() => {
                        this.liujiahao_texie.setCompleteListener(null)
                        // this.liujiahao_texie.setAnimation(0, "柳加豪特写持续", true)
                        this.liujiahao_texie.node.active = false
                        this.showQiPao(this.chatConfig[7], func, () => {
                        });
                    })
                })
            },
            (func) => {
                this.showClickDialog(this.chatConfig[8], func, () => {
                    // this.nanzhu.setAnimation(0, "说话", true)
                });
            },
        ]
        const dialogCallFunc = (logEvents: any[]) => {
            logEvents.shift()(() => {
                if (logEvents.length > 0) {
                    dialogCallFunc(logEvents);
                } else {
                    let ChuShiXieHuiLevel = User.getChuShiXieHuiLevel()
                    ChuShiXieHuiLevel[6] = 1
                    User.setChuShiXieHuiLevel(ChuShiXieHuiLevel)
                    TaskManager.finishCurMainTask(MainTaskIdEnum.MainTask_552)
                    TaskManager.setCurTask(MainTaskIdEnum.MainTask_553)
                    PrefabManage.showBlackGuoDu(() => {
                        PrefabManage.loadPrefabByType(GameType.DH_41)
                        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_409")
                    //         // 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()
    }
}