'use strict';

var mlink = require('../midware');
var Router = mlink.Router;
var simulatorManager = require('../managers/simulator_manager');
var config = require('../../lib/config');
var hook = require('../../util/hook');
var debuggerRouter = Router.get('debugger');

debuggerRouter.registerHandler(function (message) {
  if (message.payload.method === 'AtomDebug.applyChannelId') {
    var channelId = debuggerRouter.newChannel();
    message.payload = {
      method: 'AtomDebug.pushChannelId',
      params: {
        channelId: channelId,
        connectUrl: config.getConnectUrl(channelId)
      }
    };
    message.reply();
  } else if (message.payload.method === 'AtomDebug.simrun') {
    simulatorManager.connect(message.payload.params).catch(function (e) {
      hook.record('/weex_tool.weex_debugger.scenes', { feature: 'simrun', status: 'fail' });
      debuggerRouter.pushMessage('page.entry', {
        method: 'AtomDebug.prompt',
        params: {
          messageText: 'PLEASE_INSTALL_XCODE',
          channelId: message.payload.params
        }
      });
    });
  }
}).at('page.entry');