class Controller {
	constructor(model, view) {

		this.model = model;
		this.view = view;
		this.filter = "all";
		
		/** Abonnements à la vue **/

		this.view.bindAddTodo(this.addTodo.bind(this));
		this.view.bindDeleteTodo(this.deleteTodo.bind(this));
		this.view.bindToggleTodo(this.toggleTodo.bind(this));
		this.view.bindEditTodo(this.editTodo.bind(this));

		/** filtrages par url **/

		this.routes = ['all','active','done'];

		/** Routage **/
		window.addEventListener("load",this.routeChanged.bind(this));
		window.addEventListener("hashchange",this.routeChanged.bind(this));
	}


	routeChanged(){
		let route =  window.location.hash.replace(/^#\//,'');
			this.filter = this.routes.find( r => r === route) || 'all';
			this.filterTodoList();
		}




	filterTodoList () {
		let todos = this.model.getTodos(this.filter)
		this.view.renderTodoList(todos)
		this.view.setFilterTabs(this.filter)
	}

	addTodo(text)  {
		this.model.add(text)
		this.filterTodoList()
	}

	deleteTodo(id)  {
		this.model.delete(parseInt(id))
		this.filterTodoList()
	}

	toggleTodo(id)  {
		this.model.toggle(parseInt(id))
		this.filterTodoList()
	}

	editTodo(id, text)  {
		this.model.edit(parseInt(id),text)
		this.filterTodoList()
	}
}

const model = new Model()
const view = new View()
const app = new Controller(model, view)

