// Learn TypeScript:
//  - https://docs.cocos.com/creator/2.4/manual/en/scripting/typescript.html
// Learn Attribute:
//  - https://docs.cocos.com/creator/2.4/manual/en/scripting/reference/attributes.html
// Learn life-cycle callbacks:
//  - https://docs.cocos.com/creator/2.4/manual/en/scripting/life-cycle-callbacks.html

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 ChatManager from "../JuQingChat/ChatManager";
import JuQingManager from "../JuQingChat/JuQingManager";
import TaskManager, { MainTaskIdEnum } from "../JuQingChat/TaskManager";
import LevelUpManager from "../Manager/LevelUpManager";
import MiShuManager from "../Manager/MiShuManager";
import ShipuManager from "../Manager/ShipuManager";
import TanWeiManager from "../Manager/TanWeiManager";
import ZiChanManager from "../Manager/ZiChanManager";
import NewGuideScript from "../NewGuide/NewGuideScript";
import PrefabManage, { GameType } from "../PrefabManager/PrefabManage";

const { ccclass, property } = cc._decorator;

@ccclass
export default class MyRoom extends cc.Component {

    @property(cc.Node)
    uiNode: cc.Node = null;

    @property(cc.Node)
    chumenNode: cc.Node = null;

    @property(cc.Sprite)
    chenghaoPic: cc.Sprite = null;
    @property(cc.Label)
    levelLab: cc.Label = null;
    @property(cc.Label)
    clickMoneyLab: cc.Label = null;

    // onLoad () {}
    @property(cc.Label)
    secondMoneyLab: cc.Label = null;
    // LIFE-CYCLE CALLBACKS:

    maskGuideNode: cc.Node = null;
    // onLoad () {}


    start() {
        Common5.playRemoteAudioMusic('sound/JuQingBG/房间背景音乐')

        return

        EventMgr.emitEvent_custom(ryw_Event.changeScene);
        EventMgr.onEvent_custom(ryw_Event.changeScene, () => {
            console.log('changeScene++=MyRoom')
            this.node.removeFromParent()
            this.node.destroy()
        }, this)

        this.createRoom()
        this.guideStep()
        this.roomStateChange()
        this.showChatBtnStatus()
        EventMgr.onEvent_custom(ryw_Event.RefreshJuQingDuiHua, () => {
            this.roomStateChange()
            this.guideStep()
            this.showChatBtnStatus()
        }, this)

        this.refreshHead()
        EventMgr.onEvent_custom(ryw_Event.setHeadImg, (img) => {
            this.refreshHead()
        }, this)

        this.scheduleOnce(() => {
            let array = [GameType.JuQingDialog, GameType.JuQingDialog2, GameType.MainHall, GameType.ChaoFanWang, GameType.ChaoFanDesk, GameType.PeopleLevelUp, GameType.MishuGame, GameType.ChallengeChoose]
            PrefabManage.preloadPrefabArray(array)
        }, 0)

        EventMgr.onEvent_custom(ryw_Event.ChuMenEvent, (img) => {
            this.chuMenFuncEvent()
        }, this)

        this.refreshChengHao()
    }

