const preact = require('preact');
const { connect } = require('preact-redux');

const PlayCtrl = require('./play.ctrl');
const InstanceCtrl = require('./instance.ctrl');
const GameCtrl = require('./game.ctrl');
const TeamCtrl = require('./team.ctrl');

const addState = connect(
    function receiveState(state) {
        const {
            ws,
            account,
            game,
            instance,
            nav,
        } = state;

        return {
            account,
            game,
            instance,
            nav,
        };
    },
);

function Controls(args) {
    const {
        game,
        account,
        instance,
        nav,
        sendGameReady,
    } = args;

    if (!account) return false;

    if (game) return <GameCtrl />;
    if (instance) return <InstanceCtrl />;
    if (nav === 'play' || nav === 'shop' || nav === 'reshape' || !nav) return <PlayCtrl />
    if (nav === 'team' || nav === 'account') return <TeamCtrl />

    return false;
}

module.exports = addState(Controls);
