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

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

cleanupControllerMod.controller('CleanupController', ['$scope', '$uibModal', '$mdDialog', '$http', '$q', 'dataService', function($scope, $uibModal, $mdDialog, $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.deleteTasksDataWithConfirmation = function(tasks) {
        suc_tasks_promises = [];
        for(var task of tasks) {
            if(task.successor_ids) {
                for(var suc_id of task.successor_ids) {
                    suc_tasks_promises.push(dataService.getTask(suc_id));
                }
            }
        }

        $q.all(suc_tasks_promises).then(function(suc_tasks) {
            var unfinished_suc_tasks = suc_tasks.filter(function(t) { return t && t.status != 'finished' });

            if(unfinished_suc_tasks.length > 0) {
                var unfinished_ids = unfinished_suc_tasks.map(function(t) { return t.otdb_id; });
                $mdDialog.show($mdDialog.alert()
                        .parent(angular.element(document.querySelector('#popupContainer')))
                        .title('Error')
                        .textContent("Cannot delete data for " + unfinished_ids + " because there are unfinished successors")
                        .ariaLabel('Error')
                        .ok('Ok'));
                return;
            }

            du_promises = [];
            for(var task of tasks) {
                du_promises.push(dataService.getTaskDiskUsage(task));
            }

            $q.all(du_promises).then(function(du_results) {
                var unfound_du_results = du_results.filter(function(r) { return !r || !r.found; });

                if(unfound_du_results.length > 0) {
                    var unfound_ids = unfound_du_results.map(function(t) { return t.otdb_id; });
                    $mdDialog.show($mdDialog.alert()
                            .parent(angular.element(document.querySelector('#popupContainer')))
                            .title('Error')
                            .textContent("Could not find data to delete for one or more tasks")
                            .ariaLabel('Error')
                            .ok('Ok'));
                    return;
                }

                openDeleteConfirmationDialog(du_results);
            });
        });
    };

    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: {{amount_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;
                $scope.amount_scratch = 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.task_directory.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;
                    if(has_scratch) {
                        for(var scratch_path in du_result.task_directory.scratch_paths) {
                            var scratch_path_du = du_result.task_directory.scratch_paths[scratch_path];
                            if(scratch_path_du.found) {
                                $scope.amount_scratch += scratch_path_du.disk_usage;
                            }
                        }
                    }
                }

                $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.amount_scratch = dataService.humanreadablesize($scope.amount_scratch);

                $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_TASKS = 'tasks';
                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_TASKS:
                        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_TASKS:
                        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 = $scope.diskUsageChartConfig.title.text;
                            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 loadTasksDiskUsage = function(tasks) {
                    var du_promises = tasks.map(function(t) { return dataService.getTaskDiskUsageByOTDBId(t.otdb_id); });
                    $q.all(du_promises).then(function(du_results) {
                        var found_task_dus = du_results.filter(function(r) { return r.found;}).map(function(r) { return r.task_directory; });

                        if(found_task_dus.length > 0) {
                            $scope.watchedObjectType = OBJECT_TYPE_TASKS;

                            $scope.leastRecentCacheTimestamp = new Date(Math.min.apply(null, found_task_dus.map(function(tdu) { return dataService.convertDatestringToLocalUTCDate(tdu.cache_timestamp); })));

                            var total_usage = found_task_dus.map(function(tdu) { return tdu.disk_usage; }).reduce(function(a, b) { return a + b;});
                            var total_usage_readable = dataService.humanreadablesize(total_usage);

                            $scope.diskUsageChartConfig.title.text = 'Total size: ' + total_usage_readable;
                            $scope.diskUsageChartSeries[0].data.splice(0, $scope.diskUsageChartSeries[0].data.length);
                            $scope.diskUsageChartSeries[0].name = $scope.diskUsageChartConfig.title.text;

                            for(var task_du of found_task_dus) {
                                $scope.diskUsageChartSeries[0].data.push({name:task_du.name + ' ' + task_du.disk_usage_readable,
                                                                          y:task_du.disk_usage || 0,
                                                                          otdb_id: task_du.otdb_id });
                            }
                        }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 = $scope.diskUsageChartConfig.title.text;
                            $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 = $scope.diskUsageChartConfig.title.text;
                            $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) {
                    if(task.constructor === Array) {
                        loadTasksDiskUsage(task);
                    } else {
                        loadTaskDiskUsage(task.otdb_id);
                    }
                } else {
                    loadAllProjectsDiskUsage();
                }
            }
        });
    };
}]);