import { environment } from "../../../../../environments/environment";
import { DefenseInfo, IMobFactory, MD2Icon, MobInfo } from "../../massive-darkness2.model";
import { MobSkill, MobSkillType } from "../../massive-darkness2.model.boss";

const MD2_IMG_URL = (id: string = null) => { return `${environment.apiUrl}/Files/Images/MD2/Mobs${(id ? `${encodeURI(id)}` : '')}` }
const CORE_GAME_MOB_LEVEL = [
    { name: 'Gargoyles', level: 1, hp: 2, rewardTokens: 1, defBlue: 1 },
    { name: 'Gargoyles', level: 3, hp: 3, rewardTokens: 1, defBlue: 2 },
    { name: 'Gargoyles', level: 5, hp: 6, rewardTokens: 2, defBlue: 3 },

    { name: 'Demons', level: 1, hp: 3, rewardTokens: 1, defBlue: 1 },
    { name: 'Demons', level: 3, hp: 4, rewardTokens: 1, defBlue: 2 },
    { name: 'Demons', level: 5, hp: 6, rewardTokens: 2, defBlue: 4 },

    { name: 'Undead', level: 1, hp: 4, rewardTokens: 1, defBlue: 1 },
    { name: 'Undead', level: 3, hp: 5, rewardTokens: 1, defBlue: 1 },
    { name: 'Undead', level: 5, hp: 8, rewardTokens: 2, defBlue: 1 },

    { name: 'Fire Entities', level: 1, hp: 3, rewardTokens: 1, defBlue: 1 },
    { name: 'Fire Entities', level: 3, hp: 4, rewardTokens: 1, defBlue: 2 },
    { name: 'Fire Entities', level: 5, hp: 7, rewardTokens: 2, defBlue: 3 },

    { name: 'Fallen Angels', level: 1, hp: 2, rewardTokens: 1, defBlue: 2 },
    { name: 'Fallen Angels', level: 3, hp: 3, rewardTokens: 1, defBlue: 3 },
    { name: 'Fallen Angels', level: 5, hp: 5, rewardTokens: 2, defBlue: 5 },

    { name: 'Infernal Imps', level: 1, hp: 3, rewardTokens: 1, defBlue: 1 },
    { name: 'Infernal Imps', level: 3, hp: 4, rewardTokens: 1, defBlue: 1 },
    { name: 'Infernal Imps', level: 5, hp: 5, rewardTokens: 2, defBlue: 3 },

    { name: 'Skeletons', level: 1, hp: 2, rewardTokens: 1, defBlue: 1 },
    { name: 'Skeletons', level: 3, hp: 3, rewardTokens: 1, defBlue: 2 },
    { name: 'Skeletons', level: 5, hp: 5, rewardTokens: 2, defBlue: 4 },

    { name: 'Satyrs', level: 1, hp: 3, rewardTokens: 1, defBlue: 1 },
    { name: 'Satyrs', level: 3, hp: 4, rewardTokens: 1, defBlue: 2 },
    { name: 'Satyrs', level: 5, hp: 6, rewardTokens: 2, defBlue: 4 }]

export abstract class MobFactory implements IMobFactory {
    abstract mobName: string;
    abstract generate(level: number): MobInfo
    protected mob: MobInfo;
    protected loadLevelInfo(mobName: string, level: number) {
        let levelInfo = CORE_GAME_MOB_LEVEL.find(m => m.name == mobName && level >= m.level);

        this.mob = new MobInfo({
            name: mobName, hp: levelInfo.hp, level: level, rewardTokens: levelInfo.rewardTokens,
            defenseInfo: new DefenseInfo(levelInfo.defBlue)
        });
        this.mob.leaderImgUrl = MD2_IMG_URL(`/CoreGame/Mobs/${this.mob.name}/Leader.png`);
        this.mob.minionImgUrl = MD2_IMG_URL(`/CoreGame/Mobs/${this.mob.name}/Minion.png`);

    }
    iconHtml(icon: MD2Icon, cssClass = 'g-font-size-24') {
        if (icon == MD2Icon.Fire) {
            cssClass += ' g-color-google-plus ';
        }
        if (icon == MD2Icon.Frost || icon == MD2Icon.Mana) {
            cssClass += ' g-color-aqua ';
        }

        if (icon < MD2Icon.RedDice) {
            return `<span class='MD2Icon ${cssClass}'>${String.fromCharCode(65 + icon)}</span>`

        }
        else {
            return `<span class='MD2Icon  dice ${MD2Icon[icon].replace('Dice', '')} ${cssClass}'></span>`;
        }
    }
}
export class MobDemonsFactory extends MobFactory {
    mobName: string = 'Demons';
    generate(level: number): MobInfo {
        this.loadLevelInfo('Demons', level);
        this.mob.combatSkill = new MobSkill(
            {
                description: `Attacking or defending Hero discards 1 ${this.iconHtml(MD2Icon.Mana)}`
            }
        )
        return this.mob;
    }

}
export class MobFallenAngelFactory extends MobFactory {
    mobName: string = 'Fallen Angels';
    generate(level: number): MobInfo {
        this.loadLevelInfo('Fallen Angels', level);
        this.mob.combatSkill = new MobSkill(
            {
                description: `Defender -${level == 1 ? 1 : 2} ${this.iconHtml(MD2Icon.Defense)}`,
                type: MobSkillType.Attack
            }
        )
        return this.mob;
    }
}

