/**
 * @ngdoc object
 * @name anol.layer.StaticGeoJSON
 *
 * @param {Object} options AnOl Layer options
 * @param {Object} options.olLayer Options for ol.layer.Vector
 * @param {Object} options.olLayer.source Options for ol.source.Vector
 * @param {Object} options.olLayer.source.url Url to GeoJSON
 * @param {String} options.olLayer.source.dataProjection Projection if GeoJSON
 *
 * @description
 * Inherits from {@link anol.layer.Layer anol.layer.Layer}.
 */

import FeatureLayer from './feature.js';

import GML2 from 'ol/format/GML2';
import Style from 'ol/style/Style';
import Stroke from 'ol/style/Stroke';
import Fill from 'ol/style/Fill';

import VectorSource from 'ol/source/Vector';


class GMLLayer extends FeatureLayer {

    constructor(_options) {
        if(_options === false) {
            super();
            return;
        } 
        // TODO use openlayers gml filter
        if (_options.filter) {
            var filter = '';
                
            if (!angular.isArray(_options.filter.literal)) {
                _options.filter.literal = [_options.filter.literal];                
            }

            angular.forEach(_options.filter.literal, function(literal) {
                filter += 
                        '<PropertyIsEqualTo>' +
                            '<PropertyName>' + _options.filter.propertyName +'</PropertyName>'+
                            '<Literal>'+ literal +'</Literal>'+
                        '</PropertyIsEqualTo>';
            })
            if (angular.isDefined(_options.filter.filterType)) {
                filter = 
                    '<Filter>'+
                        '<'+_options.filter.filterType + '>' +  
                            filter +
                        '</'+ _options.filter.filterType +'>'+
                    '</Filter>';
            } else {
                filter = '<Filter>'+ filter + '</Filter>';                
            }
            _options.olLayer.source.url = _options.olLayer.source.url + 'FILTER=' + filter; 
        }

        var defaults = {
            olLayer: {
                source: {
                    format: new GML2()
                },
                style: new Style({
                    stroke: new Stroke({
                        color: 'rgba(255,0,0,1)',
                        width: 1
                    }),
                    fill: new Fill({
                        color: 'rgba(255,0,0,0.3)'
                    })
                })
            }
        };
        var options = $.extend(true, {}, defaults, _options);
        super(options);
        this.method = options.method || 'GET';
        this.CLASS_NAME = 'anol.layer.GML';
    }

    _createSourceOptions(srcOptions) {
        var self = this;
        srcOptions.loader = function() {
            self.loader(
                srcOptions.url
            );
        };
        return super._createSourceOptions(srcOptions);
    }

    loader(url) {
        var self = this;
        $.ajax({
            method: this.method,
            url: url
        }).done(function(response) {
            self.responseHandler(response);
        });
    }
    responseHandler(response) {
        var self = this;
        var sourceFeatures = self.olLayer.getSource().getFeatures();
        for(var i = 0; i < sourceFeatures.length; i++) {
            self.olLayer.getSource().removeFeature(sourceFeatures[i]);
        }  

        var format = new GML2();
        var features = format.readFeatures(response);
        self.olLayer.getSource().addFeatures(features);
    }    
}

export default GMLLayer;

@@ -1962,63 +1962,24 @@
 rce.
-url = _options.olLayer.source.url + 'FILTER=' +
+filter =
  filter;
  %0A  
@@ -1974,17 +1974,16 @@
  filter;
- 
 %0A       
@@ -2800,16 +2800,54 @@
 ions.url
+, srcOptions.params, srcOptions.filter
 %0A       
@@ -2941,16 +2941,32 @@
 ader(url
+, params, filter
 ) %7B%0A    
@@ -2978,32 +2978,128 @@
 ar self = this;%0A
+        if (this.method === 'GET') %7B%0A            url = url + params + '&FILTER=' + filter; %0A    
         $.ajax(%7B
@@ -3111,16 +3111,20 @@
         
+ 
+   
 method: 
@@ -3148,16 +3148,20 @@
         
+    
 url: url
@@ -3169,16 +3169,20 @@
         
+   
+ 
 %7D).done(
@@ -3194,32 +3194,36 @@
 ion(response) %7B%0A
+    
             self
@@ -3242,32 +3242,36 @@
 dler(response);%0A
+    
         %7D);%0A    
@@ -3266,16 +3266,484 @@
     %7D);%0A
+        %7D else %7B%0A            var data = JSON.parse('%7B%22' + params.replace(/&/g, '%22,%22').replace(/=/g,'%22:%22') + '%22%7D', %0A                function(key, value) %7B return key===%22%22 ? value:decodeURIComponent(value) %7D)%0A            data.FILTER = filter;%0A            $.ajax(%7B%0A                method: this.method,%0A                url: url,%0A                data: data,%0A            %7D).done(function(response) %7B%0A                self.responseHandler(response);%0A            %7D);%0A        %7D%0A
     %7D%0A  

support wfs request with post