// $Id: controller.js 32761 2015-11-02 11:50:21Z schaap $

var cleanupControllerMod = angular.module('CleanupControllerMod', ['ui.bootstrap']);

cleanupControllerMod.controller('CleanupController', ['$scope', '$uibModal', '$http', '$q', 'dataService', function($scope, $uibModal, $http, $q, dataService) {
    var self = this;

    self.getTaskDataPath = function(task) {
        var defer = $q.defer();

        $http.get('/rest/tasks/' + task.id + '/datapath').success(function(result) {
            defer.resolve(result);
        }).error(function(result) {
            console.log("Error. Could get data path for task " + task.id + ", " + result);
            alert(result);
        });

        return defer.promise;
    };

    self.deleteSelectedTasksDataWithConfirmation = function() {
        var tasks = dataService.selected_task_ids.map(function(t_id) { return dataService.taskDict[t_id]; });
        self.deleteTasksDataWithConfirmation(tasks);
    }

    self.deleteTasksDataWithConfirmation = function(tasks) {
        du_results = [];
        for(var task of tasks) {
            dataService.getTaskDiskUsage(task).then(function(du_result) {
                if(du_result.found) {
                    du_results.push(du_result);

                    if(du_results.length == tasks.length) {
                        openDeleteConfirmationDialog(du_results);
                    }

                    console.log(du_results);
                } else {
                    alert(du_result.message);
                }
            });
        }
    };

    function deleteTaskData(task, delete_is, delete_cs, delete_uv, delete_im, delete_img, delete_pulp, delete_scratch) {
        var params = {delete_is:delete_is, delete_cs:delete_cs, delete_uv:delete_uv, delete_im:delete_im, delete_img:delete_img, delete_pulp:delete_pulp, delete_scratch:delete_scratch};
        $http.delete('/rest/tasks/' + task.id + '/cleanup', {data: params}).error(function(result) {
            console.log("Error. Could cleanup data for task " + task.id + ", " + result);
        }).success(function(result) {
            alert(result.message);
        });
    };

    function openDeleteConfirmationDialog(du_results) {
        var modalInstance = $uibModal.open({
            animation: false,
            template: '<div class="modal-header">\
                            <h3 class="modal-title">Are you sure?</h3>\
                        </div>\
                        <div class="modal-body">\
                            <p>This will delete all selected data in: \
                            <ul><li ng-repeat="path in paths">{{path}}</li></ul>\
                            <br>Are you sure?</p>\
                            <label ng-if="has_is" style="margin-left:24px">IS: {{amount_is}}<input style="margin-left:8px" type="checkbox" ng-model="$parent.delete_is"></label>\
                            <label ng-if="has_cs" style="margin-left:24px">CS: {{amount_cs}}<input style="margin-left:8px" type="checkbox" ng-model="$parent.delete_cs"></label>\
                            <label ng-if="has_uv" style="margin-left:24px">UV: {{amount_uv}}<input style="margin-left:8px" type="checkbox" ng-model="$parent.delete_uv"></label>\
                            <label ng-if="has_im" style="margin-left:24px">IM: {{amount_im}}<input style="margin-left:8px" type="checkbox" ng-model="$parent.delete_im"></label>\
                            <label ng-if="has_img" style="margin-left:24px">Images: {{amount_img}}<input style="margin-left:8px" type="checkbox" ng-model="$parent.delete_img"></label>\
                            <label ng-if="has_pulp" style="margin-left:24px">Pulp: {{amount_pulp}}<input style="margin-left:8px" type="checkbox" ng-model="$parent.delete_pulp"></label>\
                            <label ng-if="has_scratch" style="margin-left:24px">scratch<input style="margin-left:8px" type="checkbox" ng-model="$parent.delete_scratch"></label>\
                        </div>\
                        <div class="modal-footer">\
                            <button class="btn btn-primary" type="button" ng-click="ok()">OK</button>\
                            <button class="btn btn-warning" type="button" autofocus ng-click="cancel()">Cancel</button>\
                        </div>',
            controller: function ($scope, $uibModalInstance) {
                $scope.paths = du_results.map(function(r) { return r.task_directory.path; });
                $scope.has_is = false;
                $scope.has_cs = false;
                $scope.has_uv = false;
                $scope.has_im = false;
                $scope.has_img = false;
                $scope.has_pulp = false;
                $scope.has_scratch = false;
                $scope.amount_is = 0;
                $scope.amount_cs = 0;
                $scope.amount_uv = 0;
                $scope.amount_im = 0;
                $scope.amount_img = 0;
                $scope.amount_pulp = 0;

                for(var du_result of du_results) {
                    var path = du_result.task_directory.path;
                    var has_is = du_result.sub_directories.hasOwnProperty(path + '/is');
                    var has_cs = du_result.sub_directories.hasOwnProperty(path + '/cs');
                    var has_uv = du_result.sub_directories.hasOwnProperty(path + '/uv');
                    var has_im = du_result.sub_directories.hasOwnProperty(path + '/im');
                    var has_img = du_result.sub_directories.hasOwnProperty(path + '/img');
                    var has_pulp = du_result.sub_directories.hasOwnProperty(path + '/pulp');
                    var has_scratch = du_result.sub_directories.hasOwnProperty('scratch_paths');

                    $scope.has_is |= has_is;
                    $scope.has_cs |= has_cs;
                    $scope.has_uv |= has_uv;
                    $scope.has_im |= has_im;
                    $scope.has_img |= has_img;
                    $scope.has_pulp |= has_pulp;
                    $scope.has_scratch |= has_scratch;

                    $scope.amount_is += has_is ? du_result.sub_directories[path + '/is'].disk_usage : 0;
                    $scope.amount_cs += has_cs ? du_result.sub_directories[path + '/cs'].disk_usage : 0;
                    $scope.amount_uv += has_uv ? du_result.sub_directories[path + '/uv'].disk_usage : 0;
                    $scope.amount_im += has_im ? du_result.sub_directories[path + '/im'].disk_usage : 0;
                    $scope.amount_img += has_img ? du_result.sub_directories[path + '/img'].disk_usage : 0;
                    $scope.amount_pulp += has_pulp ? du_result.sub_directories[path + '/pulp'].disk_usage : 0;
                }

                $scope.amount_is = dataService.humanreadablesize($scope.amount_is);
                $scope.amount_cs = dataService.humanreadablesize($scope.amount_cs);
                $scope.amount_uv = dataService.humanreadablesize($scope.amount_uv);
                $scope.amount_im = dataService.humanreadablesize($scope.amount_im);
                $scope.amount_img = dataService.humanreadablesize($scope.amount_img);
                $scope.amount_pulp = dataService.humanreadablesize($scope.amount_pulp);

                $scope.delete_is = true;
                $scope.delete_cs = true;
                $scope.delete_uv = true;
                $scope.delete_im = true;
                $scope.delete_img = true;
                $scope.delete_pulp = true;
                $scope.delete_scratch = true;

                $scope.ok = function () {
                    $uibModalInstance.close();
                    for(var du_result of du_results) {
                        var task = du_result.task;
                        deleteTaskData(task, $scope.delete_is, $scope.delete_cs, $scope.delete_uv, $scope.delete_im, $scope.delete_img, $scope.delete_pulp, $scope.delete_scratch);
                    }
                };

                $scope.cancel = function () {
                    $uibModalInstance.dismiss('cancel');
                };
            }
        });
    };

    self.showAllProjectsDiskUsage = function() {
        self.showTaskDiskUsage(undefined);
    }

    self.showTaskDiskUsage = function(task) {
        var modalInstance = $uibModal.open({
            animation: false,
            template: '<div class="modal-header">\
                            <h3 class="modal-title">Disk usage</h3>\
                        </div>\
                        <div class="modal-body" style="text-align:right">\
                            <highchart id="chart_total_disk_usage" config="totalDiskUsageChartConfig" style="width: 960px; height: 120px; margin-bottom: 20px;" ></highchart>\
                            <hr>\
                            <highchart id="chart_disk_usage" config="diskUsageChartConfig" style="width: 960px; height: 720px;" ></highchart>\
                            <p>\
                              <span style="margin-right:50px">Last updated at: {{leastRecentCacheTimestamp | date }}</span>\
                              <button class="btn btn-primary glyphicon glyphicon-level-up" type="button" ng-click="up()" title="Up one level" ng-if="watchedObjectType!=\'projects\'"></button>\
                            </p>\
                        </div>\
                        <div class="modal-footer">\
                            <button class="btn btn-primary" type="button" autofocus ng-click="ok()">OK</button>\
                        </div>',
            controller: function ($scope, $uibModalInstance) {
                $scope.ok = function () {
                    $uibModalInstance.close();
                };

                const OBJECT_TYPE_TASK = 'task';
                const OBJECT_TYPE_PROJECT = 'project';
                const OBJECT_TYPE_PROJECTS = 'projects';
                $scope.watchedObjectType = OBJECT_TYPE_TASK;

                $scope.leastRecentCacheTimestamp = '';

                $scope.onPieClicked = function(event) {
                    switch($scope.watchedObjectType) {
                        case OBJECT_TYPE_PROJECT:
                            loadTaskDiskUsage(this.otdb_id);
                            break;
                        case OBJECT_TYPE_PROJECTS:
                            loadProjectDiskUsage(this.project_name);
                            break;
                    }
                };

                $scope.up = function () {
                    switch($scope.watchedObjectType) {
                        case OBJECT_TYPE_TASK:
                            loadProjectDiskUsage($scope.diskUsageChartSeries[0].project_name);
                            break;
                        case OBJECT_TYPE_PROJECT:
                            loadAllProjectsDiskUsage();
                            break;
                    }
                };

                $scope.diskUsageChartSeries = [{name:'Loading data...', data:[]}];

                $scope.diskUsageChartConfig = {
                    options: {
                        chart: {
                            type: 'pie',
                            animation: {
                                duration: 200
                            },
                            legend: {
                                enabled: false
                            }
                        },
                        legend: {
                            enabled: false
                        },
                        plotOptions: {
                            pie: {
                                allowPointSelect: true,
                                cursor: 'pointer',
                                dataLabels: {
                                    enabled: true
                                },
                                showInLegend: false
                            },
                            series: {
                                point: {
                                    events: {
                                        click: $scope.onPieClicked
                                    }
                                }
                            }
                        },
                        tooltip: {
                            headerFormat: '{series.name}<br/>',
                            pointFormat: '{point.name}: <b>{point.percentage:.1f}%</b>'
                        }
                    },
                    series: $scope.diskUsageChartSeries,
                    title: {
                        text: 'Loading data...'
                    },
                    credits: {
                        enabled: false
                    },
                    loading: false
                }

                $scope.totalDiskUsageChartSeries = [];

                var cep4storage_resource = dataService.resources.find(function(r) { return r.name == 'cep4storage'; });
                if(cep4storage_resource) {
                    $scope.totalDiskUsageChartSeries = [{name:'Free', data:[100.0*cep4storage_resource.available_capacity/cep4storage_resource.total_capacity], color:'#a3f75c'},
                                                        {name:'Used', data:[100.0*cep4storage_resource.used_capacity/cep4storage_resource.total_capacity], color:'#f45b5b'}];
                }

                $scope.totalDiskUsageChartConfig = {
                    options: {
                        chart: {
                            type: 'bar',
                            animation: {
                                duration: 200
                            },
                            legend: {
                                enabled: false
                            }
                        },
                        navigation: {
                            buttonOptions: {
                                enabled: false
                            }

                        },
                        plotOptions: {
                            bar: {
                                allowPointSelect: false,
                                cursor: 'pointer',
                                dataLabels: {
                                    enabled: false
                                },
                                showInLegend: false,
                            },
                            series: {
                                stacking: 'normal',
                                pointWidth: 32
                            },
                        },
                        yAxis: {
                            visible: true,
                            title: {text:'Percentage'},
                            min: 0,
                            max: 100,
                            endOnTick: false
                        },
                        xAxis: {
                            visible: false
                        },
                        tooltip: {
                            headerFormat: '{series.name}<br/>',
                            pointFormat: '{point.name}: <b>{point.percentage:.1f}%</b>'
                        },
                    },
                    series: $scope.totalDiskUsageChartSeries,
                    title: {
                        text: 'CEP4 total disk usage'
                    },
                    credits: {
                        enabled: false
                    },
                    loading: false
                }

                var loadTaskDiskUsage = function(otdb_id) {
                    dataService.getTaskDiskUsageByOTDBId(otdb_id).then(function(result) {
                        if(result.found) {
                            $scope.watchedObjectType = OBJECT_TYPE_TASK;
                            $scope.diskUsageChartConfig.title.text = result.task_directory.name + ' ' + result.task_directory.disk_usage_readable;
                            $scope.diskUsageChartSeries[0].name = result.task_directory.name + ' ' + result.task_directory.disk_usage_readable;
                            var path_parts = result.task_directory.path.split('/');
                            $scope.diskUsageChartSeries[0].project_name = path_parts[path_parts.length-2];
                            $scope.diskUsageChartSeries[0].data.splice(0, $scope.diskUsageChartSeries[0].data.length);
                            $scope.leastRecentCacheTimestamp = result.task_directory.cache_timestamp;

                            var sub_directory_names = Object.keys(result.sub_directories);
                            sub_directory_names.sort(function(a, b) { return ((a.name < b.name) ? -1 : ((a.name > b.name) ? 1 : 0)); });
                            for(var sub_dir of sub_directory_names) {
                                var sub_dir_result = result.sub_directories[sub_dir];
                                $scope.diskUsageChartSeries[0].data.push({name:sub_dir_result.name + ' ' + sub_dir_result.disk_usage_readable,y:sub_dir_result.disk_usage || 0});

                                if(sub_dir_result.cache_timestamp < $scope.leastRecentCacheTimestamp) {
                                    $scope.leastRecentCacheTimestamp = sub_dir_result.cache_timestamp;
                                }
                            }
                            $scope.leastRecentCacheTimestamp = dataService.convertDatestringToLocalUTCDate($scope.leastRecentCacheTimestamp);
                        }else {
                            $scope.ok();
                            $scope.$evalAsync(function() { alert("Could not find disk usage for task " + otdb_id); });
                        }
                    });
                };

                var loadProjectDiskUsage = function(project_name) {
                    dataService.getProjectDiskUsage(project_name).then(function(result) {
                        if(result.found) {
                            $scope.watchedObjectType = OBJECT_TYPE_PROJECT;
                            $scope.diskUsageChartConfig.title.text = result.projectdir.name + ' ' + result.projectdir.disk_usage_readable;
                            $scope.diskUsageChartSeries[0].name = result.projectdir.name + ' ' + result.projectdir.disk_usage_readable;
                            $scope.diskUsageChartSeries[0].data.splice(0, $scope.diskUsageChartSeries[0].data.length);
                            $scope.leastRecentCacheTimestamp = result.projectdir.cache_timestamp;

                            var sub_directory_names = Object.keys(result.sub_directories);
                            sub_directory_names.sort(function(a, b) { return ((a.name < b.name) ? -1 : ((a.name > b.name) ? 1 : 0)); });
                            for(var sub_dir of sub_directory_names) {
                                var sub_dir_result = result.sub_directories[sub_dir];
                                $scope.diskUsageChartSeries[0].data.push({name:sub_dir_result.name + ' ' + sub_dir_result.disk_usage_readable,
                                                                        y:sub_dir_result.disk_usage || 0,
                                                                        otdb_id: parseInt(sub_dir_result.name.slice(1)) });

                                if(sub_dir_result.cache_timestamp < $scope.leastRecentCacheTimestamp) {
                                    $scope.leastRecentCacheTimestamp = sub_dir_result.cache_timestamp;
                                }
                            }
                            $scope.leastRecentCacheTimestamp = dataService.convertDatestringToLocalUTCDate($scope.leastRecentCacheTimestamp);
                        }else {
                            $scope.ok();
                            $scope.$evalAsync(function() { alert("Could not find disk usage for project " + project_name); });
                        }
                    });
                };

                var loadAllProjectsDiskUsage = function() {
                    dataService.getProjectsDiskUsage().then(function(result) {
                        if(result.found) {
                            $scope.watchedObjectType = OBJECT_TYPE_PROJECTS;
                            $scope.diskUsageChartConfig.title.text = result.projectdir.name + ' ' + result.projectdir.disk_usage_readable;
                            $scope.diskUsageChartSeries[0].name = result.projectdir.name + ' ' + result.projectdir.disk_usage_readable;
                            $scope.diskUsageChartSeries[0].data.splice(0, $scope.diskUsageChartSeries[0].data.length);
                            $scope.leastRecentCacheTimestamp = result.projectdir.cache_timestamp;

                            var sub_directory_names = Object.keys(result.sub_directories);
                            sub_directory_names.sort(function(a, b) { return ((a.name < b.name) ? -1 : ((a.name > b.name) ? 1 : 0)); });
                            for(var sub_dir of sub_directory_names) {
                                var sub_dir_result = result.sub_directories[sub_dir];
                                $scope.diskUsageChartSeries[0].data.push({name:sub_dir_result.name + ' ' + sub_dir_result.disk_usage_readable,
                                                                        y:sub_dir_result.disk_usage || 0,
                                                                        project_name: sub_dir_result.name });

                                if(sub_dir_result.cache_timestamp < $scope.leastRecentCacheTimestamp) {
                                    $scope.leastRecentCacheTimestamp = sub_dir_result.cache_timestamp;
                                }
                            }
                            $scope.leastRecentCacheTimestamp = dataService.convertDatestringToLocalUTCDate($scope.leastRecentCacheTimestamp);
                        }else {
                            $scope.ok();
                            $scope.$evalAsync(function() { alert("Could not find disk usage for all projects"); });
                        }
                    });
                };

                if(task) {
                    loadTaskDiskUsage(task.otdb_id);
                } else {
                    loadAllProjectsDiskUsage();
                }
            }
        });
    };
}]);