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

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

const Vbox = require('./vbox.component');

const addState = connect(
    function receiveState(state) {
        const {
            ws,
            instance,
            player,
            combiner,
            reclaiming,
            activeVar,
            activeCryp,
            info,
            vboxHidden,
            vboxInfo,
        } = state;

        function sendVboxApply(i) {
            return ws.sendVboxApply(instance.id, activeCryp.id, i);
        }

        function sendVboxDiscard() {
            return ws.sendVboxDiscard(instance.id);
        }

        function sendVboxAccept(group, index) {
            return ws.sendVboxAccept(instance.id, group, index);
        }

        function sendVboxCombine() {
            return ws.sendVboxCombine(instance.id, combiner);
        }

        function sendVboxReclaim(i) {
            return ws.sendVboxReclaim(instance.id, i);
        }

        return {
            instance,
            player,
            combiner,
            reclaiming,
            activeVar,
            activeCryp,
            info,
            vboxInfo,
            vboxHidden,
            sendVboxAccept,
            sendVboxDiscard,
            sendVboxReclaim,
            sendVboxCombine,
            sendVboxApply,
        };
    },

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

        function setReclaiming(v) {
            return dispatch(actions.setReclaiming(v));
        }

        function setInfo(type, info) {
            return dispatch(actions.setInfo([type, info]));
        }

        function setActiveCryp(v) {
            return dispatch(actions.setActiveCryp(v));
        }

        function showTeam() {
            dispatch(actions.setInfo([null, null]));
            dispatch(actions.setVboxHidden(true));
            return true;
        }

        return {
            setCombiner,
            setReclaiming,
            setInfo,
            setActiveCryp,
            showTeam,
        };
    }

);

module.exports = addState(Vbox);
