/*
 *   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 de.steamwar.core.Core;
import org.bukkit.Bukkit;
import org.bukkit.Material;
import org.bukkit.entity.EntityType;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.block.*;
import org.bukkit.event.entity.EntityChangeBlockEvent;
import org.bukkit.event.entity.EntitySpawnEvent;
import org.bukkit.event.inventory.InventoryMoveItemEvent;

public class CommandFreeze extends SWCommand implements Listener {

    public CommandFreeze() {
        super("freeze", "stoplag");
        Bukkit.getPluginManager().registerEvents(this, BauSystem.getPlugin());
    }

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

    @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 diese Welt nicht einfrieren";
    }

    private String getEnableMessage(){
        return "§cRegion eingefroren";
    }

    private String getDisableMessage(){
        return "§aRegion aufgetaut";
    }

    private boolean toggle(Region region) {
        region.setFreeze(!region.isFreeze());
        return region.isFreeze();
    }

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

    @EventHandler
    public void onEntitySpawn(EntitySpawnEvent e) {
        if (!Region.getRegion(e.getLocation()).isFreeze()) return;
        e.setCancelled(true);
        if (e.getEntityType() == EntityType.PRIMED_TNT) {
            Bukkit.getScheduler().runTaskLater(BauSystem.getPlugin(), () -> {
                e.getLocation().getBlock().setType(Material.TNT, false);
            }, 1L);
        }
    }

    @EventHandler
    public void onBlockCanBuild(BlockCanBuildEvent e) {
        if (Core.getVersion() == 12) return;
        if (!e.isBuildable()) return;
        if (!Region.getRegion(e.getBlock().getLocation()).isFreeze()) return;
        if (e.getMaterial() == Material.TNT) {
            e.setBuildable(false);
            e.getBlock().setType(Material.TNT, false);
        }
    }

    @EventHandler
    public void onEntityChangeBlock(EntityChangeBlockEvent e) {
        if (Region.getRegion(e.getBlock().getLocation()).isFreeze()) e.setCancelled(true);
    }

    @EventHandler
    public void onPhysicsEvent(BlockPhysicsEvent e){
        if (Region.getRegion(e.getBlock().getLocation()).isFreeze()) e.setCancelled(true);
    }

    @EventHandler
    public void onPistonExtend(BlockPistonExtendEvent e){
        if (Region.getRegion(e.getBlock().getLocation()).isFreeze()) e.setCancelled(true);
    }

    @EventHandler
    public void onPistonRetract(BlockPistonRetractEvent e){
        if (Region.getRegion(e.getBlock().getLocation()).isFreeze()) e.setCancelled(true);
    }

    @EventHandler
    public void onBlockGrow(BlockGrowEvent e){
        if (Region.getRegion(e.getBlock().getLocation()).isFreeze()) e.setCancelled(true);
    }

    @EventHandler
    public void onRedstoneEvent(BlockRedstoneEvent e) {
        if (Region.getRegion(e.getBlock().getLocation()).isFreeze()) e.setNewCurrent(e.getOldCurrent());
    }

    @EventHandler
    public void onBlockDispense(BlockDispenseEvent e) {
        if (Region.getRegion(e.getBlock().getLocation()).isFreeze()) e.setCancelled(true);
    }

    @EventHandler
    public void onInventoryMoveEvent(InventoryMoveItemEvent e){
        if (Region.getRegion(e.getDestination().getLocation()).isFreeze()) e.setCancelled(true);
    }
}
