package model;

import view.RidersList;
import vo.RiderVO;
import feathers.data.ArrayCollection;
import openfl.errors.Error;
import openfl.events.Event;
import com.adobe.cairngorm.model.IModelLocator;
import openfl.events.EventDispatcher;
import t9.util.ColorTraces.*;

class AppModelLocator extends EventDispatcher implements IModelLocator {

    // events constants
    public static final VIEWING_CHANGE:String = "viewingChange";
    public static final RIDERS_LIST_DP_CHANGE:String = "ridersListDPChange";
	public static final DRAWER_STATE_CHANGE:String = "drawerStateChange";
	public static final RIDER_SELECT_CHANGE:String = "riderSelectChange";

    // this instance stores a static reference to itself
	private static var model:AppModelLocator;

    // available values for the main viewstack defined as constants to help uncover errors at compile time instead of run time
	// Navigator
	public static final ADMIN_LOGIN = 0;
	public static final RIDERS_LIST = 1;
	public static final RIDER_DETAIL = 2;

    // viewstack starts out on the admin login screen
	public var viewing(default, set):Int = ADMIN_LOGIN;

    private function set_viewing(value:Int):Int {
		viewing = value;
		dispatchEvent(new Event(VIEWING_CHANGE));
		return viewing;
	}

	// at startup, the drawer is closed
	public var drawerIsOpen(default, set):Bool = false;

    private function set_drawerIsOpen(pIsOpen:Bool):Bool {
		drawerIsOpen = pIsOpen;
		dispatchEvent(new Event(AppModelLocator.DRAWER_STATE_CHANGE));
		return drawerIsOpen;
	}

	// temporary holding space for rider we're creating or editing
	// this gets copied into or added onto the main rider list
	public var selectedRider(default, set):RiderVO = null;

    private function set_selectedRider(pRider:RiderVO):RiderVO {
		selectedRider = pRider;
		dispatchEvent(new Event(AppModelLocator.RIDER_SELECT_CHANGE));
		return selectedRider;
	}

	// update one property of the selectedRider VO
	public function updateSelectedRiderProp(pProperty:String, pValue:Dynamic) {
		Reflect.setField(selectedRider, pProperty, pValue);
		traceYellow(Reflect.field(selectedRider, pProperty));
	}

    // contains the main riders list which is populated on startup
	// mx:application's creationComplete event is mutated into a cairngorm event
	// that calls the httpservice for the data
	public var ridersListDP(default, set):ArrayCollection<RiderVO>;

    private function set_ridersListDP(value:ArrayCollection<RiderVO>):ArrayCollection<RiderVO> {
		ridersListDP = value;
		dispatchEvent(new Event(RIDERS_LIST_DP_CHANGE));
		return ridersListDP;
	}

    // variable to store error messages from the httpservice 
    // nothinng currently binds to it, but an Alert or the login box could to show startup errors
	public var errorStatus:String;


    // singleton: constructor only allows one model locator
	public function new() {
		super();
		if (AppModelLocator.model != null) {
			throw new Error("Only one ModelLocator instance should be instantiated");
		}
	}

	// singleton: always returns the one existing static instance to itself
	public static function getInstance():AppModelLocator {
		if (model == null)
			model = new AppModelLocator();
		return model;
	}

	public function addRider(pRider:RiderVO) {
		ridersListDP.add(pRider);
	}

}