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

const Vbox = require('./vbox.component');
const InstanceConstructsContainer = require('./instance.constructs');
const Faceoff = require('./faceoff');

const actions = require('../actions');

const addState = connect(
    function receiveState(state) {
        const {
            instance,
            nav,
        } = state;
        return {
            instance,
            nav,
        };
    },

    function receiveDispatch(dispatch) {
        function setInfo(c) {
            return dispatch(actions.setInfo(c));
        }


        function clearItems() {
            dispatch(actions.setItemUnequip([]));
            dispatch(actions.setVboxSelected({ storeSelect: [], stashSelect: [] }));
            return true;
        }

        return {
            setInfo,
            clearItems,
        };
    }
);

function Instance(args) {
    const {
        instance,
        clearItems,
    } = args;

    if (!instance) return false;

    if (instance.phase !== 'InProgress') {
        return <Faceoff />;
    }

    function instanceClick(e) {
        e.stopPropagation();
        clearItems();
    }

    return (
        <main id="instance" class='instance' onClick={instanceClick}>
            <Vbox />
            <InstanceConstructsContainer />
        </main>
    );
}

module.exports = addState(Instance);
