package cz.jzitnik.game.entities;

import cz.jzitnik.game.SpriteLoader;
import cz.jzitnik.game.entities.items.Item;
import cz.jzitnik.game.entities.items.ItemType;
import cz.jzitnik.game.entities.items.ToolVariant;
import cz.jzitnik.game.ui.Inventory;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;

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

@Getter
@Setter
@NoArgsConstructor
public class Block {
    private String blockId;
    private SpriteLoader.SPRITES sprite;
    private MyOptional<Enum> spriteState = MyOptional.empty();
    private boolean ghost = false;
    private boolean isMineable = true;
    private int hardness = 1;
    private MyOptional<ItemType> tool = MyOptional.empty();
    private List<ToolVariant> toolVariants = new ArrayList<>();
    private List<Item> drops = new ArrayList<>();
    private Object data = null;
    private boolean flowing = false;
    private boolean isMob = false;
    private int hp = 0;
    private boolean onFire = false;
    private int burningTime = 0;
    private int burningTime2 = 0;
    private Block linkedMobTexture;

    public Block(String blockId, SpriteLoader.SPRITES sprite) {
        this.blockId = blockId;
        this.sprite = sprite;
    }

    public Block(String blockId, SpriteLoader.SPRITES sprite, boolean ghost, boolean isMineable) {
        this.blockId = blockId;
        this.sprite = sprite;
        this.ghost = ghost;
        this.isMineable = isMineable;
    }

    public Block(String blockId, SpriteLoader.SPRITES sprite, int hardness) {
        this.blockId = blockId;
        this.sprite = sprite;
        this.hardness = hardness;
    }

    public Block(String blockId, SpriteLoader.SPRITES sprite, int hardness, ItemType tool,
            List<ToolVariant> toolVariants) {
        this.blockId = blockId;
        this.sprite = sprite;
        this.hardness = hardness;
        this.tool = MyOptional.of(tool);
        this.toolVariants = toolVariants;
    }

    public Block(String blockId, SpriteLoader.SPRITES sprite, int hardness, ItemType tool,
            List<ToolVariant> toolVariants, Object data) {
        this.blockId = blockId;
        this.sprite = sprite;
        this.hardness = hardness;
        this.tool = MyOptional.of(tool);
        this.toolVariants = toolVariants;
        this.data = data;
    }

    public void setSpriteState(Enum spriteState) {
        this.spriteState = MyOptional.of(spriteState);
    }

    public void setSpriteState() {
        this.spriteState = MyOptional.empty();
    }

    public double calculateHardness(Inventory inventory) {
        double holdingDecrease = 0;
        if (inventory.getItemInHand().isPresent() && tool.isPresent()
                && inventory.getItemInHand().get().getType().equals(tool.get())) {
            holdingDecrease = inventory.getItemInHand().get().getMiningDecrease();
        }

        double decrease = hardness - holdingDecrease;

        if (decrease < 0) {
            decrease = 0;
        }

        return decrease;
    }

    public void decreaseHp(int amount) {
        hp -= amount;
    }
}
