var app = angular.module('infoAdmin',['ngFileUpload']);
app.config(['$httpProvider', function ($httpProvider) {
    $httpProvider.defaults.xsrfCookieName = 'csrftoken';
    $httpProvider.defaults.xsrfHeaderName = 'X-CSRFToken';
}]);
app.service("InstanceList", ["$http", function($http){
    var self = this;
    this.selected_rot = {}
    this.rotations = [];
    this.listInstances = function() {
        return self.selected_rot;
    };
    this.listRotations = function(){
        return self.rotations;
    }
    this.createInstance = function(rotation_id, item_id, item_type, duration){
        var data = {
            'rotation_id': rotation_id,
            'item_id': item_id,
            'item_type': item_type,
            'duration': duration,
        }
        $http.post("/infoscreen/instance", data).then(function(response){
            self.getRotation(rotation_id);
        });
    };
    this.getRotation = function(id){
        $http.get("/infoscreen/rotation/"+id).then(function(response){
            self.selected_rot = response.data;
        });
    };
    this.getRotations = function(){
        $http.get("/infoscreen/rotations").then(function(response){
            self.rotations = response.data;
            // select first rotation by default if we have any
            if (self.rotations.length > 0){
                self.getRotation(self.rotations[0].id);
            }
        });
    }
    this.deleteInstance = function(id){
        $http.delete("/infoscreen/instance/"+id).then(function(response){
            self.getRotation(self.selected_rot.id);
        });
    }
}]);
app.service("ItemList", ["$http",'InstanceList', function($http, InstanceList){
    var self = this;
    self.items = []
    this.loadItems = function(){
        $http.get("/infoscreen/items").then(function(response){
            self.items = response.data;
        });
    };
    this.getItems = function(){
        return self.items;
    };
    this.deleteItem = function(type_id, item_id){
        $http.delete("/infoscreen/delete_item/"+type_id+"/"+item_id).then(function(){
            self.loadItems();
            // refresh instances because deleting item may cascade to instances
            InstanceList.getRotation(InstanceList.selected_rot.id);
        });
    };
    this.refreshCB = function(response){
        self.loadItems();
    };
}]);

app.controller('infoadmin_ctrl', function($scope, $http, ItemList, InstanceList){
    // init items
    $scope.rotations = [];
    $scope.infoitems = [];
    $scope.selected_rot= {};
    // helpers
    $scope.deleteItem = ItemList.deleteItem;
    $scope.selectRotation = InstanceList.getRotation;
    $scope.createInstance = InstanceList.createInstance;
    $scope.deleteInstance = InstanceList.deleteInstance;
    // fetch data
    $scope.$watch(InstanceList.listInstances, function(instances){
        $scope.selected_rot = instances;
    });
    $scope.$watch(InstanceList.listRotations, function(rotations){
        $scope.rotations = rotations;
    });
    InstanceList.getRotations();

    $http.get("/infoscreen/types").then(function(response){
        $scope.infotypes = response.data;
    });
    $scope.$watch(ItemList.getItems, function(items){
        $scope.infoitems = items
    });
    ItemList.loadItems();
});
app.controller('infoadmin_externalimage_create', function($scope, $http,ItemList){
    $scope.item = {}
    $scope.send = function(){
        $http.post("/infoscreen/create_external_image", $scope.item).then(ItemList.loadItems)
    }
});
app.controller('infoadmin_abbitem_create', function($scope, $http,ItemList){
    $scope.item = {}
    $scope.send = function(){
        $http.post("/infoscreen/create_abbitem", $scope.item).then(ItemList.loadItems)
    }
});
app.controller('infoadmin_sossoitem_create', function($scope, $http,ItemList){
    $scope.item = {}
    $scope.send = function(){
        $http.post("/infoscreen/create_sossoitem", $scope.item).then(ItemList.loadItems)
    }
});

app.controller('infoadmin_image_create', ['$scope', 'Upload', '$timeout',"ItemList", function ($scope, Upload, $timeout,ItemList) {
    $scope.send = function(file) {
        console.log("name: "+$scope.imagename);
        file.upload = Upload.upload({
            url: '/infoscreen/create_image',
            data: {name: $scope.imagename, image: file},
        }).then(function (response) {
            $timeout(function () {
                file.result = response.data;
                ItemList.loadItems();
                $scope.name= ""
                $scope.img = {}
            });
        },function (response) {
             if (response.status > 0)
                $scope.errorMsg = response.status + ': ' + response.data;
        }, function (evt) {
            file.progress = Math.min(100, parseInt(100.0 * evt.loaded / evt.total));
        });
    }
}]);