export class MobFireEntitiesFactory extends MobFactory {
    mobName: string = 'Fire Entities';
    generate(level: number): MobInfo {
        this.loadLevelInfo('Fire Entities', level);
        this.mob.combatSkill = new MobSkill(
            {
                description: `Add 1 ${this.iconHtml(MD2Icon.Fire)} to the attacking or defending Hero.`,
                type: MobSkillType.Combat
            }
        )
        return this.mob;
    }
}

export class MobGargoylesFactory extends MobFactory {
    mobName: string = 'Gargoyles';
    generate(level: number): MobInfo {
        this.loadLevelInfo('Gargoyles', level);
        this.mob.combatSkill = new MobSkill(
            {
                description: `+ ${level < 5 ? 1 : 2} ${this.iconHtml(MD2Icon.Defense)}`,
                type: MobSkillType.Defense
            }
        )
        return this.mob;
    }
}
export class MobInfernalImpsFactory extends MobFactory {
    mobName: string = 'Infernal Imps';
    generate(level: number): MobInfo {
        this.loadLevelInfo('Infernal Imps', level);
        let damage = 1;
        switch (level) {
            case 1:
            case 2:
                damage = 1;
                break;
            case 3:
            case 4:
                damage = 2;
                break;
            case 5:
                damage = 3;
                break;

            default:
                damage = 1;
                break;
        }
        this.mob.combatSkill = new MobSkill(
            {
                description: `Kill 1 Imp, then deal ${damage} Wound to each Hero in the attacker's Zone(once per roll).`,
                type: MobSkillType.Defense
            }
        )
        return this.mob;
    }
}
export class MobSatyrsFactory extends MobFactory {
    mobName: string = 'Satyrs';
    generate(level: number): MobInfo {
        this.loadLevelInfo('Satyrs', level);
        this.mob.combatSkill = new MobSkill(
            {
                description: `+ ${level < 3 ? 1 : 2} ${this.iconHtml(MD2Icon.Attack)}`,
                type: MobSkillType.Attack
            }
        )
        return this.mob;
    }
}

export class MobSkeletonsFactory extends MobFactory {
    mobName: string = 'Skeletons';
    generate(level: number): MobInfo {
        this.loadLevelInfo('Skeletons', level);
        this.mob.combatSkill = new MobSkill(
            {
                description: `Add 1 minion to this Mob(if possible) unless the Hero discards ${level < 5 ? 1 : 2} ${this.iconHtml(MD2Icon.Mana)}.`,
                type: MobSkillType.Defense,
                skillRoll: 2
            }
        )
        return this.mob;
    }
}

export class MobUndeadFactory extends MobFactory {
    mobName: string = 'Undead';
    generate(level: number): MobInfo {
        this.loadLevelInfo('Undead', level);

        let skillDesc = '';
        if (level < 3) {
            skillDesc = `+1 ${this.iconHtml(MD2Icon.YellowDice)}`;
        } else if (level < 5) {
            skillDesc = `+2 ${this.iconHtml(MD2Icon.YellowDice)}`;

        } else {
            skillDesc = `+1 ${this.iconHtml(MD2Icon.YellowDice)} 1 ${this.iconHtml(MD2Icon.OrangeDice)}`;

        }
        skillDesc += ' and this Mob takes 2 wounds';
        this.mob.combatSkill = new MobSkill(
            {
                description: skillDesc,
                type: MobSkillType.Attack
            }
        )
        this.mob.drawingWeight = 1;
        return this.mob;
    }
}


export const CoreGameMobFactories = [
    new MobDemonsFactory(),
    new MobFallenAngelFactory(),
    new MobFireEntitiesFactory(),
    new MobGargoylesFactory(),
    new MobInfernalImpsFactory(),
    new MobSatyrsFactory(),
    new MobSkeletonsFactory(),
    new MobUndeadFactory(),
];