    refreshChengHao() {
        // let chenghao = LevelUpManager.getChengHao()
        // //this.chenghaoPic.string = chenghao
        // Common5.addUrlSprite_custom('jingjie/'+chenghao, this.chenghaoPic)
        // let levelLab = User.getMyLevel()
        // this.levelLab.string = levelLab+''


        // let str1 = LevelUpManager.getCurClickIncome()
        // let allshouyi1 = MiShuManager.getAllIncomeMishu()

        // let allshouyi2 = MiShuManager.getAllIncomeDuiyou()
        // let allNum = allshouyi1+allshouyi2

        // let beishuSP = ShipuManager.getAllShipuInCome()
        // let beishuTW= TanWeiManager.getAllShipuInCome()

        // let clickNum = str1 * beishuTW //点击收益
        // let mzNum = Math.floor(allNum*beishuSP) //秒赚收益


        let str1 = LevelUpManager.getCurClickIncome() //人物点击收益(按等级算的) * 属性秘书加成属性摊位加成 * 出摊界面的点击收益按钮倍数 * 出摊界面的收益翻倍
        let allshouyi1 = MiShuManager.getAllIncomeMishu()//秘书秒赚总收益(包含出摊界面的收益翻倍)

        // let allshouyi2 = MiShuManager.getAllIncomeDuiyou()//队友秒赚总收益(包含出摊界面的收益翻倍)
        // let allNum = allshouyi1 + allshouyi2
        let allNum = allshouyi1

        let beishuSP = ShipuManager.getAllShipuInCome()//面点界面的物品秒赚加成
        let beishuTW = TanWeiManager.getAllShipuInCome() //摊位界面的物品点击加成

        let chenghaoInCome = LevelUpManager.getAllChengHaoinCome() //称号倍数

        let zichan = ZiChanManager.getZiChanBeiShu()

        // let mzNum = Math.floor(allNum * beishuSP * beishuTW)
        let clickNum = Math.floor(str1) //点击收益
        let mzNum = Math.floor(allNum * beishuSP * chenghaoInCome * zichan) //秒赚收益

        this.clickMoneyLab.string = '' + Common5.getNumberChangeHanzi(clickNum) + '/次'
        this.secondMoneyLab.string = '' + Common5.getNumberChangeHanzi(mzNum) + '/秒'


    }

    chuMenFuncEvent() {
        this.node.removeFromParent()
        this.node.destroy()
        PrefabManage.loadPrefabByType(GameType.MainHall)
    }

    showChatBtnStatus() {

        let isHaveTask = false
        let mainTaskInfo: any = TaskManager.getCurUnLockMainTaskInfo()
        let mainId = mainTaskInfo.Id
        if (mainId != MainTaskIdEnum.MainTask_None && mainId) {
            isHaveTask = true
        }


        let taskNode = this.uiNode.getChildByName('任务')
        let taskDescNode = taskNode.getChildByName('任务描述')
        let tanhao = taskNode.getChildByName('提示叹号')
        let taskLab = taskDescNode.getChildByName('lab').getComponent(cc.Label)
        let taskConfig = TaskManager.getTaskConfigById(mainId)

        if (isHaveTask) {

            //spineNodeTask.getComponent(sp.Skeleton).setAnimation(0,'呼吸',true)
            taskDescNode.active = true
            tanhao.active = true
            taskLab.string = taskConfig.TaskTittle
            taskNode.getComponent('ScaleScripts').startTween()
        } else {
            //spineNodeTask.getComponent(sp.Skeleton).setAnimation(0,'静止',false)
            taskDescNode.active = false
            tanhao.active = false
            taskNode.getComponent('ScaleScripts').stopAnim()
        }



        let indexStr = JuQingManager.getCurJuQingUnLockIndex()
        let uiNode = this.node.getChildByName('uiNode')

        let spineNode = uiNode.getChildByName('手机').getChildByName('spine')
        if (ChatManager.isChatTypeIncludeEventIndex('WX', indexStr) || Common5.ShenMirenChat) {
            spineNode.getComponent(sp.Skeleton).setAnimation(0, '震动', true)
            this.scheduleOnce(() => {
                Common5.playRemoteAudioEffect('sound/common/手机信息')
            }, 0.1)
            if (Common5.ShenMirenChat) {
                Common5.ShenMirenChat = false
            }
        } else {
            if (mainId == MainTaskIdEnum.MainTask_518) {
                spineNode.getComponent(sp.Skeleton).setAnimation(0, '震动', true)
            } else {
                spineNode.getComponent(sp.Skeleton).setAnimation(0, '待机', false)
            }

        }
    }

