package de.steamwar.lobby.particle;

import de.steamwar.inventory.SWItem;
import de.steamwar.lobby.LobbySystem;
import de.steamwar.sql.SteamwarUser;
import lombok.Getter;
import org.bukkit.Material;
import org.bukkit.entity.Player;

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

public class ParticleData {

    private final Material material;
    private final String name;
    private final List<String> attributes = new ArrayList<>();

    @Getter
    private final ParticleRequirement requirement;

    @Getter
    private final ParticleElement particleElement;

    public ParticleData(Material material, String name, ParticleElement particleElement) {
        this.material = material;
        this.name = name;
        this.requirement = ParticleRequirement.NO_REQUIRMENT;
        this.particleElement = particleElement;
        particleElement.aggregateAttributes(this);
    }

    public ParticleData(Material material, String name, ParticleRequirement requirement, ParticleElement particleElement) {
        this.material = material;
        this.name = name;
        this.requirement = requirement;
        this.particleElement = particleElement;
        particleElement.aggregateAttributes(this);
    }

    public ParticleData add(String attribute) {
        attributes.add(attribute);
        return this;
    }

    public SWItem toSWItem(Player player, SteamwarUser user, Set<Integer> eventTeilnahme, String eggHuntConfig) {
        String translatedName;
        try {
            translatedName = LobbySystem.getMessage().parse(name, player);
        } catch (Exception e) {
            translatedName = name;
        }

        List<String> lore = new ArrayList<>();
        lore.add("");
        if (!attributes.isEmpty()) {
            lore.add(LobbySystem.getMessage().parse("PARTICLE_ATTRIBUTE", player));
            attributes.forEach(attribute -> lore.add(LobbySystem.getMessage().parse(attribute, player)));
            lore.add("");
        }

        String unlockedBy = requirement.getRequirementName(player);
        if (unlockedBy != null) {
            lore.add(LobbySystem.getMessage().parse("PARTICLE_UNLOCKED_BY", player));
            lore.add(unlockedBy);
            lore.add("");
        }
        if (requirement.test(user, eventTeilnahme, eggHuntConfig)) {
            lore.add(LobbySystem.getMessage().parse("PARTICLE_SELECT", player));
        } else {
            lore.add(LobbySystem.getMessage().parse("PARTICLE_LOCKED", player));
        }
        return new SWItem(material, translatedName, lore, false, clickType -> {});
    }
}
