'use strict';

angular.module('app').controller('PeerController', ['$scope', '$state', '$timeout', function ($scope, $state, $timeout) {

    lxtalkClient.Invoke('{C62F6059-7741-4AC1-8B8F-97EB5F7C116C}', '_Register', '', $scope);
    var onConnected = function (c) {
        c.on('data', function (data) {
            // console.log('接收数据：', data.byteLength);
            lxtalkClient.Invoke('{C62F6059-7741-4AC1-8B8F-97EB5F7C116C}', 'OnRecv', data, $scope);
        });
        c.on('close', function () {
            delete $scope.connectedPeers[c.peer];
        });
        $scope.connectedPeers[c.peer] = 1;
        lxtalkClient.Invoke('{C62F6059-7741-4AC1-8B8F-97EB5F7C116C}', 'OnConnected', '{"loginName":"' + c.peer + '"}', $scope);
    };
    $scope.options = {
        host: '120.26.136.253',
        port: 9000,
        debug: 3,
        config: {
            'iceServers': [{
                    'url': 'stun:lqq@59.110.63.164',
                    "credential": "123456"
                },
                {
                    "url": "turn:lqq@59.110.63.164",
                    "credential": "123456"
                }
            ]
        },
        // key: 'peerjs',
        logFunction: function () {
            var copy = Array.prototype.slice.call(arguments).join(' ');
            console.info(copy);
        }
    }

    $scope.init = function (jsonStr) {
        console.log(jsonStr);
        var data = JSON.parse(jsonStr);
        $scope.loginName = data.loginName;
        $scope.peer = new Peer(
            $scope.loginName,
            $scope.options
        );
        $scope.connectedPeers = {};
        $scope.peer.on('open', function (id) {
            console.log(id);
        });
        $scope.peer.on('connection', function (c) {
            c.on('open', function () {
                onConnected(c);
            });
        });
        $scope.peer.on('error', function (err) {
            $timeout(function () {
                $scope.init(jsonStr);
            }, 50000);
        });
    };

    $scope.connect = function (jsonStr) {
        var data = JSON.parse(jsonStr);
        data = _.assign(data, {
            label: 'file',
            serialization: 'binary'
        });
        console.info(data);
        if (!$scope.connectedPeers[data.loginName]) {
            var c = $scope.peer.connect(data.loginName, data);
            c.on('open', function () {
                onConnected(c);
            });
            c.on('error', function (err) {
                console.log(err);
                lxtalkClient.Invoke('{C62F6059-7741-4AC1-8B8F-97EB5F7C116C}', 'OnDisconnected', '{"loginName":"' + c.peer + '"}', $scope);
            });
            $scope.connectedPeers[data.loginName] = 1;
        }
    };

    $scope.send = function (jsonStr, data) {
        var param = JSON.parse(jsonStr);
        data = data.slice(0);
        // console.log('发送数据：', data.byteLength);
        var c = $scope.peer.connections[param.loginName];
        if (c && c[0]) {
            c[0].send(data);
        } else {
            lxtalkClient.Invoke('{C62F6059-7741-4AC1-8B8F-97EB5F7C116C}', 'OnDisconnected', '{"loginName":"' + param.loginName + '"}', $scope);
        }
    };

    $scope.close = function (jsonStr) {
        var c = $scope.peer.connections[param.loginName];
        if (c) {
            c.close();
        }
    };

    $scope.closeAll = function (jsonStr) {
        _.forEach($scope.peer.connections, function (c) {
            c.close();
        });
    };

    $scope.destroy = function (jsonStr) {
        $scope.peer.destroy();
    };
}]);