const preact = require('preact');
const range = require('lodash/range');
const reactStringReplace = require('react-string-replace');

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

function InfoComponent(args) {
    const {
        itemInfo,
        player,
        info,
    } = args;

    // args.info = 'Life';
    // const { info } = args;

    function Info() {
        if (!info) {
            return (
                <div>
                    <h2>VBOX phase</h2>
                    <p>in this phase you strengthen and specialise your constructs by equipping items to them.</p>
                    <p>double clicking items in the <b>VBOX</b> will purchase and move them to your <b>INVENTORY</b>.</p>
                    <p>
                        hover over an item to see its effects and combinations.<br />
                        combine a <b>SKILL</b> or <b>SPEC</b> with 2 <b>COLOURS</b> to create an item.<br />
                        combine 3 of the same item to upgrade it.<br />
                        click an item and then click a construct to equip that item to it.<br />
                    </p>
                    <p>click the <b>READY</b> button on the right to progress to the <b>GAME PHASE</b>.</p>
                </div>
            );
        }
        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) {
            const regEx = /(RedPower|BluePower|GreenPower|RedLife|BlueLife|GreenLife|SpeedStat)/;
            const infoDescription = reactStringReplace(fullInfo.description, regEx, match => shapes[match]());

            return (
                <div class="info-skill">
                    <h2>{fullInfo.item}</h2>
                    <h3>SKILL</h3>
                    <div>{infoDescription}</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 = {
                    red: [],
                    green: [],
                    blue: [],
                };
                const overFlow = [];

                colours.forEach(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'
                            : '';

                        if (j - start > 4) {
                            overFlow.push(
                                <figure key={j} alt={c.colour} class={reqClass} >
                                    {shapes.vboxColour(c)}
                                </figure>
                            );
                        } else {
                            colourGoals[c].push(
                                <figure key={j} alt={c.colour} class={reqClass} >
                                    {shapes.vboxColour(c)}
                                </figure>
                            );
                        }

                        return true;
                    });

                    return dots;
                });

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

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

                const goals = colours.map((c, j) => {
                    if (colourGoals[c].length) {
                        return (
                            <div key={j}>{colourGoals[c]}</div>
                        );
                    }
                    return false;
                });
                const overFlowObj = overFlow.length ? <div> {overFlow} </div> : null;
                return (
                    <div key={i} class="spec-goal">
                        {goals}
                        {overFlowObj}
                        <div class={`${reqClass} bonus`} >
                            + {bonus.bonus}
                        </div>
                    </div>
                );
            });
            const regEx = /(RedPower|BluePower|GreenPower|RedLife|BlueLife|GreenLife|SpeedStat)/;
            const infoDescription = reactStringReplace(fullInfo.description, regEx, match => shapes[match]());

            return (
                <div class="info-spec">
                    <h2>{info}</h2>
                    <h3>SPEC</h3>
                    <div>{infoDescription}</div>
                    <div class="thresholds">
                        {thresholds}
                    </div>
                </div>
            );
        }

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

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

        const vboxCombos = itemInfo.combos.filter(c => c.components.includes(info));
        if (vboxCombos.length > 6) return false;

        return (
            <table class="combos">
                <tbody>
                    {vboxCombos.map((c, i) =>
                        <tr key={i} >
                            <td class="highlight" >{convertItem(c.item)}</td>
                            {c.components.map((u, j) => <td key={j} >{convertItem(u)}</td>)}
                        </tr>
                    )}
                </tbody>
            </table>
        );
    }

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

module.exports = InfoComponent;
