const get = require('lodash/get');

const shapes = require('./components/shapes');

const stringSort = (k, desc) => {
    if (desc) {
        return (a, b) => {
            if (!get(a, k)) return 1;
            if (!get(b, k)) return -1;
            return get(b, k).localeCompare(get(a, k));
        };
    }
    return (a, b) => {
        if (!get(a, k)) return 1;
        if (!get(b, k)) return -1;
        return get(a, k).localeCompare(get(b, k));
    };
};

const numSort = (k, desc) => {
    if (desc) {
        return (a, b) => {
            if (!get(a, k)) return 1;
            if (!get(b, k)) return -1;
            return get(b, k) - get(a, k);
        };
    }
    return (a, b) => {
        if (!get(a, k)) return 1;
        if (!get(b, k)) return -1;
        return get(a, k) - get(b, k);
    };
};

const genAvatar = (name) => {
    let hash = 0;
    if (name.length === 0) return hash;
    // Probs don't need to hash using the whole string
    for (let i = 0; i < name.length; i += 1) {
        const chr = name.charCodeAt(i);
        hash = ((hash << 5) - hash) + chr;
        hash = hash & 10000; // We have avatars named 0-19
    }
    return `sprite${hash}`;
};

function requestAvatar(name) {
    const id = genAvatar(name);
    const req = new Request(`/assets/molecules/${id}.svg`);
    return fetch(req)
        .then(res => res.text())
        .then(svg => svg);
}

const NULL_UUID = '00000000-0000-0000-0000-000000000000';

const STATS = {
    speed: { stat: 'speed', colour: 'white', svg: shapes.triangle },
    redDamage: { stat: 'red_damage', colour: 'red', svg: shapes.circle },
    greenDamage: { stat: 'green_damage', colour: 'green', svg: shapes.circle },
    blueDamage: { stat: 'blue_damage', colour: 'blue', svg: shapes.circle },
    redLife: { stat: 'red_life', colour: 'red', svg: shapes.square },
    greenLife: { stat: 'green_life', colour: 'green', svg: shapes.square },
    blueLife: { stat: 'blue_life', colour: 'blue', svg: shapes.square },
};

const SPECS = {
    Life: { colour: 'white', caption: 'Life', svg: shapes.square },
    GreenLifeI: { colour: 'green', caption: 'Life', svg: shapes.square },
    RedLifeI: { colour: 'red', caption: 'Life', svg: shapes.square },
    BlueLifeI: { colour: 'blue', caption: 'Life', svg: shapes.square },
    GRLI: { colour: 'yellow', caption: 'Life', svg: shapes.square },
    GBLI: { colour: 'cyan', caption: 'Life', svg: shapes.square },
    RBLI: { colour: 'purple', caption: 'Life', svg: shapes.square },

    Damage: { colour: 'white', caption: 'Damage', svg: shapes.circle },
    RedDamageI: { colour: 'red', caption: 'DamageI', svg: shapes.circle },
    BlueDamageI: { colour: 'blue', caption: 'DamageI', svg: shapes.circle },
    GreenDamageI: { colour: 'green', caption: 'DamageI', svg: shapes.circle },
    GRDI: { colour: 'yellow', caption: 'DamageI', svg: shapes.circle },
    GBDI: { colour: 'cyan', caption: 'DamageI', svg: shapes.circle },
    RBDI: { colour: 'purple', caption: 'DamageI', svg: shapes.circle },

    Speed: { colour: 'white', caption: 'Speed', svg: shapes.diamond },
    RedSpeed: { colour: 'red', caption: 'Speed', svg: shapes.diamond },
    BlueSpeedI: { colour: 'blue', caption: 'Speed', svg: shapes.diamond },
    GreenSpeedI: { colour: 'green', caption: 'Speed', svg: shapes.diamond },
    GRSpeedI: { colour: 'yellow', caption: 'Speed', svg: shapes.diamond },
    GBSpeedI: { colour: 'cyan', caption: 'Speed', svg: shapes.diamond },
    RBSpeedI: { colour: 'purple', caption: 'Speed', svg: shapes.diamond },

};

const COLOUR_ICONS = {
    red: { colour: 'red', caption: 'red', svg: shapes.square },
    blue: { colour: 'blue', caption: 'blue', svg: shapes.square },
    green: { colour: 'green', caption: 'green', svg: shapes.square },
};

function eventClasses(resolution) {
    const [type, event] = resolution.event;

    if (type === 'Ko') {
        return 'ko';
    }

    if (type === 'Disable') {
        const { skill, disable } = event;
    }

    if (type === 'Immunity') {
        const { skill, immunity } = event;
    }

    if (type === 'TargetKo') {
        const { skill } = event;
    }

    if (type === 'Damage') {
        const { skill, amount, mitigation, colour } = event;
    }

    if (type === 'Healing') {
        const { skill, amount, overhealing } = event;
    }

    if (type === 'Inversion') {
        const { skill } = event;
    }

    if (type === 'Reflection') {
        const { skill } = event;
    }

    if (type === 'Effect') {
        const { skill, effect, duration } = event;
    }

    if (type === 'Removal') {
        const { effect } = event;
    }

    if (type === 'Recharge') {
        const { skill, red, blue } = event;
    }

    if (type === 'Evasion') {
        const { skill, evasion_rating } = event;
    }

    return false;
}

module.exports = {
    stringSort,
    numSort,
    genAvatar,
    requestAvatar,
    NULL_UUID,
    STATS,
    SPECS,
    COLOUR_ICONS,
};
