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

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

const Instance = require('./instance.component');

const addState = connect(
    function receiveState(state) {
        const { ws, instance, player, account, activeVar, vboxHidden } = state;

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

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

        return { instance, player, account, sendInstanceReady, sendVboxApply, activeVar, vboxHidden };
    },

    function receiveDispatch(dispatch) {
        function quit() {
            dispatch(actions.setInstance(null));
        }

        function setInfo(item, value) {
            dispatch(actions.setInfo([item, value]));
        }

        function setVboxHidden(v) {
            dispatch(actions.setVboxHidden(v));
        }

        function setActiveCryp(value) {
            dispatch(actions.setActiveCryp(value));
        }

        function clearInfo() {
            return dispatch(actions.setInfo([]));
        }

        return { quit, clearInfo, setInfo, setVboxHidden, setActiveCryp };
    }

);

module.exports = addState(Instance);
