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

@ccclass
@disallowMultiple()
@menu('对话剧情/DH_9')
export default class DH_9 extends DHBase {
    chatConfig = [
        { effectUrl: 'DH/DH_9/sound/汉升哥哥,谢谢你,我一定会想办法报答你的。', delayTime: 4.12, str: '汉升哥哥,谢谢你,我一定会想办法报答你的。', posi: 'qipao2' },
        { effectUrl: 'DH/DH_9/sound/好呀,那我等着你的报答哦。', delayTime: 2.34, str: '好呀,那我等着你的报答哦。', posi: '-1' },
        { effectUrl: 'DH/DH_9/sound/汉升哥哥,我这衣服好看吗?', delayTime: 2.91, str: '汉升哥哥,我这衣服好看吗?', posi: 'qipao2' },
    ]

    @property(sp.Skeleton)
    nanzhu: sp.Skeleton = null
    @property(sp.Skeleton)
    suyao: sp.Skeleton = null
    @property(sp.Skeleton)
    suyao_xiaofu: 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_99)
        // })
    }

    // 继续剧情
    continueStory() {
        this.scheduleOnce(() => {
            PrefabManage.preloadPrefabByType(GameType.DH_10)
        })
        this.nanzhu.setAnimation(0, "待机", true)
        this.suyao.setAnimation(0, "待机", true)
        const dialogEvents = [
            (func) => {
                this.showQiPao(this.chatConfig[this.dialogIndex++], func, () => {
                    this.suyao.setAnimation(0, "说话", true)
                });
            },
            (func) => {
                this.suyao.setAnimation(0, "待机", true)
                this.showClickDialog(this.chatConfig[this.dialogIndex++], func, () => {
                    this.nanzhu.setAnimation(0, "说话", true)
                });
            },
            (func) => {
                this.nanzhu.setAnimation(0, "待机", true)
                PrefabManage.loadPrefabByType(GameType.selectButtons, this.node, (node) => {

                    let strs = ['苏瑶给你准备了惊喜', '不用了,帮助苏瑶是应该的']
                    let actives = [true, true]
                    let funcs = [
                        () => {
                            Common5.playRemoteAudioEffect("remotesound/effect/暧昧-10秒")
                            this.runBg(1, 2, this.MoveNodes.getChildByName("女主"), () => {
                                this.suyao.node.active = false
                                this.suyao_xiaofu.node.active = true
                                this.showQiPao(this.chatConfig[this.dialogIndex++], () => {
                                    TaskManager.finishCurMainTask(MainTaskIdEnum.MainTask_507)
                                    TaskManager.setCurTask(MainTaskIdEnum.MainTask_508)
                                    PrefabManage.showBlackGuoDu(() => {
                                        PrefabManage.loadPrefabByType(GameType.DH_10)
                                        this.node.removeFromParent()
                                        this.node.destroy()
                                    })
                                }, () => {
                                    // this.nanzhu.setAnimation(0, "说话", true)
                                });
                            })
                        },
                        () => {
                            TaskManager.finishCurMainTask(MainTaskIdEnum.MainTask_507)
                            TaskManager.setCurTask(MainTaskIdEnum.MainTask_508)
                            PrefabManage.showBlackGuoDu(() => {
                                PrefabManage.loadPrefabByType(GameType.DH_10)
                                this.node.removeFromParent()
                                this.node.destroy()
                            })
                        },
                    ]
                    let isvideo = [true, false]
                    node.getComponent(selectButtons).init(strs, actives, funcs, isvideo)

                    // node.getComponent(selectButton).init("苏瑶给你准备了惊喜", "不用了,帮助苏瑶是应该的", () => {
                    //     TaskManager.finishCurMainTask(MainTaskIdEnum.MainTask_507)
                    //     TaskManager.setCurTask(MainTaskIdEnum.MainTask_508)
                    //     PrefabManage.showBlackGuoDu(() => {
                    //         PrefabManage.loadPrefabByType(GameType.DH_10)
                    //         this.node.removeFromParent()
                    //         this.node.destroy()
                    //     })
                    // }, () => {
                    //     TaskManager.finishCurMainTask(MainTaskIdEnum.MainTask_507)
                    //     TaskManager.setCurTask(MainTaskIdEnum.MainTask_508)
                    //     PrefabManage.showBlackGuoDu(() => {
                    //         PrefabManage.loadPrefabByType(GameType.DH_10)
                    //         this.node.removeFromParent()
                    //         this.node.destroy()
                    //     })
                    // })
                })
            },
        ]
        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_99")
                    //         // 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()
    }
}