package employees.view
{
	import org.puremvc.as3.multicore.patterns.mediator.Mediator;
	import org.puremvc.as3.multicore.interfaces.INotification;
	
	import flash.events.Event;
	import mx.controls.Alert;
	
	import employees.ApplicationFacade;
	import employees.model.LoadEmployeesProxy;
	import employees.view.components.EmployeesDataGrid;
	
	public class EmployeesDataGridMediator extends Mediator
	{
		public static const NAME:String = 'EmployeesListMediator';
		
		public function EmployeesDataGridMediator(viewComponent:EmployeesDataGrid) {
			super(NAME, viewComponent);
			employeesDataGrid.addEventListener(EmployeesDataGrid.LOAD_EMPLOYEES,
				onGetEmployees);
		}
		
		protected function onGetEmployees(event:Event):void {
			sendNotification(ApplicationFacade.LOAD_EMPLOYEES);
		}
		
		override public function listNotificationInterests():Array {
			return [
				LoadEmployeesProxy.LOAD_EMPLOYEES_SUCCESS,
				LoadEmployeesProxy.LOAD_EMPLOYEES_FAILED
			];
		}
		
		override public function handleNotification(note:INotification):void {
			switch (note.getName()) {
				case LoadEmployeesProxy.LOAD_EMPLOYEES_SUCCESS:
					employeesDataGrid.employeesList.dataProvider = note.getBody();
					break;
				case LoadEmployeesProxy.LOAD_EMPLOYEES_FAILED:
					Alert.show(note.getBody().toString(), 'Error');
					break;
			}
		}
		
		protected function get employeesDataGrid():EmployeesDataGrid {
			return viewComponent as EmployeesDataGrid;
		}
	}
}