/* global msGMLOutputFuel */
goog.require('ngeo.WfsPermalink');
goog.require('ol.format.filter');
goog.require('ngeo.test.data.msGMLOutputFuel');

describe('ngeo.WfsPermalink', () => {

  let ngeoWfsPermalink;
  let ngeoQueryResult;

  beforeEach(() => {
    module('ngeo', ($provide) => {
      $provide.value('ngeoWfsPermalinkOptions', {
        url: 'https://geomapfish-demo.camptocamp.net/2.1/wsgi/mapserv_proxy',
        wfsTypes: [{featureType: 'fuel'}, {featureType: 'highway'}],
        defaultFeatureNS: 'http://mapserver.gis.umn.edu/mapserver',
        defaultFeaturePrefix: 'ms'
      });
    });

    inject(($injector) => {
      ngeoWfsPermalink = $injector.get('ngeoWfsPermalink');
      ngeoQueryResult = $injector.get('ngeoQueryResult');
    });
  });

  it('creates a service', () => {
    expect(ngeoWfsPermalink instanceof ngeo.WfsPermalink).toBe(true);
  });

  describe('#issue', () => {

    let $httpBackend;
    let map;

    beforeEach(() => {
      const url = 'https://geomapfish-demo.camptocamp.net/2.1/wsgi/mapserv_proxy';
      inject(($injector) => {
        $httpBackend = $injector.get('$httpBackend');
        $httpBackend.when('POST', url).respond(msGMLOutputFuel);
        $httpBackend = $injector.get('$httpBackend');
      });

      const projection = ol.proj.get('EPSG:21781');
      projection.setExtent([485869.5728, 76443.1884, 837076.5648, 299941.7864]);

      map = new ol.Map({
        layers: [],
        view: new ol.View({
          projection,
          resolutions: [200, 100, 50, 20, 10, 5, 2.5, 2, 1, 0.5],
          center: [537635, 152640],
          zoom: 0
        })
      });
    });

    afterEach(() => {
      $httpBackend.verifyNoOutstandingExpectation();
      $httpBackend.verifyNoOutstandingRequest();
    });

    it('makes a query and adds the result', () => {
      const queryData = {
        'wfsType': 'fuel',
        'showFeatures': true,
        'filterGroups': [
          {
            'filters': [
              {'property': 'osm_id', 'condition': '1420918679'}
            ]
          }
        ]
      };

      ngeoWfsPermalink.issue(queryData, map);
      $httpBackend.flush();
      expect(ngeoQueryResult.sources[ngeoQueryResult.sources.length - 1].features.length).toBe(1);
      ngeoWfsPermalink.clear();
      expect(ngeoQueryResult.total).toBe(0);
    });
  });

  describe('#createFilters_', () => {
    const expectFiltersToEqual = function(filter1, filter2) {
      expect(filter1.constructor).toBe(filter2.constructor, 'same filter type');
      if (filter1 instanceof ol.format.filter.LogicalBinary) {
        expectFiltersToEqual(filter1.conditionA, filter2.conditionA);
        expectFiltersToEqual(filter1.conditionB, filter2.conditionB);
      } else {
        expect(filter1 instanceof ol.format.filter.EqualTo);
        expect(filter1.propertyName).toBe(filter2.propertyName);
        expect(filter1.expression).toBe(filter2.expression);
      }
    };

    it('creates filters', () => {
      const queryData = {
        'wfsType': 'fuel',
        'filterGroups': [
          {
            'filters': [
              {'property': 'osm_id', 'condition': '12345'},
              {'property': 'type', 'condition': ['diesel', 'gas']}
            ]
          },
          {
            'filters': [
              {'property': 'payment', 'condition': ['card', 'cash']}
            ]
          },
          {
            'filters': [
              {'property': 'open_7_24', 'condition': '1'}
            ]
          }
        ]
      };
      const f = ol.format.filter;
      const expectedFilters = f.or(
          f.or(
            f.and(
                f.equalTo('osm_id', '12345'),
                f.or(
                  f.equalTo('type', 'diesel'),
                  f.equalTo('type', 'gas')
                )
            ),
            f.or(
              f.equalTo('payment', 'card'),
              f.equalTo('payment', 'cash')
            )
          ),
          f.equalTo('open_7_24', '1')
      );
      expectFiltersToEqual(
          ngeoWfsPermalink.createFilters_(queryData['filterGroups']),
          expectedFilters
      );
    });

    it('handles 0 filter groups', () => {
      expect(ngeoWfsPermalink.createFilters_([])).toBe(null);
    });
  });

});

@@ -87,24 +87,70 @@
 t.filter');%0A
+goog.require('ol.format.filter.LogicalNary');%0A
 goog.require
@@ -2629,11 +2629,9 @@
 ical
-Bin
+N
 ary)
@@ -2679,17 +2679,24 @@
 ondition
-A
+s.length
 , filter
@@ -2710,12 +2710,83 @@
 tion
-A
+s.length
 );%0A
+        for (let i = 0; i %3C filter1.conditions.length; ++i) %7B%0A  
     
@@ -2827,17 +2827,20 @@
 ondition
-B
+s%5Bi%5D
 , filter
@@ -2854,11 +2854,24 @@
 tion
-B);
+s%5Bi%5D);%0A        %7D
 %0A   

Fix binary logical filter