(function () {
    'use strict';
    // generic UI For listing files
    angular.module('ti_common').directive('ticUser', ticUser);
    ticUser.$inject = [];
    function ticUser() {
        return {
            restrict: 'E',
            templateUrl: 'ti_common/cloud/user.template.html',
            scope: {},
            controller: ticUserCtrl,
            controllerAs: 'vm',
            bindToController: true
        };
    }
    ticUserCtrl.$inject = ['$http', '$rootScope', '$scope', '$uibModal', 'ticUserAcctMgmt'];
    function ticUserCtrl($http, $rootScope, $scope, $uibModal, ticUserAcctMgmt) {
        var vm = this;
        vm.logout = ticUserAcctMgmt.logout;
        function getUsernameCallback(response) {
            vm.username = JSON.parse(response.data).uid;
            if (typeof io === "object" && $scope.username !== "") {
                window.portal = {};
                window.portal.socket = new io.connect(window.location.origin);
                $http.get('ti_common/cloud/message_modal.template.html')
                    .then(function (response) {
                    vm.logoutTemplate = response.data;
                });
                window.portal.socket.on("logout", function () {
                    var loggedOutModal = $uibModal.open({
                        template: vm.logoutTemplate,
                        controller: MessageModalInstanceCtrl,
                        resolve: {
                            headerMsg: function () {
                                return "Logout Notice";
                            },
                            bodyMsgs: function () {
                                return ["Your user account has been logged out (inactive or logged out from another app), and your current session has been disconnected.",
                                    "Please re-login to continue using the tool."];
                            },
                            type: function () {
                                return "info";
                            }
                        }
                    });
                    MessageModalInstanceCtrl.$inject = ['$scope', '$uibModalInstance', 'headerMsg', 'bodyMsgs', 'type'];
                    function MessageModalInstanceCtrl($scope, $uibModalInstance, headerMsg, bodyMsgs, type) {
                        $scope.headerMsg = headerMsg;
                        $scope.bodyMsgs = bodyMsgs;
                        $scope.type = type;
                        $scope.ok = function () {
                            $uibModalInstance.close();
                            location.reload();
                            // window.location.replace("/logout?reason=" + vm.reason + "&redirect=/uniflash");
                        };
                    }
                    loggedOutModal.result.then(function () {
                        $scope.username = "";
                        $rootScope.username = "";
                    });
                });
                window.portal.socket.on('asklogout', function (data) {
                    vm.reason = data.reason;
                    var iframe = document.getElementById("authIFrame");
                    iframe['src'] = "/logout?reason=" + vm.reason + "&redirect=/uniflash";
                    // console.log('asklogout: ' + JSON.stringify(data));
                });
                window.portal.socket.on('preparelogout', function (data) {
                    // console.log('preparelogout: ' +JSON.stringify(data));
                    // TODO: save temp session and cancel long running ops here
                    $rootScope.$emit('logout.preparelogout');
                    // 
                    $rootScope.$on('logout.logoutready', function () {
                        window.portal.socket.emit("logoutready");
                    });
                });
            }
        }
        if (ticUserAcctMgmt) {
            ticUserAcctMgmt.getUserInfo().then(getUsernameCallback);
        }
    }
})();
