'use strict';

var _slicedToArray2 = require('babel-runtime/helpers/slicedToArray');

var _slicedToArray3 = _interopRequireDefault(_slicedToArray2);

var _regenerator = require('babel-runtime/regenerator');

var _regenerator2 = _interopRequireDefault(_regenerator);

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }

/**
 * Created by godsong on 16/6/13.
 */
var Router = require('koa-router');
var P2PSession = require('../components/P2PSession');
var DeviceManager = require('../components/DeviceManager');

var wsRouter = Router();

wsRouter.all('/debugProxy/inspector/:sessionId', _regenerator2.default.mark(function _callee(next) {
    return _regenerator2.default.wrap(function _callee$(_context) {
        while (1) {
            switch (_context.prev = _context.next) {
                case 0:
                    console.log('new inspector client connected,join[' + this.params.sessionId + ']');
                    if (P2PSession.join(this.params.sessionId, this.websocket)) ;
                    this.websocket.on('message', function (message) {
                        message = JSON.parse(message);
                        P2PSession.postMessage(this, message);
                    });
                    _context.next = 5;
                    return next;

                case 5:
                case 'end':
                    return _context.stop();
            }
        }
    }, _callee, this);
}));
wsRouter.all('/debugProxy/debugger/:sessionId', _regenerator2.default.mark(function _callee2(next) {
    return _regenerator2.default.wrap(function _callee2$(_context2) {
        while (1) {
            switch (_context2.prev = _context2.next) {
                case 0:
                    console.log('new debugger client connected,join[' + this.params.sessionId + ']');
                    P2PSession.join(this.params.sessionId, this.websocket);
                    this.websocket.on('message', function (message) {
                        message = JSON.parse(message);
                        P2PSession.postMessage(this, message);
                    });
                    _context2.next = 5;
                    return next;

                case 5:
                case 'end':
                    return _context2.stop();
            }
        }
    }, _callee2, this);
}));
var _listSocket = null;
DeviceManager.on('update', function (deviceList) {
    if (_listSocket) {
        _listSocket.send(JSON.stringify(deviceList));
    }
});
wsRouter.all('/debugProxy/list', _regenerator2.default.mark(function _callee3(next) {
    return _regenerator2.default.wrap(function _callee3$(_context3) {
        while (1) {
            switch (_context3.prev = _context3.next) {
                case 0:
                    _listSocket = this.websocket;
                    this.websocket.on('close', function () {
                        _listSocket = null;
                    });
                    _listSocket.send(JSON.stringify(DeviceManager.getDeviceList()));

                case 3:
                case 'end':
                    return _context3.stop();
            }
        }
    }, _callee3, this);
}));
wsRouter.all('/debugProxy/native1', _regenerator2.default.mark(function _callee4(next) {
    return _regenerator2.default.wrap(function _callee4$(_context4) {
        while (1) {
            switch (_context4.prev = _context4.next) {
                case 0:
                    console.log('new native  client connected');
                    this.websocket.on('message', function (message) {
                        message = JSON.parse(message);
                        console.log(message.method);
                    });
                    _context4.next = 4;
                    return next;

                case 4:
                case 'end':
                    return _context4.stop();
            }
        }
    }, _callee4, this);
}));
wsRouter.all('/debugProxy/native', _regenerator2.default.mark(function _callee5(next) {
    return _regenerator2.default.wrap(function _callee5$(_context5) {
        while (1) {
            switch (_context5.prev = _context5.next) {
                case 0:
                    console.log('new native  client connected');
                    this.websocket.on('message', function (message) {
                        var len = message.length;
                        message = JSON.parse(message);
                        if (len < 1000) {
                            console.log('native:', message);
                        } else {
                            console.log('native:', message.method);
                        }
                        var device = DeviceManager.getDevice(this);
                        if (message.method) {
                            var _message$method$split = message.method.split('.');

                            var _message$method$split2 = (0, _slicedToArray3.default)(_message$method$split, 2);

                            var domain = _message$method$split2[0];
                            var method = _message$method$split2[1];

                            if (domain == 'WxDebug') {
                                if (method == 'registerDevice') {
                                    DeviceManager.registerDevice(message.params, this);
                                    this.send(JSON.stringify({ id: message.id, result: 'ready' }));
                                } else {
                                    if (device) device.debuggerSession.postMessage(this, message);else console.error('undefined device');
                                }
                            } else {
                                if (device) device.inspectorSession.postMessage(this, message);else console.error('undefined device');
                            }
                        } else {
                            if (device) device.inspectorSession.postMessage(this, message);else console.error('undefined device');
                        }
                    });
                    _context5.next = 4;
                    return next;

                case 4:
                case 'end':
                    return _context5.stop();
            }
        }
    }, _callee5, this);
}));
module.exports = wsRouter;