package de.frajul.endlessroll.user;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import de.frajul.endlessroll.entities.tools.ToolType;

/**
 * Created by Julian on 17.07.2016.
 */
public class LevelUpBounties extends HashMap<Integer, LevelBounty> {

    private List<ToolType> unlockedTools = new ArrayList<>();
    private int unlockedToolSlots;

    public LevelUpBounties(int level) {
        super.put(1, new LevelBounty(0, 0, ToolType.RAMP, true));
        super.put(2, new LevelBounty(5, 0, ToolType.SPRING, false));
        super.put(3, new LevelBounty(6, 0, ToolType.POWER_MUSHROOM, true));
        super.put(4, new LevelBounty(7, 0, ToolType.BOMB, false));
        super.put(5, new LevelBounty(8, 2, ToolType.MAGNET, false));
        super.put(6, new LevelBounty(8, 1, null, true));
        super.put(7, new LevelBounty(8, 1, ToolType.STASIS, false));
        super.put(8, new LevelBounty(100, 100, null, false));
        super.put(9, new LevelBounty(0, 0, null, true));
        loadAllForLevel(level);
    }

    public void loadAllForLevel(int level) {
        unlockedTools.clear();
        unlockedToolSlots = 0;
        for (int i = level; i > 0; i--)
            loadBounty(super.get(i));
    }

    private void loadBounty(LevelBounty bounty) {
        if (bounty != null) {
            if (bounty.getToolType() != null)
                unlockedTools.add(bounty.getToolType());
            if (bounty.isToolSlot())
                unlockedToolSlots++;
        }
    }

    public int getLevelToolIsUnlocked(ToolType tool) {
        for (int level : this.keySet()) {
            LevelBounty bounty = this.get(level);
            if (tool.equals(bounty.getToolType()))
                return level;
        }
        return -1;
    }

    public boolean isToolLocked(ToolType tool) {
        return !unlockedTools.contains(tool);
    }

    public boolean isToolSlotLocked(int index) {
        return index >= unlockedToolSlots;
    }

}
