import GObject from "./gobject";
import {Assert} from "./utilfuncs";
import {default as State, BeginState, EndState} from "./state";

export default class FSMachine<T> extends GObject {
	private _state: State<T>;
	private _nextState: State<T>|null;

	constructor(p: number, state: State<T>) {
		super(p);
		this._state = state;
		this._nextState = null;
		state.onEnter(<T><any>this, <State<T>><any>BeginState);
	}
	_doSwitchState(): void {
		for(;;) 

	}
	setState(st: State<T>): void {
		Assert(!this._nextState);
		this._nextState = st;
	}
	onUpdate(dt: number): boolean {
		return super.aliveCB(()=>{
			this._state.onUpdate(<T><any>this, dt);
			this._doSwitchState();
		});
	}
	onDown(ret: any): void {
		this._state.onDown(<T><any>this, ret);
	}
	onUp(): void {
		this._state.onUp(<T><any>this);
	}
	// --------------- from BaseObject ---------------
	destroy(): boolean {
		if(super.destroy()) {
			this.setState(EndState);
			this._doSwitchState();
			return true;
		}
		return false;
	}
}

{
			const ns = this._nextState;
			if(!ns)
				break;

			this._nextState = null;
			const old = this._state;
			old.onExit(<T><any>this, ns);
			this._state = ns;
			ns.onEnter(<T><any>this, old);
		}