/* 
    This file is a part of the SteamWar software.
    
    Copyright (C) 2020  SteamWar.de-Serverteam

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU Affero General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
    GNU Affero General Public License for more details.

    You should have received a copy of the GNU Affero General Public License
    along with this program.  If not, see <https://www.gnu.org/licenses/>.
*/

package de.steamwar.bausystem.commands;

import de.steamwar.bausystem.BauSystem;
import de.steamwar.bausystem.Permission;
import de.steamwar.bausystem.world.Welt;
import de.steamwar.bausystem.world.regions.Region;
import de.steamwar.command.SWCommand;
import de.steamwar.sql.SchematicNode;
import de.steamwar.sql.SteamwarUser;
import org.bukkit.Bukkit;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.entity.EntityExplodeEvent;

import java.io.IOException;
import java.util.logging.Level;

public class CommandProtect extends SWCommand implements Listener {

    public CommandProtect() {
        super("protect");
        if (Region.buildAreaEnabled()) {
            Bukkit.getPluginManager().registerEvents(this, BauSystem.getPlugin());
        }
    }

    @Register(help = true)
    public void genericHelp(Player p, String... args) {
        p.sendMessage("§8/§eprotect §8- §7Schütze die Region");
        p.sendMessage("§8/§eprotect §8[§7Schematic§8] §8- §7Schütze die Region mit einer Schematic");
    }

    @Register
    public void genericProtectCommand(Player p) {
        if (!permissionCheck(p)) return;
        Region region = regionCheck(p);
        if (region == null) return;
        if (Region.buildAreaEnabled()) {
            region.setProtect(!region.isProtect());
            if (region.isProtect()) {
                RegionUtils.actionBar(region, "§aBoden geschützt");
            } else {
                RegionUtils.actionBar(region, "§cBoden Schutz aufgehoben");
            }
            return;
        }
        try {
            region.protect(null);
            p.sendMessage(BauSystem.PREFIX + "§7Boden geschützt");
        } catch (IOException e) {
            p.sendMessage(BauSystem.PREFIX + "§cFehler beim Schützen der Region");
            Bukkit.getLogger().log(Level.WARNING, "Failed protect", e);
        }
    }

    @Register
    public void schematicProtectCommand(Player p, String s) {
        if (!permissionCheck(p)) return;
        if (Region.buildAreaEnabled()) {
            genericHelp(p);
            return;
        }
        Region region = regionCheck(p);
        if (region == null) return;
        SteamwarUser owner = SteamwarUser.get(p.getUniqueId());
        SchematicNode schem = SchematicNode.getNodeFromPath(owner, s);
        if (schem == null) {
            p.sendMessage(BauSystem.PREFIX + "§cSchematic nicht gefunden");
            return;
        }
        try {
            region.protect(schem);
            p.sendMessage(BauSystem.PREFIX + "§7Boden geschützt");
        } catch (IOException e) {
            p.sendMessage(BauSystem.PREFIX + "§cFehler beim Schützen der Region");
            Bukkit.getLogger().log(Level.WARNING, "Failed protect", e);
        }
    }

    private boolean permissionCheck(Player player) {
        if (Welt.noPermission(player, Permission.WORLDEDIT)) {
            player.sendMessage(BauSystem.PREFIX + "§cDu darfst hier nicht den Boden schützen");
            return false;
        }
        return true;
    }

    private Region regionCheck(Player player) {
        Region region = Region.getRegion(player.getLocation());
        if (!region.hasProtection()) {
            player.sendMessage(BauSystem.PREFIX + "§cDu befindest dich derzeit in keiner (M)WG-Region");
            return null;
        }
        return region;
    }

    @EventHandler
    public void onExplode(EntityExplodeEvent event) {
        Region region = Region.getRegion(event.getLocation());
        if (!region.isProtect() || !region.hasProtection()) {
            return;
        }
        event.blockList().removeIf(block -> {
            return block.getY() < region.getProtectYLevel();
        });
    }
}
