angular.module('appMessage').controller('TransmitterController', ['$scope', '$interval', '$timeout', function($scope, $interval, $timeout) {
    $scope.transmitters = [];
    $scope.reflesh = function(jsonStr) {
        console.log(jsonStr);
        var arr = JSON.parse(jsonStr);

        _.forEach(arr, function(value) {
            var isExist = false;
            for (var i = 0; i <= $scope.transmitters.length - 1; i++)
                if (value.id == $scope.transmitters[i].id) {
                    isExist = true;
                    _.assign($scope.transmitters[i], value);
                    break;
                }
            if (!isExist)
                $scope.transmitters.push(value);
        });
        $timeout();
    };

    $scope.getSuffix = function(item) {
        var arr = item.fileName.split(".");
        return '../img/file/' + arr[arr.length - 1] + '.png';
    };

    $scope.getFileName = function(item) {
        $scope.arr = item.fileName.split("\\");
        return $scope.arr[$scope.arr.length - 1];
    };

    $scope.getFileSize = function(item) {
        $scope.fileSize = item.length;
        if ($scope.fileSize > 1024 * 1024 * 1024)
            return (Math.round($scope.fileSize * 100 / (1024 * 1024 * 1024)) / 100).toString() + 'GB';
        else if ($scope.fileSize > 1024 * 1024)
            return (Math.round($scope.fileSize * 100 / (1024 * 1024)) / 100).toString() + 'MB';
        else
            return (Math.round($scope.fileSize * 100 / 1024) / 100).toString() + 'KB';
    };

    $scope.getPercent = function(item) {
        return parseInt((item.completedLength / item.length) * 100).toString() + '%';
    };

    $scope.getSpeed = function(item) {
        if (item.completedLength / item.takeTime > 1024 && item.completedLength / item.takeTime < 1024 * 1024)
            return parseInt((item.completedLength / item.takeTime) / 1024).toString() + ' MB/S';
        else if (item.completedLength / item.takeTime < 1024)
            return parseInt(item.completedLength / item.takeTime).toString() + ' KB/S';
        else
            return '';
    };

    $scope.getreTime = function(item) {
        var reTime = (item.length - item.completedLength) / (item.completedLength / item.takeTime) / 1000;
        if (reTime > 60 * 60)
            return (parseInt(reTime / 60 * 60) / 100).toFixed(2).slice(-2) + ':' + (parseInt((reTime % (60 * 60)) / 60) / 100).toFixed(2).slice(-2) + ':' + (parseInt((reTime % (60 * 60)) % 60) / 100).toFixed(2).slice(-2);
        else if (reTime > 60 && reTime < 60 * 60)
            return '00:' + (parseInt(reTime / 60) / 100).toFixed(2).slice(-2) + ':' + (parseInt(reTime % 60) / 100).toFixed(2).slice(-2);
        else if (reTime < 60)
            return '00:' + '00:' + (parseInt(reTime) / 100).toFixed(2).slice(-2);
        else
            return '';
    };

    $scope.acception = function(item) {
        lxtalkClient.Invoke('{AFAF1B48-627D-48E0-B513-26A0CDF1EBD0}', 'Accept', JSON.stringify(item), $scope);
    };

    $scope.decline = function(item) {
        lxtalkClient.Invoke('{AFAF1B48-627D-48E0-B513-26A0CDF1EBD0}', 'Decline', JSON.stringify(item), $scope);
    };

    $scope.saveAs = function(item) {
        lxtalkClient.Invoke('{AFAF1B48-627D-48E0-B513-26A0CDF1EBD0}', 'SaveAs', JSON.stringify(item), $scope);
    };

    $scope.cancel = function(item) {
        console.log("hello");
        lxtalkClient.Invoke('{AFAF1B48-627D-48E0-B513-26A0CDF1EBD0}', 'Cancel', JSON.stringify(item), $scope);
    };

    $scope.turnToOffline = function(item) {
        lxtalkClient.Invoke('{AFAF1B48-627D-48E0-B513-26A0CDF1EBD0}', 'TurnToOffline', JSON.stringify(item), $scope);
    };

    $scope.onTransmitterRemoved = function(t) {
        console.log(t);
        $timeout(function() {
            var data = JSON.parse(t);
            $scope.transmitters = _.remove($scope.transmitters, function(value) {
                return data.id !== value.id;
            });

            _.forEach($scope.transmitters, function(value) {
                console.log(value.id);
            });

        });
    };

    lxtalkClient.Invoke('{AFAF1B48-627D-48E0-B513-26A0CDF1EBD0}', '_Register', '', $scope);

    $interval(function() {
        lxtalkClient.Invoke('{AFAF1B48-627D-48E0-B513-26A0CDF1EBD0}', 'Reflesh', '', $scope);
    }, 1000, 0, false);

}]);