package moe.oko.opennaw.model;

import moe.oko.opennaw.OpenNAW;
import org.bukkit.Location;
import org.bukkit.Material;
import org.bukkit.Sound;
import org.bukkit.block.Block;

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

public class Gate {
    private String name;

    private UUID uuid;

    private boolean open;

    private HashMap<UUID, GateBlock> signs = new HashMap<>();
    private HashMap<GateBlock, Material> gateBlocks;

    //New gate constructor.
    public Gate(String name) {
        this.name = name;
        this.uuid = UUID.randomUUID();
        this.open = false;
        gateBlocks = new HashMap<>();
    }

    //Db Const
    public Gate(UUID uid, String name) {
        this.uuid = uid;
        this.name = name;
        this.open = false;
        gateBlocks = new HashMap<>();
    }

    public String getName() {
        return name;
    }

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

    public UUID getUuid() {
        return uuid;
    }

    public HashMap<UUID, GateBlock> getSigns() {
        return signs;
    }

    public HashMap<GateBlock, Material> getGateBlocks() {
        return gateBlocks;
    }

    public void registerSign(Block block) {
        var gate = new GateBlock(block);
        if(signs.containsValue(gate)) {
            signs.replace(gate.getId(), gate);
            return;
        }
        signs.put(gate.getId(), gate);
    }

    public void registerSignFromDB(GateBlock block) {
        if(signs.containsValue(block)) {
            signs.replace(block.getId(), block);
            return;
        }
        signs.put(block.getId(), block);
    }

    public boolean isGateSign(Location location) {
        for(var block : signs.values()) {
            if(block.getBlock().getLocation().equals(location)) { return true; }
        }
        return false;
    }

    public void removeSignBlock(Block block) {
        for(var blocks : signs.values()) {
            if(blocks.getBlock().equals(block)) {
                signs.remove(blocks.getId());
            }
        }
    }

    public boolean registerGateBlock(Block block) {
        var newBlock = new GateBlock(block);
        if(gateBlocks.containsKey(newBlock)) { return false; }
        gateBlocks.put(newBlock, newBlock.getBlock().getType());
        return true;
    }

    public void registerGateBlockDb(GateBlock block) {
        if(gateBlocks.containsKey(block)) {
            signs.replace(block.getId(), block);
            return;
        }
        gateBlocks.put(block, block.getBlock().getType());
    }

    public void toggle() {
        if(!open) {
            open();
            open = !open;
        } else {
            close();
            open = !open;
        }
    }

    public boolean open() {
        try {
            for (var block : gateBlocks.keySet()) {
                block.getBlock().setType(Material.AIR);
                block.getBlock().getWorld().playSound(block.getBlock().getLocation(), Sound.BLOCK_PISTON_CONTRACT, 1, 1);
            }
            return true;
        } catch (Exception ex) {
            ex.printStackTrace();
            OpenNAW.getInstance().getLogger().severe("Failed to open gate " + name);
            return false;
        }
    }

    public boolean close() {
        try {
            for (var block : gateBlocks.keySet()) {
                block.getBlock().setType(gateBlocks.get(block));
                block.getBlock().getWorld().playSound(block.getBlock().getLocation(), Sound.BLOCK_PISTON_EXTEND, 1, 1);
            }
            return true;
        } catch (Exception ex) {
            ex.printStackTrace();
            OpenNAW.getInstance().getLogger().severe("Failed to open gate " + name);
            return false;
        }
    }
}
