/*
 *   This file is a part of the SteamWar software.
 *
 *   Copyright (C) 2025 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.regions.Region;
import de.steamwar.bausystem.world.Welt;
import de.steamwar.command.SWCommand;
import org.bukkit.Bukkit;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.block.BlockBurnEvent;
import org.bukkit.event.block.BlockSpreadEvent;

public class CommandFire extends SWCommand implements Listener {

    public CommandFire() {
        super("fire");
        Bukkit.getPluginManager().registerEvents(this, BauSystem.getPlugin());
    }

    @Register(help = true)
    public void genericHelp(Player p, String... args) {
        p.sendMessage("§8/§efire §8- §7Toggle Feuerschaden");
    }

    @Register
    public void toggleCommand(Player p) {
        if (!permissionCheck(p)) return;
        Region region = Region.getRegion(p.getLocation());
        if (toggle(region)) {
            RegionUtils.actionBar(region, getEnableMessage());
        } else {
            RegionUtils.actionBar(region, getDisableMessage());
        }
    }

    private String getNoPermMessage() {
        return "§cDu darfst hier nicht Feuerschaden (de-)aktivieren";
    }

    private String getEnableMessage() {
        return "§cRegions Feuerschaden deaktiviert";
    }

    private String getDisableMessage() {
        return "§aRegions Feuerschaden aktiviert";
    }

    private boolean toggle(Region region) {
        region.setFire(!region.isFire());
        return region.isFire();
    }

    private boolean permissionCheck(Player player) {
        if (Welt.noPermission(player, Permission.WORLD)) {
            player.sendMessage(BauSystem.PREFIX + getNoPermMessage());
            return false;
        }
        return true;
    }

    @EventHandler
    public void onFireDamage(BlockBurnEvent e) {
        if (Region.getRegion(e.getBlock().getLocation()).isFire()) e.setCancelled(true);
    }

    @EventHandler
    public void onFireSpread(BlockSpreadEvent e) {
        if (Region.getRegion(e.getBlock().getLocation()).isFire()) e.setCancelled(true);
    }

}
