
// AppController.j
// RMSchedulerController
//
// Created by Alonso Villanueva

@import <Foundation/Foundation.j>
@import <AppKit/AppKit.j>
@import <MapKit/MKMapView.j>
@import <AppKit/CPTextField.j>
@import <AppKit/CPApplication.j>
@import <Foundation/CPURLRequest.j>
@import "AppController.j"




// var appCont; //comment 2017-03-03





@implementation RMSchedulerPreferencesController : RMControllerObject {


	CPPanel userPane;
	id coloPopu;
	

	id rText;
	id gText;
	id bText;

	id labe;

	id befoMnth;
	id afteMnth;

}



- (id) init:(id)paraAppCont {
	[super init];
 	
 	appCont = paraAppCont;
	dataBase = [appCont getDataBase];
	preferPane = NULL;
	rText = NULL;
	gText = NULL;
	bText = NULL;
	befoMnth = NULL;
	afteMnth = NULL;

	return self;
}




- (void)  applicationQuit {

	if ( preferPane ) {
		[preferPane orderOut:0];
		[preferPane release];
	}

	if (rText) { [passText  release];}
	if (gArra) { [userArra  release];}
	if (bText) { [nameText  release];}
	
}


- (void) reportsPanel {
	return preferPane;
}


- (void) reportsPanel:(id)sender {

	var horiPos1 = 10;
	var horiPos2 = horiPos1 + 210;


	var horiTextWidt = 200;
	var horiPosiOffs = -4;
	var horiPosi = 200;

	var vertPosi = 20;
	var vertPosiOffs = -4;
	var vertIncr = 26;
	var vertLineHeig = 30;

	var paneWidt = 440;
	var paneHeig = 260;
	
	var x = 10 + Math.random()*40;
	var y = 60 + Math.random()*40;
	var fram = CGRectMake(x,y, paneWidt,paneHeig);

	preferPane = [[CPPanel alloc] initWithContentRect:fram styleMask:CPHUDBackgroundWindowMask|CPClosableWindowMask|CPResizableWindowMask]; 
	[preferPane setFloatingPanel:YES]; 
	[preferPane orderFront:self];
	[preferPane setTitle:@"Preferencias"];
	
	var paneContView = [preferPane contentView];
	[paneContView setBackgroundColor:coloDefaLite];

	var view = paneContView;
	
	var labe= [self addTextField:@"INTERFASE: Preferencias de color :" TFPosX:horiPos1 TFPosY:vertPosi TFWidth:0 TFHeight:0 VView:view Editable:NO];
	[labe setTextColor:[CPColor whiteColor]];

	vertPosi += 30;

	coloPopu = [self addPopupButton:@"Color del Fondo" TFPosX:horiPos1 TFPosY:vertPosi TFWidth:200 TFHeight:24  VView:view pullsDown:NO];
	[coloPopu setTarget:self];
	[coloPopu setAction:@selector(colorPopupSelection:)];
	[coloPopu addItemWithTitle:"Color de las ventanas"];



	labe = [self addTextField:@"R:" TFPosX:horiPos2+10 TFPosY:vertPosi TFWidth:0 TFHeight:0 VView:view Editable:NO];
	[labe setTextColor:[CPColor whiteColor]];
	rText = [self addTextField:@"" TFPosX:horiPos2+20 TFPosY:vertPosi-4 TFWidth:50 TFHeight:vertLineHeig VView:view Editable:YES];
	labe = [self addTextField:@"G:" TFPosX:horiPos2+70 TFPosY:vertPosi TFWidth:0 TFHeight:0 VView:view Editable:NO];
	[labe setTextColor:[CPColor whiteColor]];
	gText = [self addTextField:@"" TFPosX:horiPos2+80 TFPosY:vertPosi-4 TFWidth:50 TFHeight:vertLineHeig VView:view Editable:YES];
	labe = [self addTextField:@"B:" TFPosX:horiPos2+130 TFPosY:vertPosi TFWidth:0 TFHeight:0 VView:view Editable:NO];
	[labe setTextColor:[CPColor whiteColor]];
	bText = [self addTextField:@"" TFPosX:horiPos2+140 TFPosY:vertPosi-4 TFWidth:50 TFHeight:vertLineHeig VView:view Editable:YES];

    
    vertPosi += 50;
	var labe= [self addTextField:@"SCHEDULER: Calendario sin filtros : ( Rango de meses )" TFPosX:horiPos1 TFPosY:vertPosi TFWidth:0 TFHeight:0 VView:view Editable:NO];
	[labe setTextColor:[CPColor whiteColor]];

	vertPosi += 30;
	var labe= [self addTextField:@"    Meses anteriores:" TFPosX:horiPos1 TFPosY:vertPosi TFWidth:0 TFHeight:0 VView:view Editable:NO];
	[labe setTextColor:[CPColor whiteColor]];
	befoMnth = [self addTextField:@"" TFPosX:horiPos2 + 20 TFPosY:vertPosi-4 TFWidth:50 TFHeight:vertLineHeig VView:view Editable:YES];

	vertPosi += 30;
	var labe= [self addTextField:@"    Meses posteriores:" TFPosX:horiPos1 TFPosY:vertPosi TFWidth:0 TFHeight:0 VView:view Editable:NO];
	[labe setTextColor:[CPColor whiteColor]];
	afteMnth = [self addTextField:@"" TFPosX:horiPos2 + 20 TFPosY:vertPosi-4 TFWidth:50 TFHeight:vertLineHeig VView:view Editable:YES];

	vertPosi += 50;

    var butt = [[CPButton alloc] initWithFrame: CGRectMake(horiPos1,vertPosi, 75,24)];
	[butt setTitle:"Actualizar"];
	[butt setTarget:self];
	[butt setAction:@selector(colorUpdate:)];                
	[view addSubview:butt];

	[self colorPopupSelection:self];
	[self schedulerRangePreference:self];

	/**
	var quer = "Select p.* from riglobal.userAttributes as p inner join riglobal.user as u on p.objectName = u.idUser where u.name '"+appCont.accoUser+"';"
	var arraAux = [self query:quer];
	if([arraAux count] > 0){
		for ( var i=0; i<[arraAux count]; i++ ) {
			var valuList = [arraAux[i] valueList];
			console.log(valuList);
		}
	}
	**/



}

