﻿/** @class Dialog box for defining smart layers
@constructor
@param {object} config
@config {TNRIS.ShapeLayer} layer A layer to query against
@config {TNRIS.SmartLayer} smartLayer an existing smart layer to edit
@config {function} onSuccess a call back to invoke after completion.  Passes the smartlayer.
*/
TNRIS.SmartLayerWindow = function(config) {
    /** default button click handlers */
    this.handler = this.onButtonClk;
    
    /** default button handler scope */
    this.scope = this;
    
    var operatorStore = new Ext.data.SimpleStore({
        fields: ['label', 'value'],
        data: [['=', 'equal'],
               ['>=', 'greaterThan'],
               ['<=', 'lessThan']]
    });
        
    var fieldStore = new Ext.data.SimpleStore({
        fields: ['label', 'value'],
        data: [['BTID', 'BTID'],
               ['Year', 'Year'],
               ['Name', 'Name']]
    });

    this.queryFieldsetConfig = 
            {
                xtype:"fieldset",
                autoHeight:true,
                //autoWidth: true,
                cls: 'queryFieldset',
                items:
                        [{
                            xtype:"combo",
                            name:"combofield",
                            hiddenName:"combofield",
                            mode: 'local',
                            hideLabel: true,
                            forceSelection: true,
                            emptyText: 'Select a Field',
                            editable: false,
                            triggerAction: 'all',
                            store: fieldStore,
                            displayField: 'label',
                            valueField: 'value',
                            queryMember: 'fieldName' 
                      },{
                            xtype:"combo",
                            name:"combooperand",
                            hiddenName:"combooperand",
                            forceSelection: true,
                            editable: false,
                            triggerAction: 'all',
                            hideLabel: true,
                            mode: 'local',
                            store: operatorStore,
                            displayField: 'label',
                            valueField: 'value',
                            value: 'equal',
                            width: 40,
                            queryMember: 'operator' 
                      },{
                            xtype:"textfield",
                            hideLabel: true,
                            emptyText: 'Enter a value',
                            name:"textvalue",
                            queryMember: 'fieldValue' 
                      },{
                            xtype:"button",
                            text: 'add',
                            cls: 'queryButton',
                            name:"addQueryBtn",
                            queryMember: 'addQueryBtn',
                            scope: this,
                            handler: this.onAddQuery
                      },{
                            xtype:"button",
                            text: 'remove',
                            cls: 'queryButton',
                            name:"removeQueryBtn",
                            queryMember: 'removeQueryBtn',
                            scope: this,
                            handler: this.onRemoveQuery
                      }
                    ]
            };
    
    Ext.apply(this, config);

    this.form = new Ext.FormPanel({
        defaultType: 'textfield',
        bodyStyle: {'background-color': 'transparent'},
        autoScroll: true,
        items: 
            {
                id: 'nameTxt',
                fieldLabel: 'Layer Name',
                emptyText: 'Enter a Title',
                width: 300,
                allowBlank: false
            }
    });
    
    TNRIS.SmartLayerWindow.superclass.constructor.call(this, {
        title: 'Smart Layer based on "' + this.layer.name() + '"',
        id: 'smartLayerWnd',
        items: this.form,
        layout: 'fit',
        width: 600,
        height: 400,
        plain: true,
        buttonAlign: 'center',
        buttons: [
            {
                id: 'okBtn',
                text: 'Ok',
                handler: this.handler,
                scope: this.scope
            },{
                id: 'cancelBtn',
                text: 'Cancel',
                handler: this.handler,
                scope: this.scope
            }
        ]
    });
    
    this.form.add(new Ext.form.FieldSet(this.queryFieldsetConfig));
    this.form.doLayout();
    var firstRemoveButton = this.form.find('queryMember', 'removeQueryBtn').first();
    firstRemoveButton.disable();
};

Ext.extend(TNRIS.SmartLayerWindow, Ext.Window,
    /** @scope TNRIS.SmartLayerWindow */
    {
    onButtonClk: function(btn, e) {
        if (btn.id == 'okBtn') {
            // TODO: create a shapelayer if necessary, otherwise update the existing shapelayer
            var _params = [];
            this.form.findByType('fieldset').each(function(fs) {
                var nameField = fs.find('queryMember', 'fieldName').first();
                var operatorField = fs.find('queryMember', 'operator').first();
                var valueField = fs.find('queryMember', 'fieldValue').first();
                _params.push({field: nameField.getValue(), operator: operatorField.getValue(), value: valueField.getValue()});
            });
            
            if (null == this.smartLayer) {
                this.smartLayer = new TNRIS.SmartLayer({layer: this.layer, params: _params});
            } else {
                this.smartLayer.params = _params;
            }
            
            var layerName = this.form.findById('nameTxt').getValue();
            if (layerName == null || layerName == "" ) {
                layerName = this.layer.name() + " (Smart Layer)";
            }
            
            this.smartLayer.name(layerName);
            this.onSuccess && this.onSuccess(this.smartLayer);
        }
        this.close();
    },
    
    onAddQuery: function(btn, e) {
        this.form.add(new Ext.form.FieldSet(this.queryFieldsetConfig));
        this.form.doLayout();
    },
    
    onRemoveQuery: function(btn, e) {
        // HACK - this doesn't work because btn is an element - not a button 
        // var fieldset = btn.findParentByType('fieldset');
        var fieldset = btn.ownerCt;
        this.form.remove(fieldset);
    }
});

if (typeof(Sys) !== "undefined") { Sys.Application.notifyScriptLoaded(); }
