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

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

// const SKILL_HOT_KEYS = ['Q', 'W', 'E', 'R'];

function GamePanel(props) {
    const {
        game,
        activeSkill,
        setActiveSkill,
        selectSkillTarget,
        account,
        quit,
    } = props;

    if (!game) return <div>...</div>;

    function findCryp(id) {
        const team = game.teams.find(t => t.cryps.find(c => c.id === id));
        if (team) return team.cryps.find(c => c.id === id);
        return null;
    }

    const otherTeams = game.teams.filter(t => t.id !== account.id);

    const playerTeam = game.teams.find(t => t.id === account.id);

    function stackElement(c, i) {
        let skills = game.stack.filter(s => s.source_cryp_id === c.id).map((s, j) => {
            const target = findCryp(s.target_cryp_id);
            return (
                <div key={j}>{s.skill} -> {target.name}</div>
            );
        });

        if (!skills.length) skills = (<div>&nbsp;</div>);

        return (
            <div
                key={i}
                className="stack-line">
                {skills}
            </div>
        );
    }

    function Cryp(cryp) {
        const ko = cryp.hp.value === 0 ? 'ko' : '';

        const skills = range(0, 4).map(i => {
            const s = cryp.skills[i];
            if (!s) {
                return (
                    <button
                        disabled='true'
                        key={i}
                        className='cryp-skill-btn disabled'>
                        <span>&nbsp;</span>
                    </button>
                );
            }

            const cdText = cryp.skills[i].cd > 0
                ? `- ${s.cd}`
                : '';

            const highlight = activeSkill
                ? activeSkill.crypId === cryp.id && activeSkill.skill === s
                : false;

            return (
                <button
                    key={i}
                    disabled={!!cdText || ko}
                    className={`cryp-skill-btn right ${highlight ? 'active' : ''}`}
                    type="submit"
                    onClick={() => setActiveSkill(cryp.id, s.skill)}>
                    {s.skill} {cdText}
                </button>
            );
        });

        const stats = [
            { stat: 'hp', colour: '#1FF01F' },
            { stat: 'red_shield', colour: '#a52a2a' },
            { stat: 'blue_shield', colour: '#3498db' },
        ].map((s, i) => (
            <figure key={i} alt={s.stat}>
                <figcaption>{cryp[s.stat].value} / {cryp[s.stat].max}</figcaption>
            </figure>
        ));


        return (
            <div
                key={cryp.id}
                style={ activeSkill ? { cursor: 'pointer' } : {}}
                className={`cryp-box ${ko}`} >
                <div className="cryp-box-top">
                    <figure
                        className="img"
                        onClick={() => selectSkillTarget(cryp.id)} >
                        {molecule}
                        <figcaption>{cryp.name}</figcaption>
                    </figure>
                    <div className="skills">
                        {skills}
                    </div>
                </div>
                <div className="stats">
                    {stats}
                </div>
            </div>
        );
    }

    function PlayerTeam(team) {
        const cryps = team.cryps.map(c => Cryp(c));

        return (
            <div className="team-player cryp-list">
                {cryps}
            </div>
        );
    }

    function OpponentCryp(cryp, i) {
        const stats = [
            { stat: 'hp', colour: '#1FF01F' },
            { stat: 'red_shield', colour: '#a52a2a' },
            { stat: 'blue_shield', colour: '#3498db' },
        ].map((s, j) => (
            <figure key={j} alt={s.stat}>
                <figcaption>{cryp[s.stat].value} / {cryp[s.stat].max}</figcaption>
            </figure>
        ));

        return (
            <div
                key={i}
                className="cryp-box"
                style={ activeSkill ? { cursor: 'pointer' } : {}}
                onClick={() => selectSkillTarget(cryp.id)} >
                <div className="cryp-box-top">
                    <figure className="img">
                        {molecule}
                        <figcaption>{cryp.name}</figcaption>
                    </figure>
                </div>
                <div className="stats">
                    {stats}
                </div>
            </div>
        );
    }


    function OpponentTeam(team) {
        const cryps = team.cryps.map(OpponentCryp);
        return (
            <div className="cryp-list" >
                {cryps}
            </div>
        );
    }

    const selectedSkills = playerTeam.cryps.map((c, i) => stackElement(c, i));

    const logs = game.log.map((l, i) => (<div key={i}>{l}</div>)).reverse();

    return (
        <main>
            <div className="instance-hdr">
                <button
                    className="game-back-btn instance-btn instance-ui-btn left"
                    onClick={quit}>
                    Back
                </button>
                <div className="spacer">
                    <div>&nbsp;</div>
                </div>
            </div>
            {PlayerTeam(playerTeam, setActiveSkill)}
            <div className="selected-skills">
                {selectedSkills}
            </div>
            <div className="team-opponent">
                {otherTeams.map(OpponentTeam)}
            </div>
            <div className="logs">
                {logs}
            </div>
        </main>
    );
}

module.exports = GamePanel;
