'use strict';

angular.module('facebook.controllers.main', [
    'system'
])
    .controller('FacebookController', ['$scope',
        '$route',
        '$location',
        'System',
        'ArticleService',
        'ClassificationService',
        'ImageService',
        function ($scope, $route, $location, System, ArticleService, ClassificationService, ImageService) {
            var id = $route.current.params["id"];
            // Defaults to sessionStorage for storing the Facebook token
            openFB.init({ appId: '878736172139486' });
            //  Uncomment the line below to store the Facebook token in localStorage instead of sessionStorage
            //  openFB.init({appId: 'YOUR_FB_APP_ID', tokenStore: window.localStorage});

            $scope.login = function () {
                openFB.login(
                        function (response) {
                            if (response.status === 'connected') {
                                alert('Uspješno ste logovani, odobrite sigurnostni ključ: ' + response.authResponse.token);
                            } else {
                                alert('Facebook logovanje nije uspjelo: ' + response.error);
                            }
                        }, { scope: 'email,read_stream,publish_actions' });
            }

            $scope.getInfo = function () {
                openFB.api({
                    path: '/me',
                    success: function (data) {
                        console.log(JSON.stringify(data));
                        document.getElementById("userName").innerHTML = data.name;
                        document.getElementById("userPic").src = 'http://graph.facebook.com/' + data.id + '/picture?type=small';
                    },
                    error: errorHandler
                });
            }
            $scope.renderHtml = function (html_code) {
                return $sce.trustAsHtml(html_code);
            };

            function htmlToPlaintext(text) {
                return String(text).replace(/<[^>]+>/gm, '');
            }
            $scope.refresh = function () {
                ArticleService.get(id)
                    .then(function (data) {
                        $scope.article = data;
                        var kultura = Globalize.culture().name;
                        if (kultura == 'sr') {
                            System.Page.title("ОПШТИНА ЛАКТАШИ - FACEBOOK " + $scope.article.Title);
                        }
                        if (kultura == 'sr') {
                            System.Page.title("OPŠTINA LAKTAŠI - FACEBOOK" + $scope.article.Title);
                        }
                        if (kultura == 'en') {
                            System.Page.title("OPSTINA LAKTASI - FACEBOOK" + $scope.article.Title);
                        }
                        if (kultura == 'de') {
                            System.Page.title("OPSTINA LAKTASI - FACEBOOK" + $scope.article.Title);
                        }
                    });
            }

            $scope.share = function () {
                var fm = htmlToPlaintext($scope.article.Content);
                var name = $scope.article.Title;
                var id = $scope.article.ID;
                var imageID = $scope.article.Thumb[0];
                ImageService.get(imageID)
               .then(function (data) {
                   $scope.image = data;
                   var image = $scope.image.FileName;
                   var lib = $scope.image.LibraryID;
                   openFB.api({
                       method: 'POST',
                       path: '/me/feed',
                       params: {
                           name: name,
                           caption: name,
                           message: fm || 'Opština Laktaši - FACEBOOK PROFIL',
                           link: 'http://www.laktasi.net/#!/articles/' + id ,
                           picture: 'http://laktasi.net/_content/_media/' + lib + '/' + image
                       },
                       success: function () {
                           System.Message.error('Sadržaj je uspješno ovjavljen na Facebook profilu!');
                       },
                       error: System.Message.error(errorHandler)
                   });
               });

            }
            $scope.revoke = function () {
                openFB.revokePermissions(
                        function () {
                            alert('Dozvole provjerene');
                        },
                        errorHandler);
            }

            $scope.logout = function () {
                openFB.logout(
                        function () {
                            alert('Uspješno ste izlogovani');
                        },
                        errorHandler);
            }

            function errorHandler(error) {
                alert(error.message);
            }

            $scope.refresh();

        }
    ]);