import RoutingSystem from './RoutingSystem.js';
import ViewSystem, { View } from './ViewSystem.js';
import ModuleSystem from './ModuleSystem.js';
import Modules from '../../modules';
import EventSystem from './EventSystem';
import BaseSystem from './System';

export default class ActionSystem extends BaseSystem {
	constructor(CS) {
		super();
		// TODO -- Make correct Initialization
		this.CoreSystem = CS;
		this.EventSystem = new EventSystem();
		
		this.events = [...ActionSystem.Actions];

		this._disabled = false;

		this.setupEvents();
	}
	setupEvents() {
		this.events.map((Action) => {
			this.EventSystem.on(Action.id, (d) => this.dispatch(Action, d));
		});
	}
	
	dispatch(Action, data) {
		return !this._disabled && new Action(this.CoreSystem).run(data)
	}

	disable() {this._disabled = true;}
	enable() {this._disabled = false;}
	toggle(value) {this._disabled = value || !this._disabled;}

	createAction(action) {
		if (!action.type) return;
		this.EventSystem.emit(action.type, action.data);
	}
}


export class RouteAction {
	constructor(CS) {
		this.CoreSystem = CS;
	}

	run(data) {
		let {
			route
		} = data;
		if(route) 
			this.CoreSystem.goto(route);
	}
}
RouteAction.id = "changeRoute";

ActionSystem.RouteAction = RouteAction;

ActionSystem.Actions = [
	RouteAction
];