// 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 JuQingManager from "../JuQingChat/JuQingManager";
import TaskManager, { MainTaskIdEnum } from "../JuQingChat/TaskManager";
import PrefabManage, { GameType } from "../PrefabManager/PrefabManage";

const {ccclass, property} = cc._decorator;

@ccclass
export default class ShenHunShouSun extends cc.Component {



    // LIFE-CYCLE CALLBACKS:

    // onLoad () {}

    start () {
        let mainTaskInfo:any = TaskManager.getCurUnLockMainTaskInfo()
        let mainId = mainTaskInfo.Id
        
        if(mainId == MainTaskIdEnum.MainTask_529 && Common5.showDH12){
            Common5.showDH12  = false
            JuQingManager.unLockNewJuQing('DH_12')
        }
       
        this.changeRoomView()
        EventMgr.onEvent_custom(ryw_Event.RefreshJuQingDuiHua,()=>{
            this.changeRoomView()
        },this)

        EventMgr.onEvent_custom(ryw_Event.RefreshJuQingDuiHua,()=>{
            this.changeRoomView()
        },this)

        EventMgr.onEvent_custom(ryw_Event.updateMainTask,()=>{
            this.getMainTask()
        },this)
        // this.node.getChildByName('龙镇南').active = true

        this.initGame()
    }

    getMainTask(){
        let mainTaskInfo:any = TaskManager.getCurUnLockMainTaskInfo()
        let mainId = mainTaskInfo.Id
        if(mainId == MainTaskIdEnum.MainTask_537){
            this.node.getChildByName('男主').scaleX = -1
            this.node.getChildByName('男主').setPosition(cc.v2(-156,13))
            this.node.getChildByName('男主').active = true
            Common5.playRemoteSpine(this.node.getChildByName('龙镇南'), 'Spine/WuXingYuanShi','龙镇南','待机')
            this.node.getChildByName('龙镇南').active = true
        }
    }


    initGame(){
        let mainTaskInfo:any = TaskManager.getCurUnLockMainTaskInfo()
        let mainId = mainTaskInfo.Id
        if(mainId == MainTaskIdEnum.MainTask_536){
           
            let indexStr = JuQingManager.getCurJuQingUnLockIndex()
            if(indexStr == 'DH_17'){
                this.node.getChildByName('男主').scaleX = -1
                this.node.getChildByName('男主').setPosition(cc.v2(-156,13))
                this.node.getChildByName('男主').active = true
    
                this.node.getChildByName('妹妹').active = false
                this.node.getChildByName('女主站').active = false
               
                this.node.getChildByName('器灵').active = false   

                Common5.playRemoteSpine(this.node.getChildByName('龙镇南'), 'Spine/WuXingYuanShi','龙镇南','待机')
                this.node.getChildByName('龙镇南').active = true
            }else{
                PrefabManage.loadPrefabByType(GameType.WuXingYuanShi,null, ()=>{
                    this.node.getChildByName('龙镇南').active = true
                    Common5.playRemoteSpine(this.node.getChildByName('龙镇南'), 'Spine/WuXingYuanShi','龙镇南','待机')
                })  
                this.node.getChildByName('妹妹').active = false
                this.node.getChildByName('女主站').active = false
                this.node.getChildByName('男主').active = false
                this.node.getChildByName('器灵').active = false   
         
            }
        }else  if(mainId == MainTaskIdEnum.MainTask_537 || mainId == MainTaskIdEnum.MainTask_538){
            this.node.getChildByName('男主').scaleX = -1
            this.node.getChildByName('男主').setPosition(cc.v2(-156,13))
            this.node.getChildByName('男主').active = true

            Common5.playRemoteSpine(this.node.getChildByName('龙镇南'), 'Spine/WuXingYuanShi','龙镇南','待机')
            this.node.getChildByName('龙镇南').active = true
            this.node.getChildByName('妹妹').active = false
            this.node.getChildByName('女主站').active = false
           
            this.node.getChildByName('器灵').active = false 
        } else if(mainId == MainTaskIdEnum.MainTask_540){
            TaskManager.finishCurMainTask(MainTaskIdEnum.MainTask_540) 
        }

        let roomGoodStatus = User.getRoomGoodStatus()
        if(roomGoodStatus>=25){
            //
            this.node.getChildByName('妹妹').active = false
            this.node.getChildByName('妹妹站').active = true
            Common5.playRemoteSpine(this.node.getChildByName('妹妹站'), 'Spine/roomSpine/妹妹站立', '妹妹站立','animation')
            
            this.setAnim(this.node.getChildByName('女主站'),'女待机')
        }
    }

    setAnim(node, anim, loop = true){
        node.getComponent(sp.Skeleton).setAnimation(0,anim,loop)
    }


    changeRoomView(){
        let roomGoodStatus = User.getRoomGoodStatus()
        if(roomGoodStatus>=21 && roomGoodStatus<=22){
            this.node.getChildByName('妹妹').active = false
            this.node.getChildByName('妹妹站').active = true
            Common5.playRemoteSpine(this.node.getChildByName('妹妹站'), 'Spine/roomSpine/妹妹站立', '妹妹站立','animation')
            //this.setAnim(this.node.getChildByName('妹妹'),'妹妹迷茫')

            this.setAnim(this.node.getChildByName('女主站'),'女生气')
            //this.node.getChildByName('男主').active = true
            if(roomGoodStatus == 22){
                this.node.getChildByName('男主').active = false
            }
        }
    }
    // update (dt) {}
}