/**
 * Copyright 2019 interactive instruments GmbH
 *
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
 */
package de.ii.ldproxy.ogcapi.filter;

import de.ii.ldproxy.ogcapi.domain.OgcApiDatasetData;
import de.ii.ldproxy.wfs3.oas30.OpenApiExtension;
import de.ii.xtraplatform.feature.transformer.api.FeatureTypeConfiguration;
import io.swagger.v3.oas.models.PathItem;
import io.swagger.v3.oas.models.media.ArraySchema;
import io.swagger.v3.oas.models.media.Schema;
import io.swagger.v3.oas.models.parameters.Parameter;
import org.apache.felix.ipojo.annotations.Component;
import org.apache.felix.ipojo.annotations.Instantiate;
import org.apache.felix.ipojo.annotations.Provides;
import io.swagger.v3.oas.models.OpenAPI;

import java.util.Comparator;
import java.util.Objects;

@Component
@Provides
@Instantiate
public class OpenApiFilter implements OpenApiExtension {

    @Override
    public int getSortPriority() {
        return 500;
    }

    @Override
    public OpenAPI process(OpenAPI openAPI, OgcApiDatasetData apiData) {
        if (isEnabledForApi(apiData)) {
            Parameter filter = new Parameter()
                    .name("filter")
                    .in("query")
                    .description("Filter the collection using CQL query specifiend in the parameter value.")
                    .required(false)
                    .schema(new ArraySchema().items(new Schema().type("String")))
                    .style(Parameter.StyleEnum.FORM)
                    .explode(false);
            openAPI.getComponents().addParameters("filter", filter);

            apiData.getFeatureTypes()
                    .values()
                    .stream()
                    .sorted(Comparator.comparing(FeatureTypeConfiguration::getId))
                    .filter(ft -> apiData.isFeatureTypeEnabled(ft.getId()))
                    .forEach(ft -> {
                        PathItem pathItem = openAPI.getPaths().get(String.format("/collections/%s/items", ft.getId()));
                        if (Objects.nonNull(pathItem)) {
                            pathItem.getGet().addParametersItem(new Parameter().$ref("#/components/parameters/filter"));
                        }
                    });
        }

        return openAPI;
    }

    @Override
    public boolean isEnabledForApi(OgcApiDatasetData apiData) {
        return isExtensionEnabled(apiData, FilterConfiguration.class);
    }
}

@@ -503,24 +503,23 @@
 .models.
-PathItem
+OpenAPI
 ;%0Aimport
@@ -548,25 +548,16 @@
 els.
-media.ArraySch
+PathIt
 em
-a
 ;%0Aim
@@ -592,16 +592,22 @@
 s.media.
+String
 Schema;%0A
@@ -824,49 +824,36 @@
 es;%0A
+%0A
 import 
-io.swagger.v3.oas.models.OpenAPI;%0A
+java.util.ArrayList;
 %0Aimp
@@ -878,16 +878,39 @@
 arator;%0A
+import java.util.List;%0A
 import j
@@ -1384,16 +1384,32 @@
 ter the 
+features in the 
 collecti
@@ -1421,28 +1421,28 @@
 ing 
-CQL
+the
  query 
-specifiend
+expression
  in 
@@ -1537,21 +1537,22 @@
 new 
-Array
+String
 Schema()
 .ite
@@ -1551,42 +1551,593 @@
 ma()
-.items(new Schema().type(%22String%22)
+)%0A                    .style(Parameter.StyleEnum.FORM)%0A                    .explode(false);%0A            openAPI.getComponents().addParameters(%22filter%22, filter);%0A            List%3CString%3E fEnum = new ArrayList%3C%3E();%0A            fEnum.add(%22cql-text%22);%0A            Parameter filterLang = new Parameter()%0A                    .name(%22filter-lang%22)%0A                    .in(%22query%22)%0A                    .description(%22Language of the query expression in the 'filter' parameter.%22)%0A                    .required(false)%0A                    .schema(new StringSchema()._enum(fEnum)._default(%22cql-text%22
 ))%0A 
@@ -2274,32 +2274,37 @@
 rameters(%22filter
+-lang
 %22, filter);%0A%0A   
@@ -2296,16 +2296,20 @@
 , filter
+Lang
 );%0A%0A    
@@ -2820,16 +2820,53 @@
 getGet()
+%0A                                    
 .addPara
@@ -2931,16 +2931,132 @@
 ilter%22))
+%0A                                    .addParametersItem(new Parameter().$ref(%22#/components/parameters/filter-lang%22))
 ;%0A      

add filter-lang / fix filter in OpenAPI definition