/*
 * Copyright (C) 2018 con terra GmbH (info@conterra.de)
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *         http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

class QueryToolController {
    activate(componentContext) {
        this._bundleContext = componentContext.getBundleContext();
        this.i18n = this._i18n.get().ui;
    }

    deactivate() {
        let registration = this._serviceregistration;

        // clear the reference
        this._serviceregistration = null;

        if (registration) {
            // call unregister
            registration.unregister();
        }
    }

    onQueryToolActivated(event) {
        let store = event.store;
        if (!store) {
            // ignore
            return;
        }
        let complexQuery = event.complexQuery;
        let tool = this.tool = event.tool;

        if (event.options.editable === true) {
            let widget = this._editableQueryBuilderWidgetFactory.getWidget(event._properties, this, tool);
            let serviceProperties = {
                "widgetRole": "editableQueryBuilderWidget"
            };
            let interfaces = ["dijit.Widget"];
            this._serviceregistration = this._bundleContext.registerService(interfaces, widget, serviceProperties);
        } else {
            if (complexQuery.geometry && this._queryBuilderProperties.useUserExtent) {
                let extent = this._mapWidgetModel.get("extent");
                complexQuery.geometry = {
                    $contains: extent
                };
            }
            let options = {};
            let count = event.options.count;
            if (count >= 0) {
                options.count = count;
            }
            options.ignoreCase = event.options.ignoreCase;
            options.locale = event.options.locale;
            options.sort = event.options.sort || [];

            this._queryController.query(store, complexQuery, options, tool);
        }
    }
}

module.exports = QueryToolController;
@@ -619,16 +619,60 @@
 e.%0A */%0A%0A
+import Extent from %22esri/geometry/Extent%22;%0A%0A
 class Qu
@@ -1821,12 +1821,60 @@
 etry
- && 
+) %7B%0A                let extent;%0A                if (
 this
@@ -1927,27 +1927,27 @@
             
-let
+   
  extent = th
@@ -1996,16 +1996,20 @@
         
+    
 complexQ
@@ -2046,16 +2046,20 @@
         
+   
+ 
 $contain
@@ -2076,34 +2076,309 @@
                 
+    %7D;%0A                %7D else if (!complexQuery.geometry.$contains.type) %7B%0A                    extent = new Extent(complexQuery.geometry.$contains);%0A                    complexQuery.geometry = %7B%0A                        $contains: extent%0A                    %7D;%0A                
 %7D
-;
 %0A            %7D%0A 

Fix query problem in case of geometry