package moe.oko.opennaw.util;

import moe.oko.opennaw.OpenNAW;
import moe.oko.opennaw.model.Gate;
import org.bukkit.block.Block;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.UUID;

public class GateHandler {
    private HashMap<UUID, Gate> gates = new HashMap<>();

    public GateHandler() {
    }

    public Gate getGateByUuid(UUID uid) {
        return gates.get(uid);
    }

    public Gate getGateBySign(Block block) {
        for(var gate : gates.values()) {
            for(var sign : gate.getSigns().values()) {
                if(sign.getBlock().equals(block)) {
                    return gate;
                }
            }
        }
        return null; //no gate.
    }

    public UUID getGateUidByName(String name) {
        for(var gate : gates.values()) {
            if(gate.getName().equals(name)) {
                return gate.getUuid();
            }
        }
        return null;
    }

    public Gate getGateByName(String name) {
        for(var gate : gates.values()) {
            if(gate.getName().equals(name)) {
                return gate;
            }
        }
        return null;
    }

    public HashMap<UUID, Gate> getGates() {
        return gates;
    }

    public List<String> getGateNames() {
        List<String> names = new ArrayList<>();
        for(var gate : gates.values()) {
            names.add(gate.getName());
        }
        return names;
    }

    public void setGates(HashMap<UUID, Gate> gates) {
        this.gates = gates;
    }

    public void closeAllGates() {
        var i = 0;
        for(var gate : gates.values()) {
            gate.close();
            i++;
        }
        OpenNAW.getInstance().getLogger().info("Closed " + i + " gates.");
    }

    public void saveGates() {
        OpenNAW.getInstance().getDatabase().saveGates(gates.values());
    }
}
