﻿
// Harita kontrollerinin yönetilmesini sağlar
function MapControlsManager(mapOfControls, removeDefaultNavigationControl) {

    // Kontorlleri yönetilecek harita
    var map = mapOfControls;
    var toolBar = null;
    
    if (removeDefaultNavigationControl) {
        map.removeControl(map.controls[1]);

        map.controls[0].wheelChange(null, 1);
    }

    mapManager.registerEvent("removelayer", onLayerRemoved);


    // Tool bar aracılığıyla kullanılacak kontroller
    var toolBarControls = [];

    // Tool bar aracılığıyla kullanılacak kontrollerin cursorları
    var controlCursors = [];
    // Harita standart kontrolleri ( Layerswitcher gibi )
    var standartControls = [];

    // Tool seçili olduğunda haritada gösterilecek bilgi notu
    var toolNotificationMessages = [];

    function onLayerRemoved(evt) {
        var selectByPolygonControl = toolBarControls['selectByPoint'];
        if (selectByPolygonControl != null) {
            if (arrayContains(selectByPolygonControl.layers, evt.layer))
                removeFromArray(selectByPolygonControl.layers, evt.layer);
        }
    }


    this.setToolbar = function(toolbarId) { toolBar = toolbarId; }

    // Kullanımda olan harita tool unu döndürür
    // returns  : Kullanımda olan harita tool u
    this.getActiveMapControl = function() {
        for (var index in toolBarControls) {
            if (toolBarControls[index].active)
                return index;
        }

        for (var index in standartControls) {
            if (standartControls[index].active)
                return index;
        }
    }

    // Kullanımda olan harita tool unu döndürür
    // returns  : Kullanımda olan harita tool u
    function getActiveMapControl() {
        for (var index in toolBarControls) {
            if (toolBarControls[index].active)
                return index;
        }

        for (var index in standartControls) {
            if (standartControls[index].active)
                return index;
        }
    }


    // Harita toolarından key i verilen tool aktive diğerlerini deaktive eder
    // uniqueControlKey     : Aktive edilecek tool un key i
    // returns              : void
    this.setMapControl = function(uniqueControlKey) {

        // Toolbar kontrolleri arasında dolanıp verilen
        // key e sahip kontrolü aktive ediyorum. Diğerlerini deaktive ediyorum
        for (var controlKey in toolBarControls) {
            if (controlKey.toString() == uniqueControlKey.toString()) {
                toolBarControls[controlKey].activate();
                toolBarControls[controlKey].map.div.style.cursor =  controlCursors[uniqueControlKey] ;
                var toolBarButtons = $find(toolBar).get_items();
                for (var index in toolBarButtons._array) {
                    if (toolBarButtons._array[index]._attributes != undefined) {
                        if (toolBarButtons._array[index]._attributes._data.Command == controlKey) {
                            toolBarButtons._array[index]._setChecked(true);

                        }
                    }
                }

                setToolNotification();
            }
            else {
                toolBarControls[controlKey].deactivate();
            }
        }
    }

    function setToolNotification() {
        if (standartControls['information'] != null)
            standartControls['information'].setMessage(toolNotificationMessages[getActiveMapControl()]);
    }

    // Verilen kontrolü saklar ya da günceller
    // uniqueControlKey : Eklenecek kontrole ait unique key
    // isToolBarControl : Verilen kontrol toolbar kontrol ü mü
    // controlToAdd     : Saklanacak ya da güncellenecek kontrol
    // returns          : void
    this.assignControl = function(uniqueControlKey, isToolBarControl, controlToAdd, notificationMessage, cursor) {
        if (isToolBarControl)
            toolBarControls[uniqueControlKey] = controlToAdd;
        else
            standartControls[uniqueControlKey] = controlToAdd;

        if (map.getControl(uniqueControlKey) == null)
            map.addControl(controlToAdd);

        if (notificationMessage != null)
            toolNotificationMessages[uniqueControlKey] = notificationMessage;

        controlCursors[uniqueControlKey] = cursor;
    }


    // Verilen kontrolü haritadan ve kontrol listelerinden siler
    // uniqueControlKey : Kaldırılacak kontrole ait key
    // returns          : void
    this.removeControl = function(uniqueControlKey) {
        var controlToRemove = this.getControl(uniqueControlKey);
        if (controlToRemove != null) {
            map.removeControl(controlToRemove);
            removeFromArray(toolBarControls, controlToRemove);
            removeFromArray(standartControls, controlToRemove);
        }
    }


    // Verilen key e sahip kontrol ü döndürür
    // uniqueControlKey : Bulunması istenen kontrolün key i
    // returns          : Verilen key e sahip kontrol. 
    //                    Eğer kontrol bulunmazsa null
    this.getControl = function(uniqueControlKey) {

        // Kontrolü toolbar kontrolleri arasında arıyorum. Bulunursa direk geri döndürüyorum        
        for (var controlKey in toolBarControls) {
            if (controlKey.toString() == uniqueControlKey.toString())
                return toolBarControls[controlKey]
        }

        // Kontoller toolbar kontrolleri arasında bulunamazsa
        // standart kontroller içerisinde arıyorum.
        // Bulunursa onu döndürüyorum
        for (var controlKey in standartControls) {
            if (controlKey.toString() == uniqueControlKey.toString())
                return standartControls[controlKey];
        }

        // Verilen key e sahip kontrol bulunamazsa null dönüyorum
        return null;
    }

    this.getControlCursor = function(uniqueControlKey) {
        return controlCursors[uniqueControlKey];
    }
}


