Ext.namespace('Ext.cgss');

// opciones de configuracion
/*
allowComparasions
allowLogics
allowParentheses
allowSigns
allowComma
allowConstant
allowEqual
allowLessThan
allowGreaterThan
allowLessEqualThan
allowGreaterEqualThan
allowDifferent
allowAnd
allowOr
allowPlus
allowMinus
allowMultiply
allowDivide
allowParOpen
allowParClose
constant_delimiter_open
constant_delimiter_close
components 
*/

// metodos
/*
addText(text)
getValue()
getText()
setValue(value)
clearText()
*/

// eventos 
/*
change(elemento, anterior, nuevo, valor_adicionado)
*/

Ext.cgss.Expression  = Ext.extend(Ext.Panel, {
	addText: this.addText,
	getValue: this.getValue,
	clearText: this.clearText,
	setValue: this.setValue,
	constructor: function (config) {
		
		var arr_textadded = [];
		var arrValueAdded = [];
			
		// console.log(config);
		addText = function (text, scope) {
			if (!Ext.isDefined(scope)){ scope = this; }	
			// console.log("this", scope);
			var text_cmp = scope.get(0);	
			var txt = text_cmp.getValue();
			
			var new_text = "";

			if (!Ext.isDefined(txt)){ txt = ""; }
			// si el objeto es un texto, debe tener una propiedad text y una value
			if (Ext.isObject(text)) {
				if (text.text) {
					new_text = text.text;					
				}
				if (text.value) {
					arrValueAdded.push(text.value);
				}
			} else  {
				new_text = text;
				arrValueAdded.push(text);
			}

			var new_val = txt + new_text;
			text_cmp.setValue(new_val);
			
			arr_textadded.push(new_text);

			this.fireEvent("change", this, txt, new_val, new_text);
		};

		this.clearText = function (scope) {
			// var originalThis = this;
			if (!Ext.isDefined(scope)) { scope = this; }	
			// console.log("this", scope);
			var text_cmp = scope.get(0);
			var orig_txt = text_cmp.getValue();	
			var txt = text_cmp.setValue("");
			// limpio el value
			arrValueAdded = [];

			this.fireEvent("change", this, orig_txt, "", null);
		};

		this.getValue = function (scope) {
			if (!Ext.isDefined(scope)) { scope = this; }	
			return arrValueAdded.join("");
		};
				
		this.getText = function (scope) {
			if (!Ext.isDefined(scope)) { scope = this; }	
			// console.log("this", scope);
			var text_cmp = scope.get(0);	
			return text_cmp.getValue();		
		};

		this.setValue = function (value, scope) {
			if (!Ext.isDefined(scope)) { scope = this; }	
			// console.log("this", scope);
			var text_cmp = scope.get(0);	
			var orig_txt = text_cmp.getValue();	
			var txt = text_cmp.setValue(value);
			
			this.fireEvent("change", this, orig_txt, value, value);
				
		};

		this.addText = addText;
		
		var ancho_boton = 20;

		var btn_eq = new Ext.Button({
			text: "=",
			width: ancho_boton
		});

		btn_eq.on("click", function(){
			this.addText(" = ", this);
		}, this);

		var btn_lt = new Ext.Button({
			text: "&lt;",
			width: ancho_boton
		});

		btn_lt.on("click", function(btn,e){
			this.addText(" < ", this);		
		}, this);

		var btn_gt = new Ext.Button({
			text:">",
			width: ancho_boton
		});

		btn_gt.on("click", function(btn,e){
			this.addText(" > ", this);		
		}, this);

		var btn_lte = new Ext.Button({
			text:"<=",
			width: ancho_boton
		});

		btn_lte.on("click", function(btn,e){
			this.addText(" <= ", this);		
		}, this);

		var btn_gte = new Ext.Button({
			text:">=",
			width: ancho_boton
		});

		btn_gte.on("click", function(btn,e){
			this.addText(" >= ", this);		
		}, this);

		var btn_dif = new Ext.Button({
			text: "<>",
			width: ancho_boton
		});

		btn_dif.on("click", function(){
			this.addText(" <> ", this);
		}, this);

		var btn_y = new Ext.Button({
			text: "Y",
			width: ancho_boton
		});

		btn_y.on("click", function(btn,e){
			this.addText(" Y ", this);
		}, this);

		var btn_o = new Ext.Button({
			text: "O",
			width: ancho_boton
		});

		btn_o.on("click", function(btn,e){
			this.addText(" O ", this);
		}, this);

		// SIGNOS
		var btn_plus = new Ext.Button({
			text: "+",
			width: ancho_boton
		});

		btn_plus.on("click", function(){
			this.addText(" + ", this);
		}, this);

		var btn_minus = new Ext.Button({
			text: "-",
			width: ancho_boton
		});

		btn_minus.on("click",function(){
			this.addText(" - ", this);
		}, this);

		var btn_mult = new Ext.Button({
			text: "*",
			width: ancho_boton
		});

		btn_mult.on("click", function(){
			this.addText(" * ");
		}, this);

		var btn_div = new Ext.Button({
			text: "/",
			width: ancho_boton
		});

		btn_div.on("click", function(){
			this.addText(" / ");
		}, this);
		
		// PARENTESIS
		var btn_par_op = new Ext.Button({
			text: "(",
			width: ancho_boton
		});

		btn_par_op.on("click", function(){
			this.addText(" ( ", this);	
		}, this);

		var btn_par_cl = new Ext.Button({
			text: ")",
			width: ancho_boton
		});

		btn_par_cl.on("click", function(){
			this.addText(" ) ", this);	
		}, this);

		var btn_comma = new Ext.Button({
			text: ",",
			width: ancho_boton
		});

		btn_comma.on("click", function(){
			this.addText(", ");
		}, this);
		
		// campo para adicionar un valor constante
		var lbl_const = new Ext.form.Label({
			text: "Constante: "
		}); 

		var txt_const = new Ext.form.TextField({
			width: 80
		});		

		var btn_const = new Ext.Button({
			icon: cgss.UI.iconos.flecha_abajo
		});


		var constant_delimiter_open = config.constant_delimiter_open || "[";
		var constant_delimiter_close = config.constant_delimiter_close || "]";

		btn_const.on("click", function(){
			var val = txt_const.getValue();
			if (Ext.isEmpty(val)) { return false; }
			this.addText(constant_delimiter_open + val + constant_delimiter_close);
		}, this);

		var btn_back = new Ext.Button({
			text: "Retroceder"
		});

		btn_back.on("click", function(){
			arr_textadded.pop();
			arrValueAdded.pop();
			this.setValue(arr_textadded.join(""));
		}, this);
		
		var btn_clear = new Ext.Button({
			text: "Borrar"
		});
					
		btn_clear.on("click",function(){
			this.clearText(this);
		},this);

		
		var items_toolbar = [];

		if (Ext.isDefined(config.components)){
			var components = config.components;
			for ( var i = 0; i < components.length; i++ ){
				items_toolbar.push( components[ i ] );
			}
			items_toolbar.push("-");			
		}

		if (!Ext.isDefined(config.allowComparasions) || config.allowComparasions){

			if (!Ext.isDefined(config.allowEqual) || config.allowEqual){
				items_toolbar.push(btn_eq);
			}

			if (!Ext.isDefined(config.allowLessThan) || config.allowLessThan){
				items_toolbar.push(btn_lt);
			}

			if (!Ext.isDefined(config.allowGreaterThan) || config.allowGreaterThan){
				items_toolbar.push(btn_gt);
			}

			if (!Ext.isDefined(config.allowLessEqualThan) || config.allowLessEqualThan){
				items_toolbar.push(btn_lte);
			}

			if (!Ext.isDefined(config.allowGreaterEqualThan) || config.allowGreaterEqualThan){
				items_toolbar.push(btn_gte);
			}

			if (!Ext.isDefined(config.allowDifferent) || config.allowDifferent){
				items_toolbar.push(btn_dif);
			}
				
			items_toolbar.push("-");
		}
		
		if (!Ext.isDefined(config.allowLogics) || config.allowLogics){

			if (!Ext.isDefined(config.allowAnd) || config.allowAnd){
				items_toolbar.push(btn_y);
			}

			if (!Ext.isDefined(config.allowOr) || config.allowOr){
				items_toolbar.push(btn_o);
			}
			
			items_toolbar.push("-");
		}	
		

		if (!Ext.isDefined(config.allowSigns) || config.allowSigns){

			if (!Ext.isDefined(config.allowPlus) || config.allowPlus){
				items_toolbar.push(btn_plus);
			}

			if (!Ext.isDefined(config.allowMinus) || config.allowMinus){
				items_toolbar.push(btn_minus);
			}

			if (!Ext.isDefined(config.allowMultiply) || config.allowMultiply){
				items_toolbar.push(btn_mult);
			}

			if (!Ext.isDefined(config.allowDivide) || config.allowDivide){
				items_toolbar.push(btn_div);
			}

			items_toolbar.push("-");
		}

		if (!Ext.isDefined(config.allowParentheses) || config.allowParentheses){

			if (!Ext.isDefined(config.allowParOpen) || config.allowParOpen){
				items_toolbar.push(btn_par_op);
			}

			if (!Ext.isDefined(config.allowParClose) || config.allowParClose){
				items_toolbar.push(btn_par_cl);
			}
			items_toolbar.push("-");
		}

		if (!Ext.isDefined(config.allowComma) || config.allowComma){
			items_toolbar.push(btn_comma);
			items_toolbar.push("-");	
		}	

		if (!Ext.isDefined(config.allowConstant) || config.allowConstant){
			items_toolbar.push(lbl_const);
			items_toolbar.push(txt_const);
			items_toolbar.push(btn_const);
			items_toolbar.push("-");	
		}
		
		items_toolbar.push("->");

		items_toolbar.push(btn_back);
		items_toolbar.push(btn_clear);
		
		var toolbar = new Ext.Toolbar({
			items: items_toolbar
		});
		
		var text = new Ext.form.TextArea({
			style: "border-color: #fff;",
			readOnly: true			
		});			

		this.tbar = toolbar;	
		this.items = text;

		var conf = {
			layout: "fit"
		};

		if (config.title){
			conf.title = config.title;
		}

		config = Ext.apply(conf, config || {});
			
		Ext.cgss.Expression.superclass.constructor.call(this, config);
	}
});

    
