﻿mekkahExplorerApp.controller("mainCtrl", function ($scope, $http, $rootScope, mapService, widgetsManager, helper, $timeout) {
    $rootScope.appPath = window.appPath;
    
    $http.defaults.useXDomain = true;
    $http.defaults.withCredentials = true
    $scope.mapService = mapService;
    $scope.helper = helper;


    $http.post(window.appPath + 'Data/HashKey1?cv=' + window.cv).then(function (response1) {
        var config = JSON.parse(decodeURIComponent(atob(atob(atob(response1.data)))));
        $http.post(window.appPath + 'Data/HashKey2?cv=' + window.cv).then(function (response2) {
            var layersNameMapping = JSON.parse(decodeURIComponent(atob(atob(atob(response2.data)))));
            $rootScope.config = config;
            $rootScope.config.layersNameMapping = layersNameMapping.Rasters;
            Array.prototype.push.apply($rootScope.config.layersNameMapping, layersNameMapping.RastersPlan);
            Array.prototype.push.apply($rootScope.config.layersNameMapping, layersNameMapping.Layers);
			Array.prototype.push.apply($rootScope.config.layersNameMapping, layersNameMapping.RespLayer);
            Array.prototype.push.apply($rootScope.config.layersNameMapping, layersNameMapping.BasemapLayers);
            $scope.widgetsManager = widgetsManager;
            widgetsManager.menus = $rootScope.config.menus;
        }, function (response2) {
            console.error(response2);
        });
    }, function (response1) {
        console.error(response1);
        });


    //Mobile us
    var dragItem = document.querySelector("#toolsContainerRow1");
    var dragActive = false;

    window.addEventListener("touchstart", dragStart, false);
    window.addEventListener("touchend", dragEnd, false);
    window.addEventListener("touchmove", drag, false);
    function dragStart(e) {
        if (dragItem && (e.target === dragItem || e.target.parentNode === dragItem || e.target.parentNode.parentNode === dragItem)) {
            dragActive = true;

        }
    }
    function dragEnd(e) {
        dragActive = false;
    }
    function drag(e) {
        if (dragActive) {
            $scope.showMobileTools = true;
            if (e.type === "touchmove") {
                $scope.toolsContainerHeight = $rootScope.mobileHeight - e.touches[0].clientY;
            } else {
                $scope.toolsContainerHeight = $rootScope.mobileHeight - e.clientY;
            }
            if ($scope.toolsContainerHeight < 0) {
                $scope.toolsContainerHeight = $rootScope.toolsContainerMinHeight;
            }
        }
    }

    $scope.showMobileTools = true;
    $scope.toolsContainerHeight = -1;
    $scope.getToolsContainerStyle = function () {
        if ($scope.toolsContainerHeight < 0) {
            $scope.toolsContainerHeight = $rootScope.toolsContainerMaxHeight / 2;
        }

        if ($scope.toolsContainerHeight > $rootScope.toolsContainerMaxHeight) {
            $scope.toolsContainerHeight = $rootScope.toolsContainerMaxHeight + 0;
        }

        if ($scope.toolsContainerHeight < $rootScope.toolsContainerMinHeight) {
            $scope.toolsContainerHeight = $rootScope.toolsContainerMinHeight + 0;
        }

        if ($scope.showMobileTools)
            return { 'height': $scope.toolsContainerHeight };
        else
            return { 'height': 'unset' };
    }


    $scope.getMobileTabContentStyle = function () {
        if ($scope.showMobileTools === false) {
            return { 'height': 'unset', 'max-height': '0px' };
        }

        $rootScope.toolsContainer = document.querySelector("#toolsContainer");

        if ($rootScope.isMobile) {
            if (!$rootScope.toolsContainerRow1) {
                $rootScope.toolsContainerRow1 = document.querySelector("#toolsContainerRow1");
            }
            if (!$rootScope.toolsContainerRow2) {
                $rootScope.toolsContainerRow2 = document.querySelector("#toolsContainerRow2");
            }
            if ($rootScope.toolsContainerRow1 && $rootScope.toolsContainerRow2 && $rootScope.toolsContainer) {
                var rowsHeight = $rootScope.toolsContainerRow1.offsetHeight + $rootScope.toolsContainerRow2.offsetHeight;
                var height = ($rootScope.toolsContainer.offsetHeight - rowsHeight)
                return { 'max-height': height + 'px', 'height': height + 'px' };
            } else {
                return { 'max-height': '200px' };
            }
           

            
        } else {
            return { 'max-height': '200px' };
        }


        
    }
    $scope.calcMobileHeight = function () {
        var barHeight = 50;
        if (!dragItem) {
            dragItem = document.querySelector("#toolsContainerRow1");
        }
        if ($rootScope.firstBar && $rootScope.secondBar)
            barHeight = $rootScope.firstBar.offsetHeight + $rootScope.secondBar.offsetHeight;

        $rootScope.mobileHeight = window.innerHeight;
        $rootScope.mobileWidth = window.innerWidth;

        $rootScope.toolsContainerMaxHeight = $rootScope.mobileHeight - barHeight;


        if ($rootScope.isMobile) {
            $timeout(function () {
                $scope.calcMobileHeight();
            }, 100)
        }
    }
    $scope.calcMobileHeight()
    $scope.showHideMobileTools = function () {
        $scope.showMobileTools = !$scope.showMobileTools;
    }
    $scope.closeWidget = function (widget) {
        if (widget.confirmationBeforeClose) {
            bootbox.confirm({
                title: 'تأكيد إغلاق ',
                message: "هل أنت متأكد من إغلاق الأداة" + " <b><i>" + widget.title + "</i></b>؟",
                buttons: {
                    confirm: {
                        label: 'نعم',
                        className: 'btn-success'
                    },
                    cancel: {
                        label: 'لا',
                        className: 'btn-danger'
                    }
                },
                callback: function (result) {
                    if (result) {
                        if (widget.close)
                            widget.close();
                        widgetsManager.closeWidget(widget);
                    }
                }
            });
        } else {
            if (widget.close)
                widget.close();
            widgetsManager.closeWidget(widget);
        }
    }

});
