'use strict';

/*eslint-disable*/
var mlink = require('../midware');
var Router = mlink.Router;
var debuggerRouter = Router.get('debugger');
var DeviceManager = require('../managers/device_manager');

debuggerRouter.registerHandler(function (message, next) {
    var payload = message.payload;
    var device = DeviceManager.getDevice(message.channelId);
    if (!device) {
        message.discard = true;
        return;
    }
    if (payload.method === 'AtomDebug.setLogLevel') {
        device.logLevel = payload.params.data;
        message.payload = {
            method: 'AtomDebug.setLogLevel',
            params: {
                logLevel: payload.params.data
            }
        };
        debuggerRouter.pushMessage('page.debugger', message.terminalId, {
            method: 'AtomDebug.reloadInspector'
        });
        debuggerRouter.pushMessage('page.debugger', message.terminalId, {
            method: 'AtomDebug.reloadRuntime'
        });
    } else if (payload.method === 'AtomDebug.setElementMode') {
        device.elementMode = payload.params.data;
        message.payload = {
            method: 'AtomDebug.setElementMode',
            params: {
                mode: payload.params.data
            }
        };
        debuggerRouter.pushMessage('page.debugger', message.terminalId, {
            method: 'AtomDebug.reloadInspector'
        });
        debuggerRouter.pushMessage('page.debugger', message.terminalId, {
            method: 'AtomDebug.reloadRuntime'
        });
    } else if (payload.method === 'AtomDebug.network') {
        device && (device.network = payload.params.enable);
        message.payload = {
            method: 'AtomDebug.network',
            params: {
                enable: payload.params.enable
            }
        };
        debuggerRouter.pushMessage('page.debugger', message.terminalId, {
            method: 'AtomDebug.reloadInspector'
        });
        debuggerRouter.pushMessage('page.debugger', message.terminalId, {
            method: 'AtomDebug.reloadRuntime'
        });
    } else if (payload.method === 'AtomDebug.enable') {
        device && (device.remoteDebug = true);
    } else if (payload.method === 'AtomDebug.disable') {
        device && (device.remoteDebug = false);
        debuggerRouter.pushMessage('page.debugger', message.terminalId, {
            method: 'AtomDebug.reloadInspector'
        });
    } else if (payload.method === 'AtomDebug.enableTracing') {
        // todo
    } else if (payload.method === 'AtomDebug.setFrameworkLog') {
        device.isDebugger = payload.params.data;
        debuggerRouter.pushMessage('runtime.worker', message.terminalId, {
            method: 'AtomDebug.setFrameworkLog',
            params: {
                messageText: payload.params.data
            }
        });
    } else if (payload.method === 'AtomDebug.setCommandsLog') {
        device.isDebugger = payload.params.data;
        debuggerRouter.pushMessage('runtime.worker', message.terminalId, {
            method: 'AtomDebug.setCommandsLog',
            params: {
                messageText: payload.params.data
            }
        });
    } else if (payload.method === 'AtomDebug.setDebuggerInteract') {
        device.isDebugger = payload.params.data;
        debuggerRouter.pushMessage('runtime.worker', message.terminalId, {
            method: 'AtomDebug.setDebuggerInteract',
            params: {
                messageText: payload.params.data
            }
        });
    }
    message.to('proxy.native');
}).at('page.debugger');
/*eslint-disable*/