package de.frajul.endlessroll.user;

import de.frajul.endlessroll.entities.shapes.PlayerShape;

/**
 * Created by Julian on 10.07.2016.
 */
public class User {

    public interface LvUpListener {
        public void onLvUp(int level);
    }

    private final int LV_DONE_EP = 20;
    private final int STAR_EP = 2;
    private final int ENERGY_EP = 10;
    //MAX EP IN ONE LEVEL: 36 (20 + 3*2 + 10)

    private LvUpListener lvUpListener;
    private int ep;
    private int level;
    private int starCount;
    private int energyCount;
    private ToolSlotSettings toolSlotSettings;
    private PlayerShape currentPlayerShape;

    private LevelUpBounties levelUpBounties;

    public User(LvUpListener lvUpListener, int ep, int level, int starCount, int energyCount, ToolSlotSettings toolSlotSettings, PlayerShape playerShape) {
        this.lvUpListener = lvUpListener;
        this.ep = ep;
        this.level = level;
        this.starCount = starCount;
        this.energyCount = energyCount;
        this.toolSlotSettings = toolSlotSettings;
        this.currentPlayerShape = playerShape;

        levelUpBounties = new LevelUpBounties(level);
    }

    public void clearData() {
        ep = 0;
        level = 1;
        starCount = 0;
        energyCount = 0;
        toolSlotSettings.reset();
        currentPlayerShape = PlayerShape.BALL;
    }

    public void onStarCollected() {
        increaseStarCount(1, true);
    }

    public void increaseStarCount(int starCount, boolean gainEp) {
        this.starCount += starCount;
        if(gainEp)
        gainEp(STAR_EP * starCount);
    }

    public void onEnergyCollected() {
        increaseEnergyCount(1, true);
    }

    public void increaseEnergyCount(int energyCount, boolean gainEp) {
        this.energyCount += energyCount;
        if(gainEp)
        gainEp(ENERGY_EP * energyCount);
    }

    public void gainLvFinishedEp() {
        gainEp(LV_DONE_EP);
    }

    public void gainEp(int amount) {
        ep += amount;
        if (ep >= 100) {
            ep -= 100;
            levelUp();
        }
    }

    private void levelUp() {
        level++;
        levelUpBounties.loadAllForLevel(level);

        toolSlotSettings.unlockSlotsIfLevelReached(levelUpBounties);
        LevelBounty bounty = levelUpBounties.get(level);
        if (bounty != null) {
            increaseStarCount(bounty.getStarCount(), false);
            increaseEnergyCount(bounty.getEnergyCount(), false);
        }
        lvUpListener.onLvUp(level);
    }

    public int getEp() {
        return ep;
    }

    public int getLevel() {
        return level;
    }

    public int getStarCount() {
        return starCount;
    }

    public int getEnergyCount() {
        return energyCount;
    }

    public ToolSlotSettings getToolSlotSettings() {
        return toolSlotSettings;
    }

    public PlayerShape getCurrentPlayerShape() {
        return currentPlayerShape;
    }

    public void setCurrentPlayerShape(PlayerShape currentPlayerShape) {
        this.currentPlayerShape = currentPlayerShape;
    }

    public LevelUpBounties getLevelUpBounties() {
        return levelUpBounties;
    }

    //CHEAT
    public void setLevel(int level) {
        this.level = level;
    }

    public void setStarCount(int starCount) {
        this.starCount = starCount;
    }
}
