/*
 * @Author: YeeChan
 * @Date: 2021-12-30 14:42:48
 * @Description: 体力管理器
 */

import { ryw_Event } from "../../FrameWork/Event/EventEnum";
import EventMgr from "../../FrameWork/Event/EventMgr";
import GameMgr from "../../FrameWork/Mgr/GameMgr";
import Common5 from "../../Platform/th/Common5";
import RotateScripts from "./RotateScripts";


export default class TimeManger {
    maxTime:number = 0;
    initTime:number = 0;
    timeLabel:cc.Label = null;
    rotNode:cc.Node = null;
    title:string = "当前剩余时间:";
    interval = null;
    startTime = true
    public init(time,label){
        this.maxTime = time;
        this.initTime = time;
        this.timeLabel = label;
        if(this.timeLabel){
            this.timeLabel.string = this.initTime+''
        }
   
        this.interval = setInterval(()=>{
            this.interFunc();
        },1000)
    }
    public setRotNode(node){
        node.getComponent(RotateScripts).stop();
        this.rotNode = node;
    }
    public reset(){        
        clearInterval(this.interval);
        this.interval = null;
        this.maxTime = Math.max(this.maxTime,this.initTime);
        this.interval = setInterval(()=>{
            this.interFunc();
        },1000)
    }
    public add(addTime){
        this.maxTime += addTime;
        if (this.interval == null){
            this.interval = setInterval(()=>{
               this.interFunc();
            },1000)
        }
    }
    public setTitle(titleInput){
        this.title = titleInput;
    }
    interFunc(){
        if(!this.startTime){
            return
        }  
        try{
            if (this.maxTime > 0){
                this.maxTime--;
                if (this.timeLabel != null){
                    this.timeLabel.string = this.title+this.maxTime.toString();
                }
                if (this.maxTime <= 10){
                    if (this.maxTime == 10){
                        this.rotNode.getComponent(RotateScripts).startCustom();
                    }
                    Common5.playEffect("timeTick");
                }
                else{
                    this.rotNode.getComponent(RotateScripts).stop();
                }
            }
            else{
                if (this.rotNode!=null){
                    this.rotNode.getComponent(RotateScripts).stop();
                }
                EventMgr.emitEvent_custom(ryw_Event.timeOut);
                clearInterval(this.interval);
                this.interval = null;
            }
        }
        catch{
            clearInterval(this.interval);
        }
    }
    public stop(){
        clearInterval(this.interval);
        this.interval = null;
    }
    public restart(){
        if (null == this.interval){
            this.interval = setInterval(()=>{
                this.interFunc();
            },1000) 
        }
    }

    public pauseTime(){
        this.startTime = false
    }
    public recoveryTime(){
        this.startTime = true

    }

}