'use strict';

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

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') {
        hook.record('/weex_tool.weex_debugger.scenes', {
            feature: 'setLogLevel',
            status: payload.params.data
        });
        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') {
        hook.record('/weex_tool.weex_debugger.scenes', {
            feature: 'setElementMode',
            status: payload.params.data
        });

        device.elementMode = payload.params.data;
        message.payload = {
            method: 'AtomDebug.setElementMode',
            params: {
                mode: payload.params.data
            }
        };
        // setTimeout(function(){})
        debuggerRouter.pushMessage('page.debugger', message.terminalId, {
            method: 'AtomDebug.reloadInspector'
        });
        debuggerRouter.pushMessage('page.debugger', message.terminalId, {
            method: 'AtomDebug.reloadRuntime'
        });
    } else if (payload.method === 'AtomDebug.network') {
        hook.record('/weex_tool.weex_debugger.scenes', {
            feature: 'network',
            status: payload.params.enable
        });
        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') {
        hook.record('/weex_tool.weex_debugger.scenes', {
            feature: 'remoteDebug',
            status: true
        });
        device && (device.remoteDebug = true);
        // debuggerRouter.pushMessage('page.debugger', message.terminalId, {
        //   method: 'AtomDebug.reloadInspector'
        // });
        // debuggerRouter.pushMessage('page.debugger', message.terminalId, {
        //   method: 'AtomDebug.reloadRuntime'
        // });
    } else if (payload.method === 'AtomDebug.disable') {
        hook.record('/weex_tool.weex_debugger.scenes', {
            feature: 'remoteDebug',
            status: false
        });
        device && (device.remoteDebug = false);
        debuggerRouter.pushMessage('page.debugger', message.terminalId, {
            method: 'AtomDebug.reloadInspector'
        });
    } else if (payload.method === 'AtomDebug.enableTracing') {
        hook.record('/weex_tool.weex_debugger.scenes', {
            feature: 'Tracing',
            status: payload.params.status
        });
    } 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
            }
        });
    }
    message.to('proxy.native');
}).at('page.debugger');
/*eslint-disable*/