import {UserDataInfo} from "../Data/UserDataInfo";
import {LocalDataControl} from "../Control/LocalDataControl";
import {pingtaiutils} from "../WXSDK/pingtaiutils";

export class TimesTamper implements TimerControl
{
	public lastgameTime:number = 0;
	public constructor() 
	{

	}

	private static ins: TimesTamper;

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

	public init( ):void
	{
		this.lastgameTime = LocalDataControl.Instance.getnum( UserDataInfo.lasttime );
		Laya.timer.frameLoop(1, this, this.update );
	}

	/**
	 * 同步服务器时间(秒)
	 */
	private _serverTime: number = 0;

	private iscan:boolean = false;
	private _onlinedistime:number = 0;
	
	public updateServiceTime(serverTime: number): void {
		this._serverTime = Number(serverTime);
		this.iscan = true;
		this._onlinedistime = this._serverTime;
		LocalDataControl.Instance.addstr( UserDataInfo.lasttime,this._serverTime.toString() );
	}

	private _time:number = 0;
	private _onlinetime:number = 0;
	public update(): void 
	{
		if( this.iscan )
		{
			this._time += Laya.timer.delta;
			this._onlinetime += Laya.timer.delta;
			this._serverTime += Laya.timer.delta / 1000;
			if( this._time >= 30000 )
			{
				this._time = 0;
				LocalDataControl.Instance.addstr( UserDataInfo.lasttime,this._serverTime.toString() );
			}
			if( this._onlinetime >= 90000 )
			{
				this._onlinetime = 0;
				this.sendonlineTongji();
			}
		}
	}

	public sendonlineTongji():void
	{
		var distime:number = this.serverTime - this._onlinedistime;
		if( distime > 0 )
		{
			this._onlinedistime = this.serverTime;
		}
	}

	/**
	 * 服务器时间 ( 秒 ) 
	 * @return 
	 * 
	 */
	public get serverTime(): number {
		return this._serverTime;
	}

}