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

@ccclass
@disallowMultiple()
@menu('对话剧情/DH_17')
export default class DH_17 extends DHBase {
    chatConfig = [
        { effectUrl: 'DH/DH_17/sound/汉升哥哥,没想到开店会遇到这么多麻烦。', delayTime: 3.38, str: '汉升哥哥,没想到开店会遇到这么多麻烦。', posi: 'qipao2' },
        { effectUrl: 'DH/DH_17/sound/嗯,好在我们都挺过来了。能过上好日子了。', delayTime: 3.67, str: '嗯,好在我们都挺过来了。能过上好日子了。', posi: '-1' },
        { effectUrl: 'DH/DH_17/sound/这房子买来还没装修呢,重新装修一下好不好?', delayTime: 4.35, str: '这房子买来还没装修呢,重新装修一下好不好?', posi: 'qipao2' },
        { effectUrl: 'DH/DH_17/sound/嗯,可以,赚了钱,该享受得享受。', delayTime: 3.57, str: '嗯,可以,赚了钱,该享受得享受。', posi: '-1' },
        { effectUrl: 'DH/DH_17/sound/好期待啊,那我们赶紧赚钱去吧。', delayTime: 2.47, str: '好期待啊,那我们赶紧赚钱去吧。', posi: 'qipao2' },
    ]

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

    // 继续剧情
    continueStory() {
        this.scheduleOnce(() => {
            PrefabManage.preloadPrefabByType(GameType.DH_18)
        })
        this.nanzhu.setAnimation(0, "男主待机", true)
        this.nvzhu.setAnimation(0, "女主待机", true)
        const dialogEvents = [
            (func) => {
                this.showQiPao(this.chatConfig[this.dialogIndex++], func, () => {
                    this.nvzhu.setAnimation(0, "女主说话", true)
                });
            },
            (func) => {
                this.nvzhu.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.nvzhu.setAnimation(0, "女主说话", true)
                });
            },
            (func) => {
                this.nvzhu.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.nvzhu.setAnimation(0, "女主说话", true)
                });
            },
        ]
        const dialogCallFunc = (logEvents: any[]) => {
            logEvents.shift()(() => {
                if (logEvents.length > 0) {
                    dialogCallFunc(logEvents);
                } else {
                    this.nvzhu.setAnimation(0, "女主待机", true)
                    TaskManager.finishCurMainTask(MainTaskIdEnum.MainTask_518_1)
                    TaskManager.setCurTask(MainTaskIdEnum.MainTask_518_2)
                    PrefabManage.showBlackGuoDu(() => {
                        this.node.removeFromParent()
                        this.node.destroy()
                        PrefabManage.loadPrefabByType(GameType.MainHall)
                    })
                    // 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_179")
                    //         // 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()
    }
}