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

const Vbox = require('./vbox.component');
const InfoContainer = require('./info.container');
const InstanceConstructsContainer = require('./instance.constructs');
// const EquipmentContainer = require('./instance.equip');
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.setCombiner([]));
            dispatch(actions.setReclaiming(false));
            dispatch(actions.setItemEquip(null));
            dispatch(actions.setItemUnequip([]));
            dispatch(actions.setVboxHighlight([]));
            dispatch(actions.setVboxSelected([]));
            return true;
        }

        return {
            setInfo,
            clearItems,
        };
    }
);

function Instance(args) {
    const {
        instance,
        setInfo,
        clearItems,

        nav,
    } = args;

    if (!instance) return false;

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

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

    const instanceClasses = `instance ${nav === 'constructs' ? 'constructs-visible' : ''}`;
    return (
        <main class={instanceClasses} onClick={instanceClick} onMouseOver={() => setInfo(null)} >
            <Vbox />
            <InfoContainer />
            <InstanceConstructsContainer />
        </main>
    );
}

module.exports = addState(Instance);
