angular.module('appMessage').controller('MessageController', ['$scope', '$filter', '$location', '$anchorScroll', '$element', function($scope, $filter, $location, $anchorScroll, $element) {
        lxtalkClient.Invoke('{15180DEF-D508-45F7-BAEB-6486CD8752C8}', '_Register', '', $scope);
        $scope.glued = true;
        $scope.count = true;
        $scope.appContent = {};
        $scope.sendmessages = [];
        $scope.historymessages = [];

        $scope.initAppContent = function(appContentJson) {
            $scope.appContent = JSON.parse(appContentJson);
        };

        $scope.addMessage = function() {
            scroll(document.body);
        };

        $scope.showMessage = function(jsonStr) {
            console.log(jsonStr);
            jsonStr = jsonStr.replace(/ /g, "&nbsp");
            jsonStr = jsonStr.replace(/\r\n/g, "<br>");
            jsonStr = jsonStr.replace(/\r/g, "\\r");
            jsonStr = jsonStr.replace(/\n/g, "\\n");
            jsonStr = jsonStr.replace(/\t/g, "\\t");
            jsonStr = jsonStr.replace(/\f/g, "\\f");
            var msg = JSON.parse(jsonStr);
            $scope.sendmessages.push(msg);
            $scope.scrollHide();
        };

        $scope.showHistoryMessage = function(jsonStr) {
            console.log(jsonStr);
            jsonStr = jsonStr.replace(/ /g, "&nbsp");
            jsonStr = jsonStr.replace(/\r\n/g, "<br>");
            jsonStr = jsonStr.replace(/\r/g, "\\r");
            jsonStr = jsonStr.replace(/\n/g, "\\n");
            jsonStr = jsonStr.replace(/\t/g, "\\t");
            jsonStr = jsonStr.replace(/\f/g, "\\f");
            var msg = JSON.parse(jsonStr);
            $scope.historymessages.push(msg);
        };

        $scope.getTime = function(item) {
            return $filter("date")(item.ts, "yyyy-MM-dd HH:mm:ss");
        };

        $scope.getMore = function() {
            if ($scope.count) {
                $scope.height = document.body.scrollHeight;
                lxtalkClient.Invoke('{15180DEF-D508-45F7-BAEB-6486CD8752C8}', 'LoadHistoryMessages', '', $scope);
                $scope.count = false;
            } else {
                angular.element(".i-More").addClass("hide");
                angular.element(".n-More").removeClass("hide");
                $scope.height = document.body.scrollHeight;
                lxtalkClient.Invoke('{15180DEF-D508-45F7-BAEB-6486CD8752C8}', 'LoadHistoryMessages', '', $scope);
                $scope.count = false;
            }
        };

        $scope.scrollWheel = function() {
            if (document.body.scrollTop == 0) {
                $scope.height = document.body.scrollHeight;
                lxtalkClient.Invoke('{15180DEF-D508-45F7-BAEB-6486CD8752C8}', 'LoadHistoryMessages', '', $scope);
                $scope.count = false;
            }
        };

        $scope.loadImages = function(data) {
            lxtalkClient.Invoke('{15180DEF-D508-45F7-BAEB-6486CD8752C8}', 'LoadImages', JSON.stringify(data), $scope);
        };

        $scope.scrollHide = function() {
            angular.element("#body").addClass("scrollGlue");
        };
        $scope.scrollShow = function() {
            if (angular.element("#body").hasClass("scrollGlue")) {
                angular.element("#body").removeClass("scrollGlue");
                gotoBottom();
            } else angular.element("#body").removeClass("scrollGlue");
        };

        var gotoBottom = function() {
            $location.hash("bottom");
            $anchorScroll();
        };

    }])
    .animation(".animate", function() {
        return {
            enter: function(element, done) {
                var height = element.height();
                element.css({ position: 'relative', bottom: 0, opacity: 0 });
                element.animate({ bottom: 0, opacity: 1 }, 500, done);
            }
        };
    });