package command;

import openfl.utils.Object;
import t9.util.ColorTraces;
import openfl.Vector;
import vo.RiderVO;
import feathers.data.ArrayCollection;
import openfl.Lib;
import haxe.DynamicAccess;
import business.LoadRidersDelegate;
import com.adobe.cairngorm.commands.ICommand;
import com.adobe.cairngorm.control.CairngormEvent;
import feathers.rpc.IResponder;
import feathers.rpc.events.ResultEvent;
import haxe.Json;
import model.AppModelLocator;
import t9.util.ColorTraces.*;

class LoadRidersCommand implements ICommand implements IResponder {
	private var model = AppModelLocator.getInstance();

	public function execute(cgEvent:CairngormEvent):Void {
		// create a worker who will go get some data and pass it a reference to this command so the delegate knows where to return the data
		var delegate = new LoadRidersDelegate(this);
		// make the delegate do some work
		delegate.loadRidersService();
	}

	// this is called when the delegate receives a result from the service
	public function result(rpcEvent:Dynamic):Void {

        // populate the riders DP in the model locator with the JSON results from the service call

        var data:Array<Dynamic> = cast(rpcEvent, ResultEvent).result;
		var riders:Array<RiderVO> = data.map((r:Dynamic) -> {

			final id:Int = Std.parseInt(r.id);
			final name:String = r.name;
			final firstName:String = r.firstName;
			final age:Int = Std.parseInt(r.age);
			final lastLessonDate:Int = Std.parseInt(r.lastLessonDate);
			final level:Int = Std.parseInt(r.level);
			final credit:Int = Std.parseInt(r.credit);
			final address:String = r.address;
			final notes:String = r.notes;
			final ffeLicence:Bool = stringToBool(r.ffeLicence);
			final ffeLicenceValidityYear:Int = Std.parseInt(r.ffeLicenceValidityYear);
			final ffeLicenceNumber:String = r.ffeLicenceNumber;
			final legalGuardianName:String = r.legalGuardianName;
			final legalGuardianFirstName:String = r.legalGuardianFirstName;
			final legalGuardianRole:String = r.legalGuardianRole;
			final legalGuardianPhoneNumber:String = r.legalGuardianPhoneNumber;
			final legalGuardianEmail:String = r.legalGuardianEmail;

			return new RiderVO(id, name, firstName, age, lastLessonDate, level, credit, address, notes, ffeLicence, ffeLicenceValidityYear,ffeLicenceNumber,legalGuardianName,legalGuardianFirstName,legalGuardianRole,legalGuardianPhoneNumber,legalGuardianEmail);
		});

		model.ridersListDP = new ArrayCollection(riders);

		/*var riders:Array<RiderVO> = [];

		for (i in 0...data.length) {
			var r:Object = data[i];

			final id:Int = Std.parseInt(r.id);
			final name:String = r.name;
			final firstName:String = r.firstName;
			final age:Int = Std.parseInt(r.age);
			final lastLessonDate:Int = Std.parseInt(r.lastLessonDate);
			final level:Int = Std.parseInt(r.level);
			final credit:Int = Std.parseInt(r.credit);
			final address:String = r.address;
			final notes:String = r.notes;
			final ffeLicence:Bool = stringToBool(r.ffeLicence);
			final ffeLicenceValidityYear:Int = Std.parseInt(r.ffeLicenceValidityYear);
			final ffeLicenceNumber:String = r.ffeLicenceNumber;
			final legalGuardianName:String = r.legalGuardianName;
			final legalGuardianFirstName:String = r.legalGuardianFirstName;
			final legalGuardianRole:String = r.legalGuardianRole;
			final legalGuardianPhoneNumber:String = r.legalGuardianPhoneNumber;
			final legalGuardianEmail:String = r.legalGuardianEmail;

			final rider:RiderVO = new RiderVO(id, 
											name, 
											firstName, 
											age, 
											lastLessonDate, 
											level, credit, 
											address, 
											notes, 
											ffeLicence, 
											ffeLicenceValidityYear,
											ffeLicenceNumber,
											legalGuardianName,
											legalGuardianFirstName,
											legalGuardianRole,
											legalGuardianPhoneNumber,
											legalGuardianEmail
											);
			riders.push(rider);
		}*/

		/*var riders:Array<RiderVO> = cast(rpcEvent, ResultEvent).result;
		model.ridersListDP = new ArrayCollection(riders);*/

		//trace(model.ridersListDP.get(5).firstName);
        //traceGreen("ridersListDP.length --> " + model.ridersListDP.length);
		//var arr:Array<RiderVO> = [for (n in data) cast(n, RiderVO)];

		/*for(v in data){
			//var rider:RiderVO = new RiderVO();
			//var id:Int = Std.parseInt();
			traceCyan(v[]);

		}*/

		/*for (key => value in riders){
            //ConfigValues.data[key] = value;
			traceMagenta(key + " / " + riders[key]);
        }*/

		/*var rac:ArrayCollection<RiderVO> = new ArrayCollection();
		for (v in riders) {
			//var rider:RiderVO = cast v;
			rac.add(v);
		}
		model.ridersListDP = rac;*/

        /*
		var data:DynamicAccess<Dynamic> = Json.parse(cast(rpcEvent, ResultEvent).result);
		var riders:Array<RiderVO> = cast(rpcEvent, ResultEvent).result;
		var data:DynamicAccess<Dynamic> = Json.parse(e.target.data);
        for (key => value in data){
            ConfigValues.data[key] = value;
        } 
		*/

	}

	private function stringToBool(str) {
		var boolValue:Bool = (str == "true") ? true : false;
		return boolValue;
	}

	// this is called when the delegate receives a fault from the service
	public function fault(rpcEvent:Dynamic):Void {
		// store an error message in the model locator
		// labels, alerts, etc can bind to this to notify the user of errors
		model.errorStatus = "Fault occured in LoadRidersCommand.";
	}
}
