var FilterGroupConditions = {
    AND: 1,
    OR: 2
};

var FilterGroup = function(groupName, condition) {
    this.init(groupName, condition);
};

FilterGroup.prototype = {
    filters: {},
    condition: FilterGroupConditions.AND,
	
    init: function(groupName, condition) {
        this.filters = {};
        this.groupName = groupName;
        this.condition = condition || FilterGroupConditions.AND;
    },
    addFilter: function(filterType, filterCtrlId, filter) {
        this.filters[filterCtrlId] = {
            type: filterType,
            filterCtrlId: filterCtrlId,
            filter: filter
        };
    },
    activateFilter: function(filterCtrlId) {
        var flt = this.getFilter(filterCtrlId);
        if(flt) flt.activate();
    },
    deactivateFilter: function(filterName) {
        var flt = this.getFilter(filterName);
        if(flt) flt.deactivate();
    },
    setFilterValue: function(filterName, filterValue) {
        var flt = this.getFilter(filterName);
        if(flt) flt.setValue(filterValue);
    },
    getFilterValue: function(filterName, filterValue) {
        var flt = this.getFilter(filterName);
        if(flt) {
            return flt.getValue(filterValue);
        } else {
            return null;
        }
    },
    setSelectedFilterValue: function(filterName, filterValue) {
        var flt = this.getFilter(filterName);
        if(flt) flt.setSelectedValue(filterValue);
    },
    setSelectedFilterSubValue: function(filterName, filterValue) {
        var flt = this.getFilter(filterName);
        if(flt) flt.setSelectedSubValue(filterValue);
    },
    disableFilters: function() {
        for(var filterName in this.filters) {
            var flt = this.getFilter(filterName);
            if(flt) flt.disable();
        }
    },
    disableEmptyFilters: function() {
        for(var filterName in this.filters) {
            var filterCtrlId = this.filters[filterName].filterCtrlId;
            var flt = this.getFilter(filterName);

            if(flt) {
                if(!flt.hasMatches())
                    FilterManager.disableFilter(filterCtrlId);
                else
                    FilterManager.enableFilter(filterCtrlId);
            }

            if (flt.subMatches) {
            
                jQuery("#"+filterCtrlId+"_sub INPUT[type='checkbox']").attr("checked", false);
                //jQuery("#"+filterCtrlId+"_sub INPUT[type='checkbox']").attr("disabled", true);
                var subID;
                var numSub = 0;
                for (subID in flt.subMatches) {
                    if (flt.getSubMatches(subID)) {
                        jQuery("#"+subID).attr('checked', true);
                        numSub++;
                        //jQuery("#"+subID).removeAttr("disabled");
                    }
                    //Logger.debug('*********SubFilter: getSubMatches: '+subID+' ' +flt.getSubMatches(subID));
                }
                if (numSub == 1) {
                    jQuery("#"+subID).attr("disabled", true);
                }
            }

        }
    },
    enable: function() {
        for(var filterName in this.filters)
            this.enableFilter(filterName);
    },
    enableFilter: function(filterName) {
        var flt = this.getFilter(filterName);
        if(flt) flt.enable();
    },
    disableFilter: function(filterName) {
        var flt = this.getFilter(filterName);
        if(flt) flt.disable();
    },
    /**
	 * Applico i filtri del gruppo alla riga corrente del resultset
	 */
    apply: function(rset) {

        var groupActive = false, match, filter;
		
        switch(this.condition) {
            case FilterGroupConditions.AND:
                match = true;
                break;
            case FilterGroupConditions.OR:
                match = false;
        }

        for(var fltId in this.filters) {
		
            filter = this.getFilter(fltId);
			
            if(filter.isActive()) {
                //Logger.info('ECTRL: FilterGroup.apply ID: '+ fltId + ' - Active:' +filter.isActive());
                groupActive = true;
				
                if(this.condition == FilterGroupConditions.AND) {
                    match = match && filter.apply(rset);
                    if(!match)
                        return false;
                }
                else if(this.condition == FilterGroupConditions.OR) {
                    match = match || filter.apply(rset);
                    if(match)
                        return true;
                }
				
            }

        }

        return (groupActive ? match : true);
    },
	
    evaluateMatch: function(rset) {
        var match = false;
        for(var fltId in this.filters) {
            filter = this.getFilter(fltId);
            match = filter.evaluateMatch(rset) || match;
        }
        return match;
    },
    resetMatch: function() {
        for(var fltId in this.filters) {
            filter = this.getFilter(fltId);
            filter.resetMatch();
        }
    },
    isActive: function() {
        for(var fltId in this.filters) {
            filter = this.getFilter(fltId);
            if(filter.isActive())
                return true;
        }
        return false;
    },
    isEnabled: function() {
        for(var fltId in this.filters) {
            filter = this.getFilter(fltId);
            if(filter.isEnabled())
                return true;
        }
        return false;
    },
    getFilter: function(filterName) {
        if(this.filters[filterName])
            return this.filters[filterName].filter;
        return null;
    },
    getFilters: function() {
        return this.filters;
    },
    toString: function() {
        var str = 'NAME:: ' + this.groupName + '\n';
        for(var filterName in this.filters) {
            var fltDef = this.filters[filterName];
            str += '\tfilterCtrlId: ' + fltDef.filterCtrlId + '\n\tfilterType: ' + fltDef.filterType + '\n';
        }
        return str;
    }
};