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

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

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

        $scope.voicePlay = function($event) {
            var audio = $($event.target.parentElement.nextElementSibling)[0];
            var unread = $($event.target.nextElementSibling);
            var audioAll = $('audio');
            console.log($($event.target));
            console.log(audioAll);
            if (audio !== null) {
                if (audio.paused) {
                    for (var i = 0, len = audioAll.length; i < len; i++) {
                        if (!audioAll[i].paused) {
                            $(audioAll[i].previousElementSibling.firstElementChild).removeClass("app-voice-play");
                            $(audioAll[i].previousElementSibling.firstElementChild).addClass("app-voice-pause");
                            audioAll[i].load();
                        }
                    }
                    unread.removeClass("app-voice-unread");
                    $($event.target).removeClass("app-voice-pause");
                    $($event.target).addClass("app-voice-play");
                    $timeout(function() {
                        $($event.target).removeClass("app-voice-play");
                        $($event.target).addClass("app-voice-pause");
                    }, 1000 * audio.duration);
                    audio.play();
                } else {
                    $($event.target).removeClass("app-voice-play");
                    $($event.target).addClass("app-voice-pause");
                    audio.pause();
                    audio.load();
                }
            }
        };

        $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.getUserhead = function(item) {
            var arr = $scope.appContent.directory.user.split("\\");
            var userself = arr[arr.length - 2];
            if (item.sender.indexOf(userself) !== -1)
                return 'user-head user-head-rt';
            else
                return 'user-head user-head-lf';
        };
        $scope.getUserheadImg = function(item) {
            var arr = $scope.appContent.directory.user.split("\\");
            var userself = arr[arr.length - 2];
            if (item.sender.indexOf(userself) !== -1)
                return '../img/自己头像.png';
            else
                return '../img/对方头像.png';
        };

        $scope.getHisTime = function(item) {
            var myDate = new Date();
            var nowDate = $filter("date")(myDate.getTime(), "yyyy-MM-dd");
            var messageDate = $filter("date")(item.ts, "yyyy-MM-dd");
            if (messageDate == nowDate)
                return $filter("date")(item.ts, "HH:mm:ss");
            else
                return $filter("date")(item.ts, "yyyy-MM-dd HH:mm:ss");
        };

        $scope.getCurTime = function(item) {
            var arr = $scope.appContent.directory.user.split("\\");
            var userself = arr[arr.length - 2];
            var getUserID = item.sender.indexOf(userself);
            var myDate = new Date();
            var nowDate = $filter("date")(myDate.getTime(), "yyyy-MM-dd");
            var messageDate = $filter("date")(item.ts, "yyyy-MM-dd");
            if (getUserID !== -1) {
                $scope.sMessageTime[$scope.sendMark] = item.ts;
                $scope.sendMark = $scope.sendMark + 1;
                $scope.receiveMark = 0;
                $scope.rMessageTime = [];
            } else {
                $scope.rMessageTime[$scope.receiveMark] = item.ts;
                $scope.receiveMark = $scope.receiveMark + 1;
                $scope.sendMark = 0;
                $scope.sMessageTime = [];
            }
            if ($scope.sendMark == 1 || $scope.receiveMark == 1 || Math.abs($scope.sMessageTime[$scope.sendMark - 1] - $scope.sMessageTime[0]) == 0 || Math.abs($scope.rMessageTime[$scope.receiveMark - 1] - $scope.rMessageTime[0]) == 0) {
                return $filter("date")(item.ts, "HH:mm:ss");
            } else if ($scope.sendMark > 1 && Math.abs($scope.sMessageTime[$scope.sendMark - 1] - $scope.sMessageTime[0]) <= 60000) {
                return '';
            } else if ($scope.receiveMark > 1 && Math.abs($scope.rMessageTime[$scope.receiveMark - 1] - $scope.rMessageTime[0]) <= 60000) {
                return '';
            } else if ($scope.sendMark > 1 && Math.abs($scope.sMessageTime[$scope.sendMark - 1] - $scope.sMessageTime[0]) > 60000) {
                $scope.sMessageTime[0] = $scope.sMessageTime[$scope.sendMark - 1];
                $scope.sendMark = 1;
                return $filter("date")(item.ts, "HH:mm:ss");
            } else if ($scope.receiveMark > 1 && Math.abs($scope.rMessageTime[$scope.receiveMark - 1] - $scope.rMessageTime[0]) > 60000) {
                $scope.rMessageTime[0] = $scope.rMessageTime[$scope.receiveMark - 1];
                $scope.receiveMark = 1;
                return $filter("date")(item.ts, "HH:mm:ss");
            }
        };

        $scope.getMore = function() {
            if ($scope.count) {
                $scope.height = document.body.scrollHeight + 200;
                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 + 200;
                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);
            }
        };
    });