'use strict';

let _ = require('lodash');
let blessed = require('blessed');
let parser = require('./lib/config_parser');


let screen = blessed.screen();
screen.title = 'Hive';

let widgets = {};
let focusableWidgets = [];
let focusIndex = 0;


let buildUi = function (hosts) {

    let titlePanel = widgets.titlePanel = require('./lib/widgets/title_panel')();
    screen.append(titlePanel);

    let hostsPanel = widgets.hostsPanel = require('./lib/widgets/hosts_panel')(_.pluck(hosts, 'hostname'));
    screen.append(hostsPanel);

    let hostTitlePanel = widgets.hostTitlePanel = require('./lib/widgets/host_title_panel')();
    screen.append(hostTitlePanel);

    let hostDetailsPanel = widgets.hostDetailsPanel = require('./lib/widgets/host_details_panel')();
    screen.append(hostDetailsPanel);

    let hostActionsPanel = widgets.hostActionsPanel = require('./lib/widgets/host_actions_panel')();
    screen.append(hostActionsPanel);

    focusableWidgets.push(hostsPanel);
    focusableWidgets.push(hostDetailsPanel);
    focusableWidgets.push(hostActionsPanel);
};


let setupUiActions = function (hosts) {
    widgets.hostsPanel.on('select', function (item) {
        widgets.hostTitlePanel.updateContent(item.content);
        widgets.hostDetailsPanel.updateItems(_.find(hosts, {hostname: item.content}));
        screen.render();
    });
};


parser.parse(function (err, hosts) {
    if (err) {
        console.error('Error while parsing ssh config -', err.message);
        return process.exit(1);
    }

    buildUi(hosts);
    setupUiActions(hosts);

    screen.key('tab', function () {
        focusIndex = (focusIndex + 1) % focusableWidgets.length;
        focusableWidgets[focusIndex].focus();
    });

    screen.key('S-tab', function () {
        focusIndex = (focusIndex + focusableWidgets.length - 1) % focusableWidgets.length;
        focusableWidgets[focusIndex].focus();
    });

    screen.key(['escape', 'q', 'C-c'], function () {
        return process.exit(0);
    });

    widgets.hostsPanel.select(1);
    focusableWidgets[focusIndex].focus();
    screen.render();
    widgets.hostsPanel.enterSelected(1);
});

'use strict';

let _ = require('lodash');
let blessed = require('blessed');
let parser = require('./lib/config_parser');


let widgets = {};
let focusableWidgets = [];
let focusIndex = 0;


let buildUi = function (hosts) {
    let screen = blessed.screen();
    screen.title = 'Hive';

    // Add widgets

    let titlePanel = widgets.titlePanel = require('./lib/widgets/title_panel')();
    screen.append(titlePanel);

    let hostsPanel = widgets.hostsPanel = require('./lib/widgets/hosts_panel')(_.pluck(hosts, 'hostname'));
    screen.append(hostsPanel);

    let hostTitlePanel = widgets.hostTitlePanel = require('./lib/widgets/host_title_panel')();
    screen.append(hostTitlePanel);

    let hostDetailsPanel = widgets.hostDetailsPanel = require('./lib/widgets/host_details_panel')();
    screen.append(hostDetailsPanel);

    let hostActionsPanel = widgets.hostActionsPanel = require('./lib/widgets/host_actions_panel')();
    screen.append(hostActionsPanel);

    // Focus management

    focusableWidgets.push(hostsPanel);
    focusableWidgets.push(hostDetailsPanel);
    focusableWidgets.push(hostActionsPanel);

    // Events management

    widgets.hostsPanel.on('select', function (item) {
        widgets.hostTitlePanel.updateContent(item.content);
        widgets.hostDetailsPanel.updateItems(_.find(hosts, {hostname: item.content}));
        screen.render();
    });

    screen.key('tab', function () {
        focusIndex = (focusIndex + 1) % focusableWidgets.length;
        focusableWidgets[focusIndex].focus();
    });

    screen.key('S-tab', function () {
        focusIndex = (focusIndex + focusableWidgets.length - 1) % focusableWidgets.length;
        focusableWidgets[focusIndex].focus();
    });

    screen.key(['escape', 'q', 'C-c'], function () {
        return process.exit(0);
    });

    // Display UI

    widgets.hostsPanel.select(1);
    focusableWidgets[focusIndex].focus();
    screen.render();
    widgets.hostsPanel.enterSelected(1);
};


parser.parse(function (err, hosts) {
    if (err) {
        console.error('Error while parsing ssh config -', err.message);
        return process.exit(1);
    }

    buildUi(hosts);
});

Refactor main file organization