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

@ccclass
@disallowMultiple()
@menu('对话剧情/DH_10')
export default class DH_10 extends DHBase {
    chatConfig = [
        { effectUrl: 'DH/DH_10/sound/三号有一个美食大赛?得想办法去参加这个大赛。', delayTime: 4.4, str: '三号有一个美食大赛?得想办法去参加这个大赛。', posi: '-1' },
        { effectUrl: 'DH/DH_10/sound/铜钱?我记得魏会长喜欢收集古钱币。', delayTime: 3.83, str: '铜钱?我记得魏会长喜欢收集古钱币。', posi: '-1' },
        { effectUrl: 'DH/DH_10/sound/燕子,这次我一定会搞清楚,你到底是因为什么原因这样对我。', delayTime: 4.46, str: '燕子,这次我一定会搞清楚,你到底是因为什么原因这样对我。', posi: '-1' },
        { effectUrl: 'DH/DH_10/sound/柳家,这一世,你们别再想拿到厨神称号了。', delayTime: 4.3, str: '柳家,这一世,你们别再想拿到厨神称号了。', posi: '-1' },
    ]

    @property(sp.Skeleton)
    nanzhu: 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_11)
        })
    }

    // // 继续剧情
    // continueStory() {
    //     this.scheduleOnce(() => {
    //         PrefabManage.preloadPrefabByType(GameType.DH_4)
    //     })
    //     this.nanzhu.setAnimation(0, "待机", true)
    //     const dialogEvents = [
    //         (func) => {
    //             this.runBg(0, 3, this.MoveNodes.getChildByName("男主"), () => {
    //                 func()
    //             })
    //         },
    //         (func) => {
    //             this.showQiPao(this.chatConfig[this.dialogIndex++], func, () => {
    //                 this.nanzhu.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_109")
    //                 //         // 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()
    }

    onTouchBaoZhi() {
        this.m_bg.getChildByName("放大报纸").active = false
    }

    onTouchZhaopian() {
        this.m_bg.getChildByName("照片大").active = false
    }

    onTouchChaDian(event) {
        let name = event.target.name
        let chadianIconArray = this.node.getChildByName("chadianIconArray")
        if (name == "click1") {
            Common5.playRemoteAudioEffect("remotesound/effect/游戏点击音效")
            chadianIconArray.children[0].getChildByName("icon").active = true
            this.showQiPao(this.chatConfig[0])
            this.m_bg.getChildByName("墙上报纸").active = false
            this.m_bg.getChildByName("放大报纸").active = true
            event.target.active = false
        } else if (name == "click2") {
            if (!this.m_bg.getChildByName("打开抽屉").active) {
                this.m_bg.getChildByName("打开抽屉").active = true
                return
            }
            Common5.playRemoteAudioEffect("remotesound/effect/游戏点击音效")
            chadianIconArray.children[1].getChildByName("icon").active = true
            this.showQiPao(this.chatConfig[1])
            this.m_bg.getChildByName("打开抽屉").getChildByName("抽屉钱").active = false
            event.target.active = false
        } else if (name == "click3") {
            Common5.playRemoteAudioEffect("remotesound/effect/游戏点击音效")
            chadianIconArray.children[2].getChildByName("icon").active = true
            this.showQiPao(this.chatConfig[2])
            this.m_bg.getChildByName("照片小").active = false
            this.m_bg.getChildByName("照片大").active = true
            event.target.active = false
        } else if (name == "click4") {
            // chadianIconArray.children[3].getChildByName("icon").active = true
            // this.showQiPao(this.chatConfig[3])
            // this.m_bg.getChildByName("牌匾 破旧").active = false
        }

        this.checkAllFind()
    }

    checkAllFind() {
        let chadianIconArray = this.node.getChildByName("chadianIconArray")
        let isFind = true
        for (let i = 0; i < chadianIconArray.children.length; i++) {
            if (!chadianIconArray.children[i].getChildByName("icon").active) {
                isFind = false
            }
        }
        if (isFind) {
            TaskManager.finishCurMainTask(MainTaskIdEnum.MainTask_508)
            TaskManager.setCurTask(MainTaskIdEnum.MainTask_509)
            this.scheduleOnce(() => {
                PrefabManage.showBlackGuoDu(() => {
                    this.node.removeFromParent()
                    this.node.destroy()
                    EventMgr.emitEvent_custom(ryw_Event.GuideTask)
                    PrefabManage.loadPrefabByType(GameType.MainHall)
                })
            }, 2)
        }
    }

    maskCallBack() {
        this.scheduleOnce(() => {
            Common5.playRemoteAudioEffect("remotesound/effect/游戏点击音效")
        }, 0.1)
        let chadianIconArray = this.node.getChildByName("chadianIconArray")
        chadianIconArray.children[3].getChildByName("icon").active = true
        this.showQiPao(this.chatConfig[3])
        this.m_bg.getChildByName("牌匾新").active = false
        this.checkAllFind()
    }
}