/*
 * Copyright 2017-present Open Networking Laboratory
 *
 * 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.
 */

/*
 ONOS GUI -- Topology Background Module.
 Module that maintains the map and sprite layers
 */

(function () {

    var $log;

    var instance;

    function getZoom(z) {
        var u = z.usr,
            c = z.cfg;
        return (u && !u.useCfg) ? u : c;
    }

    // returns the pan (offset) values as an array [x, y]
    function zoomPan(z) {
        var zoom = getZoom(z);
        return [zoom.offsetX, zoom.offsetY];
    }

    // returns the scale value
    function zoomScale(z) {
        var zoom = getZoom(z);
        return zoom.scale;
    }

    angular.module('ovTopo2')
        .factory('Topo2BackgroundService', [
            '$log', 'Topo2ViewController', 'Topo2SpriteLayerService', 'Topo2MapService',
            'Topo2MapConfigService', 'Topo2ZoomService',
            function (_$log_, ViewController, t2sls, t2ms, t2mcs, t2zs) {

                $log = _$log_;

                var BackgroundView = ViewController.extend({

                    id: 'topo2-background',
                    displayName: 'Background',

                    init: function () {
                        instance = this;
                        this.appendElement('#topo2-zoomlayer', 'g');
                        t2sls.init();
                        t2ms.init();
                        this.zoomer = t2zs.getZoomer();
                    },
                    addLayout: function (data) {

                        this.background = data;
                        this.bgType = data.bgType;
                        this.zoomData = data.bgZoom;

                        var _this = this,
                            pan = zoomPan(this.zoomData),
                            scale = zoomScale(this.zoomData);

                        if (this.bgType === 'geo') {

                            // Hide Sprite Layer and show Map
                            t2sls.hide();
                            t2ms.show();

                            t2ms.setUpMap(data.bgId, data.bgFilePath)
                            .then(function (proj) {
                                t2mcs.projection(proj);
                                $log.debug('** We installed the projection:', proj);
                                _this.region.loaded('bgRendered', true);
                                t2zs.panAndZoom(pan, scale, 1000);
                            });
                        } else if (this.bgType === 'grid') {

                            // Hide Sprite Layer and show Map
                            t2ms.hide();
                            t2sls.show();

                            t2sls.loadLayout(data.bgId).then(function (spriteLayout) {
                                _this.background.layout = spriteLayout;
                                _this.region.loaded('bgRendered', true);
                                t2zs.panAndZoom(pan, scale, 1000);
                            });
                        } else {
                            // No background type - Tell the region the background is ready for placing nodes
                            t2ms.hide();
                            t2sls.hide();
                            // _this.region.loaded('bgRendered', true);
                            // t2zs.panAndZoom(pan, _this.background.bgZoomScale, 1000);
                        }
                    },
                    getBackgroundType: function () {
                        return this.bgType;
                    },
                    resetZoom: function () {
                        t2zs.getZoomer().reset();
                    }
                });

                return instance || new BackgroundView();
            }
        ]);
})();
@@ -782,20 +782,28 @@
 etZoom(z
+, useCfg
 ) %7B%0A
-
         
@@ -870,16 +870,27 @@
 u.useCfg
+ && !useCfg
 ) ? u : 
@@ -979,16 +979,24 @@
 oomPan(z
+, useCfg
 ) %7B%0A    
@@ -1011,32 +1011,40 @@
 zoom = getZoom(z
+, useCfg
 );%0A        retur
@@ -1137,16 +1137,24 @@
 mScale(z
+, useCfg
 ) %7B%0A    
@@ -1177,16 +1177,24 @@
 etZoom(z
+, useCfg
 );%0A     
@@ -4177,31 +4177,129 @@
     
-t2zs.getZoomer().reset(
+var pan = zoomPan(this.zoomData, true);%0A                        t2zs.panAndZoom(pan, zoomScale(this.zoomData, true), 1000
 );%0A 

Reset Pan and zoom to user defined or configured location