/*
 *   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.teamserver.listener;

import de.steamwar.core.Core;
import de.steamwar.core.TrickyTrialsWrapper;
import de.steamwar.linkage.Linked;
import de.steamwar.teamserver.Builder;
import net.md_5.bungee.api.ChatMessageType;
import net.md_5.bungee.api.chat.BaseComponent;
import net.md_5.bungee.api.chat.TextComponent;
import org.bukkit.Bukkit;
import org.bukkit.Material;
import org.bukkit.block.Block;
import org.bukkit.block.data.type.Switch;
import org.bukkit.entity.EntityType;
import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority;
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;
import org.bukkit.event.player.PlayerInteractEvent;

@Linked
public class FreezeListener implements Listener {

    public static boolean freeze = true;

    public FreezeListener() {
        Bukkit.getScheduler().runTaskTimer(Builder.getInstance(), () -> {
            if (!freeze) return;
            Bukkit.getOnlinePlayers().forEach(player -> {
                Builder.MESSAGE.sendPrefixless("REGION_FREEZE_ENABLED", player, ChatMessageType.ACTION_BAR);
            });
        }, 1, 1);
    }

    @EventHandler
    public void onEntitySpawn(EntitySpawnEvent e) {
        if (!freeze) return;
        e.setCancelled(true);
        if (e.getEntityType() == TrickyTrialsWrapper.impl.getTntEntityType()) {
            Bukkit.getScheduler().runTaskLater(Builder.getInstance(), () -> {
                e.getLocation().getBlock().setType(Material.TNT, false);
            }, 1L);
        }
    }

    @EventHandler
    public void onBlockCanBuild(BlockCanBuildEvent e) {
        if (!e.isBuildable()) return;
        if (freeze) return;
        if (e.getMaterial() == Material.TNT) {
            e.setBuildable(false);
            e.getBlock().setType(Material.TNT, false);
        }
    }

    @EventHandler
    public void onEntityChangeBlock(EntityChangeBlockEvent e) {
        if (freeze) {
            e.setCancelled(true);
        }
    }

    @EventHandler
    public void onPhysicsEvent(BlockPhysicsEvent e) {
        if (freeze) {
            e.setCancelled(true);
        }
    }

    @EventHandler
    public void onPistonExtend(BlockPistonExtendEvent e) {
        if (freeze) {
            e.setCancelled(true);
        }
    }

    @EventHandler
    public void onPistonRetract(BlockPistonRetractEvent e) {
        if (freeze) {
            e.setCancelled(true);
        }
    }

    @EventHandler
    public void onBlockGrow(BlockGrowEvent e) {
        if (freeze) {
            e.setCancelled(true);
        }
    }

    @EventHandler
    public void onRedstoneEvent(BlockRedstoneEvent e) {
        if (freeze) {
            e.setNewCurrent(e.getOldCurrent());
        }
    }

    @EventHandler
    public void onBlockDispense(BlockDispenseEvent e) {
        if (freeze) {
            e.setCancelled(true);
        }
    }

    @EventHandler
    public void onInventoryMoveEvent(InventoryMoveItemEvent e) {
        if (e.getDestination().getLocation() != null && freeze) {
            e.setCancelled(true);
        } else if (e.getSource().getLocation() != null && freeze) {
            e.setCancelled(true);
        }
    }

    @EventHandler(priority = EventPriority.MONITOR)
    public void onBlockBreak(BlockBreakEvent e) {
        if (Core.getVersion() < 19) return;
        if (e.getPlayer().getInventory().getItemInMainHand().getType() == Material.DEBUG_STICK) return;
        if (freeze) {
            if (e.isCancelled()) return;
            e.setCancelled(true);
            e.getBlock().setType(Material.AIR, false);
        }
    }

    /*
    @EventHandler
    public void onBlockPlace(BlockPlaceEvent e) {
        if (Core.getVersion() < 19) return;
        if (!e.getItemInHand().getType().isBlock()) return;
        if (freeze) {
            e.setCancelled(true);
            Bukkit.getScheduler().runTaskLater(BauSystem.getInstance(), () -> {
                e.getBlock().setType(e.getItemInHand().getType(), false);
            }, 1L);
        }
    }
     */

    @EventHandler
    public void onFluidLevelChange(FluidLevelChangeEvent e) {
        if (freeze) {
            e.setCancelled(true);
        }
    }

    @EventHandler
    public void onBlockSpread(BlockSpreadEvent e) {
        if (freeze) {
            e.setCancelled(true);
        }
    }

    @EventHandler
    public void onBlockFromTo(BlockFromToEvent e) {
        if (freeze) {
            e.setCancelled(true);
        }
    }

    @EventHandler
    public void onSpongeAbsorb(SpongeAbsorbEvent e) {
        if (freeze) {
            e.setCancelled(true);
        }
    }

    @EventHandler
    public void onBlockForm(BlockFormEvent e) {
        if (freeze) {
            e.setCancelled(true);
        }
    }

    @EventHandler
    public void onPlayerInteract(PlayerInteractEvent e) {
        if (e.getAction() != Action.RIGHT_CLICK_BLOCK) return;
        if (freeze) {
            Block block = e.getClickedBlock();
            if (block.getType() == Material.LEVER) {
                Switch data = ((Switch) block.getBlockData());
                data.setPowered(!data.isPowered());
                block.setBlockData(data, false);
            }
        }
    }

    @EventHandler
    public void onBlockFade(BlockFadeEvent event) {
        if (freeze) {
            event.setCancelled(true);
        }
    }
}