    refreshHead() {
        let headSpr = this.node.getChildByName("uiNode").getChildByName("head").getChildByName("头像").getComponent(cc.Sprite)
        Common5.addUrlSprite_custom("head/" + User.getHeadImg(), headSpr);

    }
    guideStep() {
        let mainTaskInfo: any = TaskManager.getCurUnLockMainTaskInfo()
        let mainId = mainTaskInfo.Id
        if (mainId == MainTaskIdEnum.MainTask_502 && Common5.task502) {
            Common5.task502 = false
            this.scheduleOnce(() => {
                Common5.ReportDY("inLevel", '关卡000011-开始剧情');
                let node = this.uiNode.getChildByName('出门')
                this.guideView([node])
            }, 0.05)
        } else if (mainId == MainTaskIdEnum.MainTask_505 && Common5.task505) {
            Common5.task505 = false
            this.scheduleOnce(() => {
                let node = this.uiNode.getChildByName('出门')
                this.guideView([node])
            }, 0.05)
        }



    }

    guideView(nodeArray) {
        PrefabManage.loadPrefabByType(GameType.GuideMskNode, null, (prefab) => {
            let guideNodeArray = nodeArray
            let firstNode = guideNodeArray.shift()
            prefab.getComponent(NewGuideScript).setBindNode(firstNode, guideNodeArray)
        })
    }


    roomStateChange() {
        let roomGoodStatus = User.getRoomGoodStatus()
        if (roomGoodStatus < 5) {
            this.uiNode.active = false
        } else {
            this.uiNode.active = true
        }
    }

    clickSettingBtnEvent() {
        //Common5.playEffect("sound/按键点击")
        PrefabManage.loadPrefabByType(GameType.Setting)
    }

    createRoom() {
        let roomGoodStatus = User.getRoomGoodStatus()
        if (roomGoodStatus <= 7) {
            Common5.loadRemoteAudioEffect('sound/锅飞出')
            PrefabManage.loadPrefabByType(GameType.JuQingRoom2, this.node.getChildByName('layer'))
        } else if (roomGoodStatus > 7 && roomGoodStatus <= 18) {
            PrefabManage.loadPrefabByType(GameType.JuQingRoom3, this.node.getChildByName('layer'))
        } else if (roomGoodStatus >= 19 && roomGoodStatus <= 25) {

            PrefabManage.loadPrefabByType(GameType.ShenHunShouSun, this.node.getChildByName('layer'))
        } else if (roomGoodStatus >= 26) {
            PrefabManage.loadPrefabByType(GameType.HuiJiaChaKan, this.node.getChildByName('layer'))
        }



    }
    chumenClick() {
        //Common5.playEffect("sound/按键点击")
        this.node.removeFromParent()
        this.node.destroy()
        PrefabManage.loadPrefabByType(GameType.MainHall)
    }

    clickTaskBtnEvent() {
        //Common5.playEffect("sound/按键点击")
        let customTaskInfo: any = TaskManager.getCurUnLockMainTaskInfo()
        if (customTaskInfo && Object.keys(customTaskInfo).length > 0 && customTaskInfo.Id != MainTaskIdEnum.MainTask_None) {
            PrefabManage.loadPrefabByType(GameType.TaskInfoNode, this.node)
        } else {
            PrefabManage.showTextTips('当前无任务')
        }

    }

    jishibenClickEvent() {
        //Common5.playEffect("sound/按键点击")

        PrefabManage.loadPrefabByType(GameType.JiShiBen)
        //PrefabManage.loadPrefabByType(GameType.FeiQiJiDi1)
    }

    // cebianlanClick(){
    //     //Common5.playEffect("sound/按键点击")
    //     // this.node.removeFromParent()
    //     // this.node.destroy()
    //     PrefabManage.loadPrefabByType(GameType.YanJiuSuo)
    // }

    clickPhoneBtnEvent() {
        //Common5.playEffect("sound/按键点击")
        JuQingManager.openJuQingOrChat('WX')
    }

    // update (dt) {}
}