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

const { INFO } = require('./../constants');
const { convertItem } = require('../utils');
const shapes = require('./shapes');

function InfoComponent(args) {
    const {
        info,
        itemInfo,

        combiner,
        instance,
        account,
    } = args;

    const player = instance.players.find(p => p.id === account.id);

    function Info() {
        if (!info) return false;
        const fullInfo = itemInfo.items.find(i => i.item === info) || INFO[info];
        if (!fullInfo) return false;
        const isSkill = fullInfo.skill;
        const isSpec = fullInfo.spec;

        if (isSkill) {
            return (
                <div className="info-skill">
                    <h2>{fullInfo.item}</h2>
                    <div>{fullInfo.description}</div>
                </div>
            );
        }

        if (isSpec) {
            let red = 0;
            let blue = 0;
            let green = 0;
            player.constructs.forEach(construct => {
                red += construct.colours.red;
                blue += construct.colours.blue;
                green += construct.colours.green;
            });
            const teamColours = { red, blue, green };

            const colourReqs = fullInfo.values.bonuses || [];

            const thresholds = colourReqs.map((bonus, i) => {
                const colours = ['red', 'green', 'blue'];
                const colourGoals = colours.map(c => {
                    const colourReq = bonus.req[c];
                    if (colourReqs === 0) return false;

                    const start = i === 0
                        ? 0
                        : colourReqs[i - 1].req[c];

                    const dots = range(start, colourReq).map(j => {
                        const unmet = teamColours[c] < j + 1;

                        const reqClass = unmet
                            ? 'unmet'
                            : '';

                        return (
                            <figure key={j} alt={c.colour} className={reqClass} >
                                {shapes.square([c])}
                            </figure>
                        );
                    });

                    return (
                        <div key={c}>
                            {dots}
                        </div>
                    );
                });

                const reqsMet = colours.every(c => teamColours[c] >= bonus.req[c]);

                const reqClass = reqsMet
                    ? ''
                    : 'unmet';

                return (
                    <div key={i} className="spec-goal">
                        <div className="colour-reqs">
                            {colourGoals}
                        </div>
                        <div className={reqClass}>
                            + {bonus.bonus}
                            <hr />
                        </div>
                    </div>
                );
            });

            return (
                <div className="info-spec">
                    <h2>{info}</h2>
                    <div>{fullInfo.description}</div>
                    <div className="thresholds">
                        {thresholds}
                    </div>
                </div>
            );
        }

        return (
            <div className="info-item">
                <h2>{fullInfo.item}</h2>
                <div>{fullInfo.description}</div>
            </div>
        );
    }

    function Combos() {
        if (!player) return false;

        // show recipe for what's in combiner
        if (combiner.some(u => u !== null)) {
            const filteredCombos = itemInfo.combos
                .filter(combo => combiner.every(u => u === null
                    || combo.components.includes(player.vbox.bound[u])));
            if (filteredCombos.length > 6) return false;
            return (
                <table>
                    <tbody>
                        {filteredCombos.map((c, i) =>
                            <tr key={i} >
                                <td className="highlight" >{convertItem(c.item)}</td>
                                {c.components.map((u, j) => <td key={j}>{convertItem(u)}</td>)}
                            </tr>
                        )}
                    </tbody>
                </table>
            );
        }

        if (!info) return false;
        const vboxCombos = itemInfo.combos.filter(c => c.components.includes(info));
        if (vboxCombos.length > 6) return false;
        return (
            <table className="combos">
                <tbody>
                    {vboxCombos.map((c, i) =>
                        <tr key={i} >
                            <td className="highlight" >{convertItem(c.item)}</td>
                            {c.components.map((u, j) => <td key={j} >{convertItem(u)}</td>)}
                        </tr>
                    )}
                </tbody>
            </table>
        );
    }

    // const beginningHdr = instance.phase === 'Lobby'
    //     ? <h2>game beginning...</h2>
    //     : null;

    return (
        <div className='info' >
            <Info />
            <Combos />
        </div>
    );
}

module.exports = InfoComponent;