- (void) colorPopupSelection:(id)sender {
	[rText setStringValue:@""];
	[gText setStringValue:@""];
	[bText setStringValue:@""];
	var preference = "";
	var coloOption = [coloPopu titleOfSelectedItem];
	if(coloOption == "Color del Fondo"){
		preference = "backgroundColor";
	}else if(coloOption == "Color de las ventanas"){
		preference = "windowColor";
	}
	var quer = "Select attributeValue from riglobal.userAttributes where objectName = '"+appCont.accoUser+"' and attributeName = '"+ preference +"' limit 1;"
	var arraAux = [self query:quer];
	if([arraAux count] > 0){
		for ( var i=0; i<[arraAux count]; i++ ) {
			var valuList = [arraAux[i] valueList];
			var attrValue = valuList[0];
			var comp = [attrValue componentsSeparatedByString:@"#SC#"];
			[rText setStringValue:comp[0]];
			[gText setStringValue:comp[1]];
			[bText setStringValue:comp[2]];
		}
	}
}

- (void) colorUpdate:(id)sender {
	var r = [rText  stringValue];
	var g = [gText  stringValue];
	var b = [bText  stringValue];

	if(r > 255){
		r = 255;
		[rText setStringValue:@"255"];
	}
	if(g > 255){
		g = 255;
		[gText setStringValue:@"255"];
	}
	if(b > 255){
		b = 255;
		[bText setStringValue:@"255"];
	}
	console.log((r*1+g*1+b*1));
	if((r*1+g*1+b*1) > 756){
		[glblAlerPane showWithMessage:@"Atención" info:"Debe escoger un color mas oscuro para evitar conflictos con el color de las letras."];
		return;
	}


    var frst = NO;
    if ( [[befoMnth stringValue] isEqualToString:@""] || [[afteMnth stringValue] isEqualToString:@""] ) {
		[befoMnth setStringValue:@"2"];
		[afteMnth setStringValue:@"4"];   
		frst = YES; 
    }

    var attrValu =  [befoMnth stringValue] + "#SC#" + [afteMnth stringValue];
	console.log(attrValu);
	var quer;
	if ( frst ) {
	    quer = "Insert into riglobal.userAttributes (objectName,attributeName,attributeValue) values ( '"+appCont.accoUser + "','scheduleRange','" + attrValu + "');";
	}
	else {
		quer = "Update riglobal.userAttributes SET attributeValue='" + attrValu + "' WHERE objectName = '" + appCont.accoUser + "' AND attributeName='scheduleRange'";
	}
	console.log(quer);
	var arraAux = [self query:quer];
	

	var preference = "";
	var coloOption = [coloPopu titleOfSelectedItem];

	if(coloOption == "Color del Fondo"){
		preference = "backgroundColor";
	}else if(coloOption == "Color de las ventanas"){
		preference = "windowColor";
	}
	var quer = "Select p.id from riglobal.userAttributes as p  where p.objectName = '"+appCont.accoUser+"' and  p.attributeName = '"+preference+"';";
	var arraAux = [self query:quer];
	if ( [arraAux count] > 0 ) {
    	var quer = "Update riglobal.userAttributes set attributeValue = '"+r+"#SC#"+g+"#SC#"+b+"' where objectName = '"+appCont.accoUser+"' and attributeName = '"+preference+"';";
		var arraAux = [self query:quer];
    }else{
    	var quer = "Insert into riglobal.userAttributes (objectName,attributeName,attributeValue) values ( '"+appCont.accoUser+"','"+preference+"','"+r+"#SC#"+g+"#SC#"+b+"');";
		var arraAux = [self query:quer];
    }

    [glblAlerPane showWithMessage:@"Atención" info:"Cambio realizado! Debe cerrar sesion y volver a ingresar para ver reflejados los cambios."];
	
}




- (void) schedulerRangePreference:(id)sender {
	[befoMnth setStringValue:@""];
	[afteMnth setStringValue:@""];
	var preference = "scheduleRange";
	[befoMnth setStringValue:@"2"];
	[afteMnth setStringValue:@"4"];
	var quer = "Select attributeValue from riglobal.userAttributes where objectName = '"+appCont.accoUser+"' and attributeName = '"+ preference +"' limit 1;"
	var arraAux = [self query:quer];
	if( [arraAux count] == 1 ){
		var comp = [[arraAux[0] valueList][0] componentsSeparatedByString:@"#SC#"];
		[befoMnth setStringValue:comp[0]];
		[afteMnth setStringValue:comp[1]];
	}
	else {

	}
}


@end
