import { DispatcherManager } from "../utils/DispatcherManager";
/*
* name;
*/
export class TimerService extends Laya.EventDispatcher
{
    private static ins:TimerService;
    private _timeScale:number = 1;

    private _unscalableList:Array<TimerControl> = [];
    constructor(){
        super();
        TimerService.ins = this;
    }

    public static get Instance():TimerService
    {
        if( TimerService.ins == null )
        {
            return new TimerService();
        }
        return TimerService.ins;
    }

    public init():void
    {
        Laya.timer.frameLoop( 1, this, this.onTimer );
    }

    private initEvent():void
    {
       
    }

    public stop():void
    {
        Laya.timer.clear( this, this.onTimer );
    }

    public recover():void
    {
        this.stop();
        Laya.timer.frameLoop( 1, this, this.onTimer );
    }

    private onTimer():void
    {
        var i:number = 0;
        for(i=0; i<this._unscalableList.length; i++)
        {
            this._unscalableList[i].update(Laya.timer.delta * this._timeScale);
        }
    }

    public hasUpdataTarget( updateTarget:TimerControl ):Boolean
    {
        var index:number;
        index = this._unscalableList.indexOf(updateTarget);
        if( index != -1 )
        {
            return true;
        }
        return false;
    }

    public addUpdateTarget(updateTarget:TimerControl):void
    {
        var index:number;
        index = this._unscalableList.indexOf(updateTarget);
        if(index==-1)
        {
            this._unscalableList.push(updateTarget);
        }
    }

    public removeUpdateTarget(updateTarget:TimerControl):void
    {
        var index:number = this._unscalableList.indexOf(updateTarget);
        if(index!=-1)
        {
            this._unscalableList.splice(index,1);
        }
    }

    public get timeScale():number
    {
        return this._timeScale;
    }
    /**
     * @private
     */
    public set timeScale(value:number)
    {
        if (value < 0 || isNaN(value))
        {
            value = 1;
        }
        this._timeScale = value;
    }
}