import view.RiderCardDrawer;
import openfl.system.Security;
import business.Services;
import com.adobe.cairngorm.control.CairngormEventDispatcher;
import control.AppController;
import control.LoadRidersEvent;
import feathers.controls.Application;
import feathers.events.FeathersEvent;
import model.AppModelLocator;
import openfl.events.Event;
import t9.util.ColorTraces.*;
import view.MainDrawer;
import view.MainPanel;

class LPTCManager2026 extends Application {
	
	private var model:AppModelLocator;
	private var services:Services;
	private var appController:AppController;
	private var mainPanel:MainPanel;
	private var md:MainDrawer;
	private var rcd:RiderCardDrawer;

	public function new() {
		super();

		model = AppModelLocator.getInstance();
		services = new Services();
		appController = new AppController();

		addEventListener(FeathersEvent.CREATION_COMPLETE, onCreationComplete);
	}

	override private function initialize():Void {
		super.initialize();

		stage.displayState = NORMAL;
		stage.scaleMode = NO_SCALE;

		mainPanel = new MainPanel();
		addChild(mainPanel);

		md = new MainDrawer();
		addChild(md);

		rcd = new RiderCardDrawer();
		addChild(rcd);

		model.addEventListener(AppModelLocator.DRAWER_STATE_CHANGE, onDrawerStateChange);
		model.addEventListener(AppModelLocator.RIDER_SELECT_CHANGE, onRiderSelectChange);
		
		
		//traceBlue(this + " --> initialize()");
	}

	private function loadRiders():Void {
		traceGreen(this + " --> loadRiders()");
		var cgEvent:LoadRidersEvent = new LoadRidersEvent();
		CairngormEventDispatcher.getInstance().dispatchEvent(cgEvent);
	}

	private function onRiderSelectChange(event:Event):Void {
		trace(this + " --> onRiderSelectChange()");
		rcd.populateAndShow();
	}

	private function onDrawerStateChange(e:Event):Void {
		model.drawerIsOpen ? md.openDrawer() : md.closeDrawer();
	}

	private function onCreationComplete(event:FeathersEvent):Void {
		traceBlue("Application --> onCreationComplete()");
		loadRiders();
	}
}
