'use strict';
angular.module('appMessage').controller('HistoryMsgController', ['$scope', '$filter', '$timeout', function($scope, $filter, $timeout) {
    //lxtalkClient.Invoke('{15180DEF-D508-45F7-BAEB-6486CD8752C8}', '_Register', '', $scope);
    $scope.count = true;
    $scope.appContent = {};
    $scope.historymessages = [];
    $scope.searchArea = "全部";
    $scope.activeData = "全部";
    $scope.animateFirst = true;
    $scope.totalItems = 1000;
    $scope.currentPage = Math.floor($scope.totalItems / 10) + 1; //初始当前页
    $scope.maxSize = 5; //最多显示7页其他的用···代替

    $scope.$on('$viewContentLoaded', function() {
        var oBox = $("#box")[0],
            oLeft = $(".tab-content")[0],
            oRight = $("#media")[0],
            oLine = $(".div-draw")[0];
        console.log($(oLine));
        oLine.onmousedown = function(e) {
            var oClientX = (e || event).clientX; //鼠标位置距离最左端长度
            var oLineX = oLine.offsetLeft; //line相对父元素的长度
            console.log((e || event));
            document.onmousemove = function(e) {
                var LineX = oLineX + ((e || event).clientX - oClientX);
                var e = e || window.event;
                oLine.style.margin = 0;
                LineX < 150 && (LineX = 150);
                LineX > 300 && (LineX = 300);
                oLine.style.left = oLeft.style.width = LineX + "px";
                oRight.style.width = oBox.clientWidth - LineX + "px";
                return false;
            };
            document.onmouseup = function() {
                document.onmousemove = null;
                document.onmouseup = null;
                oLine.releaseCapture && oLine.releaseCapture(); //将鼠标事件退回
            };
            oLine.setCapture && oLine.setCapture(); //将后续的mouse事件都发送给当前对象
            return false;
        };
    });

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

    $scope.animate = function($event) {
        var animate = $($event.target);
        console.log(animate);
        if (animate[0].innerText == "最近联系人" && $scope.animateFirst)
            return;
        else if (animate[0].nodeName == "A") {
            angular.element(".slip").removeClass("animate");
            animate.addClass("animate");
        } else if (animate[0].nodeName == "SPAN") {
            angular.element(".slip").removeClass("animate");
            $(animate[0].parentElement).addClass("animate");
        }
        $scope.animateFirst = false;
    };

    $scope.actived = function($event) {
        var activeClick = $($event.target);
        console.log(activeClick);
        if (activeClick[0].nodeName == "A") {
            angular.element(".Li").removeClass("activeBackground");
            angular.element(".Aa").removeClass("activeColor");
            activeClick.addClass("activeColor");
        } else if (activeClick[0].nodeName == "LI") {
            angular.element(".Aa").removeClass("activeColor");
            angular.element(".Li").removeClass("activeBackground");
            activeClick.addClass("activeBackground");
        } else if (activeClick[0].nodeName == "SPAN") {
            angular.element(".Aa").removeClass("activeColor");
            angular.element(".Li").removeClass("activeBackground");
            $(activeClick[0].parentElement).addClass("activeBackground");
            $(activeClick[0].parentElement.parentElement).addClass("activeBackground");
        }
    };

    $scope.activeClick = function($event) {
        var activeClick = $($event.target.firstElementChild);
        $scope.activeData = $event.target.innerText;
        angular.element(".active").addClass("hid");
        activeClick.removeClass("hid");
    };

    $scope.onRefresh = function($event) {
        var activeClick = $($(".dropdown-menu")[0].firstElementChild.firstElementChild.firstElementChild);
        console.log(activeClick)
        $scope.activeData = "全部";
        $scope.searchArea = "全部"
        $scope.keyWords = "";
        angular.element(".active").addClass("hid");
        activeClick.removeClass("hid");
    };

    $scope.searchMsg = function() {
        console.log($scope.searchArea, $scope.keyWords);
    };

    $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.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.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.previousPage = function() {
        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);
    };

}]);