package de.frajul.endlessroll.user;

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

import java.util.ArrayList;

/**
 * Created by Julian on 15.07.2016.
 */
public class ToolSlotSettings extends ArrayList<ToolSlot> {

    public ToolSlotSettings(String slot1, String slot2, String slot3, String slot4, int slotsLocked) throws Exception {
        super.add(new ToolSlot(slot1.equals("null") ? null : ToolType.valueOf(slot1), false));
        super.add(new ToolSlot(slot2.equals("null") ? null : ToolType.valueOf(slot2), slotsLocked >= 3));
        super.add(new ToolSlot(slot3.equals("null") ? null : ToolType.valueOf(slot3), slotsLocked >= 2));
        super.add(new ToolSlot(slot4.equals("null") ? null : ToolType.valueOf(slot4), slotsLocked >= 1));
    }

    public void reset() {
        super.set(0, new ToolSlot(ToolType.RAMP, false));
        super.set(1, new ToolSlot(null, true));
        super.set(2, new ToolSlot(null, true));
        super.set(3, new ToolSlot(null, true));
    }

    public void changeToolSlotType(int slot, ToolType newType) {
        for (ToolSlot toolSlot : this) {
            if (super.indexOf(toolSlot) == slot)
                toolSlot.setToolType(newType);
            else if (toolSlot.getToolType() == newType)
                toolSlot.setToolType(null);
        }
    }

    public void unlockSlotsIfLevelReached(LevelUpBounties levelUpBounties) {
        for (int i = 0; i < 4; i++)
            super.get(i).setLocked(levelUpBounties.isToolSlotLocked(i));
    }

    public int getLockedSlotCount() {
        int count = 0;
        for (ToolSlot slot : this)
            if(slot.isLocked())
                count ++;
        return count;
    }

}
