'use strict';

angular.module('radrigesApp')
    .controller('MainController', function ($scope, Principal, PubNews, PubMusic, PubRadioshow, PubTopslider, InstagramService, $sce) {
        Principal.identity().then(function (account) {
            $scope.account = account;
            $scope.isAuthenticated = Principal.isAuthenticated;
        });

        $scope.newss = [];
        $scope.loadNews = function () {
            PubNews.query({page: $scope.page, per_page: 5}, function (result, headers) {
                $scope.newss = result;
            });
        };
        $scope.loadNews();

        $scope.music = [];
        $scope.loadMusic = function () {
            PubMusic.query({page: $scope.page, per_page: 5}, function (result, headers) {
                $scope.music = result;

                var previewLength = 400;
                angular.forEach($scope.music, function (value, key) {
                    if (value.text.length > previewLength) {
                        value.text = value.text.substring(0, previewLength) + '...';
                    } else {
                        value.text = value.text;
                    }
                    $scope.music[key]["trackLink"] = $sce.trustAsHtml(value.trackLink);
                });
            });
        };
        $scope.loadMusic();

        $scope.radioshow = [];
        $scope.loadRadioshow = function () {
            PubRadioshow.query({page: $scope.page, per_page: 5}, function (result, headers) {
                $scope.radioshow = result;

                var previewLength = 400;
                angular.forEach($scope.radioshow, function (value, key) {
                    if (value.text.length > previewLength) {
                        value.text = value.text.substring(0, previewLength) + '...';
                    } else {
                        value.text = value.text;
                    }
                    $scope.radioshow[key]["trackLink"] = $sce.trustAsHtml(value.trackLink);
                });
            });
        };
        $scope.loadRadioshow();

        $scope.topsliders = [];
        $scope.loadTopslider = function () {
            PubTopslider.query({page: $scope.page, per_page: 5}, function (result) {
                $scope.topsliders = result;
                angular.forEach($scope.topsliders, function (value, key) {
                    $scope.topsliders[key]["coverLink"] = $sce.trustAsHtml(value.coverLink);
                });
            });
        };

        $scope.loadTopslider();

        $scope.getRecentPhotos = function () {
            InstagramService.getRecentPhotos().then(function (response) {
                $scope.instaPhotos = response;
            });
        };

        $scope.getRecentPhotos();
    });
