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_43')
export default class DH_43 extends DHBase {
    chatConfig = [
        { effectUrl: 'DH/DH_43/sound/恭贺厨神!', delayTime: 1.29, str: '恭贺厨神!', posi: 'qipao1' },
        { effectUrl: 'DH/DH_43/sound/恭贺厨神!', delayTime: 1.29, str: '恭贺厨神!', posi: 'qipao2' },
        { effectUrl: 'DH/DH_43/sound/恭贺厨神!', delayTime: 1.29, str: '恭贺厨神!', posi: 'qipao3' },
        { effectUrl: 'DH/DH_43/sound/是时候开始下一步计划了。', delayTime: 3.54, str: '是时候开始下一步计划了。', posi: 'qipao4' },
    ]

    @property(sp.Skeleton)
    nanzhu: sp.Skeleton = null
    @property(sp.Skeleton)
    spine: 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

    @property(cc.Node)
    haitan: cc.Node = null

    start() {
        Common5.preLoadRemoteAudioByChatConfig(this.chatConfig);
        super.start();
        this.continueStory();
        // this.scheduleOnce(() => {
        //     PrefabManage.preloadPrefabByType(GameType.DH_439)
        // })
    }

    // 继续剧情
    continueStory() {
        // this.scheduleOnce(() => {
        //     PrefabManage.preloadPrefabByType(GameType.DH_4)
        // })
        Common5.playRemoteAudioEffect("remotesound/effect/拍照-多个")
        this.haitan.active = false
        this.nanzhu.setAnimation(0, "待机", true)
        const dialogEvents = [
            (func) => {
                this.nanzhu.setAnimation(0, "向前", false)
                this.nanzhu.setCompleteListener(() => {
                    this.nanzhu.setCompleteListener(null)
                    this.nanzhu.setAnimation(0, "捧证书待机", true)
                    this.scheduleOnce(() => {
                        Common5.playRemoteAudioEffect("remotesound/effect/观众鼓掌声")
                    }, 0.2)
                    this.scheduleOnce(() => {
                        this.showQiPao(this.chatConfig[0], null, () => {
                        });
                    }, 0)
                    this.scheduleOnce(() => {
                        this.showQiPao(this.chatConfig[1], null, () => {
                        });
                    }, 0.1)
                    this.scheduleOnce(() => {
                        this.showQiPao(this.chatConfig[2], func, () => {
                        });
                    }, 0.15)
                })
            },
            (func) => {
                PrefabManage.showBlackGuoDu(() => {
                    this.haitan.active = true
                }, () => {
                    this.spine.setAnimation(0, "呼吸2", true)
                    this.scheduleOnce(() => {
                        this.texie.node.active = true
                        this.texie.setAnimation(0, "特写出现", false)
                        this.texie.setCompleteListener(() => {
                            this.texie.setCompleteListener(null)
                            this.showQiPao(this.chatConfig[3], func, () => {
                                this.texie.setAnimation(0, "特写说话", true)
                            });
                        })
                    }, 2)
                })
            },
            (func) => {
                this.texie.setAnimation(0, "特写持续", true)
                func()
            }
        ]
        const dialogCallFunc = (logEvents: any[]) => {
            logEvents.shift()(() => {
                if (logEvents.length > 0) {
                    dialogCallFunc(logEvents);
                } else {
                    let level = User.getChuShiXieHuiLevel()
                    level[7] = 1
                    User.setChuShiXieHuiLevel(level)
                    TaskManager.finishCurMainTask(MainTaskIdEnum.MainTask_555)
                    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_439")
                    //         // 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()
    }
}