package de.viper.survivalplus.util;

import org.bukkit.Location;
import org.bukkit.entity.Player;

import java.util.HashSet;
import java.util.Set;
import java.util.UUID;

public class Claim {
    private UUID owner;
    private Set<UUID> trusted = new HashSet<>();
    private int x1, z1, x2, z2;
    private String worldName;

    public Claim(Player owner, String worldName, int x1, int z1, int x2, int z2) {
        this.owner = owner.getUniqueId();
        this.worldName = worldName;
        this.x1 = Math.min(x1, x2);
        this.z1 = Math.min(z1, z2);
        this.x2 = Math.max(x1, x2);
        this.z2 = Math.max(z1, z2);
        trusted.add(owner.getUniqueId());
    }

    public Claim(UUID owner, String worldName, int x1, int z1, int x2, int z2) {
        this.owner = owner;
        this.worldName = worldName;
        this.x1 = Math.min(x1, x2);
        this.z1 = Math.min(z1, z2);
        this.x2 = Math.max(x1, x2);
        this.z2 = Math.max(z1, z2);
        trusted.add(owner);
    }

    public boolean canInteract(UUID uuid) {
        return trusted.contains(uuid);
    }

    public void addTrusted(UUID uuid) {
        trusted.add(uuid);
    }

    public void removeTrusted(UUID uuid) {
        trusted.remove(uuid);
    }

    public UUID getOwner() {
        return owner;
    }

    public Set<UUID> getTrusted() {
        return trusted;
    }

    public int getX1() {
        return x1;
    }

    public int getZ1() {
        return z1;
    }

    public int getX2() {
        return x2;
    }

    public int getZ2() {
        return z2;
    }

    public String getWorldName() {
        return worldName;
    }

    public boolean isInside(Location location) {
        if (!location.getWorld().getName().equals(worldName)) {
            return false;
        }
        int x = location.getBlockX();
        int z = location.getBlockZ();
        return x >= x1 && x <= x2 && z >= z1 && z <= z2;
    }

    public boolean overlaps(Claim other) {
        if (!this.worldName.equals(other.worldName)) {
            return false;
        }
        return !(this.x2 < other.x1 || this.x1 > other.x2 || this.z2 < other.z1 || this.z1 > other.z2);
    }
}