global.dashModule
    .controller('dashboardsViewCtrl', ['$scope', '$http', '$', '$rootScope', '$compile', '$translate', 'utilsService', dashboardsViewCtrl])
    .directive('dashboardsView', dashboardsViewDirective);

function dashboardsViewDirective() {
    return {
        restrict: 'E',
        templateUrl: 'dashboards/html/dashboardsViewTemplate'
    };
}

function dashboardsViewCtrl($scope, $http, $, $rootScope, $compile, $translate, utilsService) {
    $scope.initMyDashborad = initMyDashborad;
    $scope.gadgetsDirective = gadgetsDirective;
    $scope.showSelectDashborad = showSelectDashborad;
    $scope.getMyDashboard = getMyDashboard;
    $scope.getDashboradIcon = getDashboradIcon;
    $scope.showHideRemoveDialog = showHideRemoveDialog;
    $scope.removeDashboard = removeDashboard;
    $scope.isRemoveDialogVisible = false;
    $scope.isSelectDashboardVisible = false;
    $scope.allDashboards = [];
    $scope.dashboardInfo = {};
    $scope.gadgets = [];

    function initMyDashborad() {
        getUserDashboards();
        getMyDashboard();
    }

    function getUserDashboards() {
        $http({
            method: 'POST',
            url: 'dashboards/api/getUserDashboards'
        }).then(setUserDashboards, utilsService.onHttpError);
    }

    function setUserDashboards(response) {
        if (response.data) {
            $scope.allDashboards = response.data;
        }
    }

    function getDashboradFromStorage(){
        return (typeof (Storage) !== 'undefined' && localStorage.myDashboard) ? localStorage.myDashboard : 0;
    }

    function setDashboardInStorage(idDashboard){
        const idDashboardChecked = idDashboard || 0;

        if (typeof (Storage) !== 'undefined'){
            localStorage.myDashboard = idDashboardChecked;
        }
    }
    function getMyDashboard(idDashboard) {
        const myDashboard = idDashboard || getDashboradFromStorage(idDashboard);
        setDashboardInStorage(myDashboard);
        const params = $.param({
            myDashboard
        });
        $http({
            method: 'POST',
            url: 'dashboards/api/getMyDashboard',
            data: params
        }).then(showDashboard, utilsService.onHttpError);
    }

    function showDashboard(response) {
        if (response.data && response.data.info) {
            response.data.info.isOwner = parseInt(response.data.info.isOwner) === 1;
            $scope.dashboardInfo = response.data.info;
            $scope.gadgets = response.data.gadgets ? response.data.gadgets : [];
            $scope.isSelectDashboardVisible = false;
        }
    }

    function gadgetsDirective(gadget) {
        const directiveHtml = '<' + gadget.module + '></' + gadget.module + '>';
        const scope = $rootScope.$new();

        scope.gadget = gadget;
        const comp = $compile($(directiveHtml))(scope);

        $scope.$evalAsync(() => {
            $('#dashboard-gadget-' + gadget.idGadget).append(comp);
        });
    }

    function showSelectDashborad() {
        $scope.isSelectDashboardVisible = !$scope.isSelectDashboardVisible;
    }

    function getDashboradIcon(visibility){
        return visibility === 'private' ? 'close' : 'open';
    }

    function showHideRemoveDialog(){
        $scope.$evalAsync(() => {
            $scope.isRemoveDialogVisible = !$scope.isRemoveDialogVisible;
        });
    }

    function removeDashboard(idDashboard){
        const params = $.param({
            idDashboard
        });
        $http({
            method: 'POST',
            url: 'dashboards/api/removeDashboard',
            data: params
        }).then(showRemoveMessage, utilsService.onHttpError);
    }

    function showRemoveMessage(response){
        if (typeof response.data.messages !== 'undefined') {
            response.data.messages.forEach((messageObj) => {
                const translatedMessage = $translate.instant('dashboards.messages.' + messageObj.message);
                utilsService.displayMessage(messageObj.code, translatedMessage);
            });
            setDashboardInStorage(0);
            initMyDashborad();
        }
    }
}
