const preact = require('preact');
const range = require('lodash/range');

function convertVar(v) {
    return v || <span>&nbsp;</span>;
    // uncomment for double borders in vbox;
    // if (v) {
    //     return <div>{v}</div>;
    // }
    // return;
}

function Vbox(args) {
    const {
        instance,
        combiner,
        reclaiming,
        sendVboxAccept,
        sendVboxDiscard,
        sendVboxReclaim,
        sendVboxCombine,
        setCombiner,
        setReclaiming,
        setInfo,
    } = args;

    const { vbox } = instance;
    if (!instance.vbox) return false;

    // lots of rubbish to make it flow nice
    function boundClick(i) {
        if (reclaiming) {
            return sendVboxReclaim(i);
        }

        const insert = combiner.findIndex(j => j === null);
        if (insert === -1) return setCombiner([i, null, null]);
        combiner[insert] = i;
        return setCombiner(combiner);
    }

    function combinerRmv(i) {
        combiner[i] = null;
        return setCombiner(combiner);
    }

    const free = [];
    for (let i = 0 ; i < 6; i++) {
        free.push([vbox.free[0][i], vbox.free[1][i], vbox.free[2][i]]);
    }

    const freeRows = free.map((row, i) => {
        const cells = row.map((c, j) => (
            <td
                key={j}
                onClick={() => { if (c) return setInfo(c, null) }}
                onDblClick={() => sendVboxAccept(j, i) }
                >
                {convertVar(c)}
            </td>
        ));

        return (
            <tr key={i}>
                {cells}
            </tr>
        );
    });

    const boundTds = range(0, 9).map(i => {
        if (combiner.indexOf(i) > -1) {
            return (
                <td
                    key={i}>
                    &nbsp;
                </td>
            );
        }
        return (
            <td
                key={i}
                draggable="true"
                onDragStart={e => e.dataTransfer.setData('text', i)}
                onClick={() => boundClick(i) }>
                {convertVar(vbox.bound[i])}
            </td>
        );
    });

    const boundRows = [
        <tr key={0} >
            {boundTds[0]}
            {boundTds[1]}
            {boundTds[2]}
        </tr>,
        <tr key={1}>
            {boundTds[3]}
            {boundTds[4]}
            {boundTds[5]}
        </tr>,
        <tr key={2}>
            {boundTds[6]}
            {boundTds[7]}
            {boundTds[8]}
        </tr>,
    ];

    const combinerElement = (
        <table className="vbox-table">
            <tbody>
                <tr>
                    <td onClick={() => combinerRmv(0)}>{convertVar(vbox.bound[combiner[0]])}</td>
                    <td onClick={() => combinerRmv(1)}>{convertVar(vbox.bound[combiner[1]])}</td>
                    <td onClick={() => combinerRmv(2)}>{convertVar(vbox.bound[combiner[2]])}</td>
                </tr>
            </tbody>
        </table>
    );

    return (
        <div className="vbox">
            <div className="vbox-hdr">
                <div>VBOX</div>
                <div className="bits" >{vbox.bits}b</div>
            </div>
            <button
                className="instance-btn instance-ui-btn vbox-btn"
                onClick={() => sendVboxDiscard()}>
                Reroll
            </button>
            <table className="vbox-table">
                <tbody>
                    {freeRows}
                </tbody>
            </table>
            <span>INVENTORY</span>
            <button
                className="instance-btn instance-ui-btn vbox-btn"
                onClick={() => setReclaiming(!reclaiming)}>
                reclaim
            </button>
            <table className="vbox-table">
                <tbody>
                    {boundRows}
                </tbody>
            </table>
            <span>I-COMBINATOR</span>
            <button
                className="instance-btn instance-ui-btn vbox-btn"
                onClick={() => sendVboxCombine()}>
                refine
            </button>
            {combinerElement}
        </div>
    );
}

module.exports = Vbox;