'use strict';

var searchFieldsService = function() {
    var searchFields = {
        searchField: "",
        results: [],
        facets: []
    };
    return searchFields;
};

var appModule = angular.module('istex-widget', []).controller('istexWidgetCtrl', function($scope, searchFields) {
    $scope.searchFields = searchFields;
    $scope.search = function() {

        var query = "document/?q=";

        query += $scope.searchFields.searchField;
        query += "&size=10";
        query += "&from=0";
        query += "&output=*";
        $("#searchButton").button('loading');
        $("#result").css("opacity", 0.4);

        var request = {
            url: "http://api.istex.fr/" + query,
            jsonp: true,
            crossDomain: true,
            success: function(data) {
                $scope.searchFields.results = data.hits;
                $("button").button('reset');
                $("#result").css("opacity", 1);
            },
            error: function() {
                console.log("Une erreur est survenue!!!!");
            }
        };

        $.ajax(request);

        $("#result").removeClass('hide');
        $("#paginRow").removeClass('hide');
        $("#pageNumber").removeClass('hide');

    };
}).service('searchFields', searchFieldsService);

appModule.filter('linksIcon', ['$sce', function($sce) {
    return function(argument) {
        var html = "";
        var i = 0;
        while (i < argument.length) {
            var typeFile;
            switch (argument[i].mimetype) {
                case 'application/zip':
                    typeFile = 'http://caron.ads.intra.inist.fr:53390/img/mimetypes/32px/zip.png'
                    break;
                case 'application/pdf':
                    typeFile = 'http://caron.ads.intra.inist.fr:53390/img/mimetypes/32px/pdf.png'
                    break;
                case 'image/tiff':
                    typeFile = 'http://caron.ads.intra.inist.fr:53390/img/mimetypes/32px/tiff.png'
                    break;
                case 'application/xml':
                    typeFile = 'http://caron.ads.intra.inist.fr:53390/img/mimetypes/32px/xml.png'
                    break;
                case 'application/mods+xml':
                    typeFile = 'http://caron.ads.intra.inist.fr:53390/img/mimetypes/32px/mods.png'
                    break;
                default:
                    typeFile = 'http://caron.ads.intra.inist.fr:53390/img/mimetypes/32px/_blank.png'
                    break;
            }
            console.log("<a href=\"" + argument[i].uri + "\" target=\"_blank\"><img src=\"" + typeFile + "\" alt=\'" + argument[i].type + "\' title=\'" + argument[i].type + "\'></a>");
            html += "<a href=\"" + argument[i].uri + "\" target=\"_blank\"><img src=\"" + typeFile + "\" alt=\'" + argument[i].type + "\' title=\'" + argument[i].type + "\'></a>"
            i++;
        }
        return $sce.trustAsHtml(html);
    }
}]);

angular.element(document).ready(function() {
    angular.bootstrap(document, ['istex-widget']);
});