'use strict';

var mlink = require('../midware');
var Router = mlink.Router;
var DeviceManager = require('../managers/device_manager');
var config = require('../../lib/config');
var debuggerRouter = Router.get('debugger');
var registerDeviceChannelId = void 0;
var FirstStartDebug = true;

debuggerRouter.on(Router.Event.TERMINAL_LEAVED, 'proxy.native', function (signal) {
  var device = DeviceManager.getDevice(signal.channelId);
  if (!device) {
    return;
  }
  // This is a special treatment for android devices
  // The android platform will registe twice and the socket leave signl may come after registed, the newly registered device may be removed immediately。
  if (device.platform === 'android' && FirstStartDebug) {
    FirstStartDebug = false;
  } else {
    DeviceManager.removeDevice(signal.channelId, function () {
      debuggerRouter.pushMessageByChannelId('page.debugger', signal.channelId, {
        method: 'WxDebug.deviceDisconnect',
        params: device
      });
    });
  }
});

debuggerRouter.on(Router.Event.TERMINAL_JOINED, 'page.debugger', function (signal) {
  var device = DeviceManager.getDevice(signal.channelId);
  // Fixme Android will connect twice while first scan.
  if (registerDeviceChannelId === signal.channelId) {
    FirstStartDebug = true;
  }
  registerDeviceChannelId = signal.channelId;
  debuggerRouter.pushMessageByChannelId('page.debugger', signal.channelId, {
    method: 'WxDebug.pushDebuggerInfo',
    params: {
      device: device,
      bundles: config.bundleUrls,
      connectUrl: config.getConnectUrl(signal.channelId)
    }
  });
});

debuggerRouter.registerHandler(function (message) {
  var device = DeviceManager.registerDevice(message.payload.params, message.channelId);
  if (device) {
    message.payload = {
      method: 'WxDebug.pushDebuggerInfo',
      params: {
        device: device,
        bundles: config.bundleUrls,
        connectUrl: config.getConnectUrl(message.channelId)
      }
    };
    debuggerRouter.pushMessage('page.entry', {
      method: 'WxDebug.startDebugger',
      params: message.channelId
    });
    message.to('page.debugger');
    // iOS platform need reload signal to reload runtime context.
    // if (device.platform === 'iOS') {
    //   setTimeout(() => {
    //     debuggerRouter.pushMessageByChannelId('page.debugger', message.channelId, {
    //       method: 'WxDebug.reloadInspector',
    //       params: device
    //     });
    //   }, 3000);
    // }
  }
  return false;
}).at('proxy.native').when('payload.method=="WxDebug.registerDevice"');