package com.bitnix.dirtguilds.guild;

import org.bukkit.inventory.ItemStack;

import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

public class Guild {

    private final UUID id;
    private String name;
    private String prefix;
    private UUID owner;
    private int level;
    private double bankBalance;
    private boolean friendlyFire;
    private final Map<UUID, String> memberRanks;
    private ItemStack[] vaultContents;

    public Guild(UUID id, String name, String prefix, UUID owner, int level, double bankBalance, boolean friendlyFire, Map<UUID, String> memberRanks, ItemStack[] vaultContents) {
        this.id = id;
        this.name = name;
        this.prefix = prefix;
        this.owner = owner;
        this.level = level;
        this.bankBalance = bankBalance;
        this.friendlyFire = friendlyFire;
        this.memberRanks = new HashMap<>(memberRanks);
        this.memberRanks.put(owner, "OWNER");
        this.vaultContents = vaultContents == null ? new ItemStack[9] : vaultContents;
    }

    public UUID getId() {
        return id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getPrefix() {
        return prefix;
    }

    public void setPrefix(String prefix) {
        this.prefix = prefix;
    }

    public UUID getOwner() {
        return owner;
    }

    public void setOwner(UUID owner) {
        this.owner = owner;
        this.memberRanks.put(owner, "OWNER");
    }

    public int getLevel() {
        return level;
    }

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

    public double getBankBalance() {
        return bankBalance;
    }

    public void setBankBalance(double bankBalance) {
        this.bankBalance = bankBalance;
    }

    public boolean isFriendlyFire() {
        return friendlyFire;
    }

    public void setFriendlyFire(boolean friendlyFire) {
        this.friendlyFire = friendlyFire;
    }

    public Map<UUID, String> getMemberRanks() {
        return memberRanks;
    }

    public boolean isMember(UUID uuid) {
        return memberRanks.containsKey(uuid);
    }

    public void addMember(UUID uuid) {
        memberRanks.putIfAbsent(uuid, "MEMBER");
    }

    public void removeMember(UUID uuid) {
        memberRanks.remove(uuid);
    }

    public String getRank(UUID uuid) {
        return memberRanks.getOrDefault(uuid, "MEMBER");
    }

    public void setRank(UUID uuid, String rank) {
        memberRanks.put(uuid, rank);
    }

    public boolean canManage(UUID uuid) {
        String rank = getRank(uuid).toUpperCase();
        return uuid.equals(owner) || rank.equals("OWNER") || rank.equals("ADMIN") || rank.equals("MOD");
    }

    public ItemStack[] getVaultContents() {
        return vaultContents;
    }

    public void setVaultContents(ItemStack[] vaultContents) {
        this.vaultContents = vaultContents;
    }
}
