/*
 *   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.features.attributescopy;

import de.steamwar.bausystem.BauSystem;
import de.steamwar.bausystem.Permission;
import de.steamwar.linkage.Linked;
import org.bukkit.Bukkit;
import org.bukkit.Material;
import org.bukkit.OfflinePlayer;
import org.bukkit.block.Block;
import org.bukkit.block.Skull;
import org.bukkit.block.data.BlockData;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.block.BlockPlaceEvent;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.ItemMeta;

import java.util.List;

@Linked
public class AttributesPlaceListener implements Listener {

    @EventHandler
    public void onBlockPlace(BlockPlaceEvent event) {
        if (!Permission.BUILD.hasPermission(event.getPlayer())) return;
        ItemStack itemStack = event.getItemInHand();
        ItemMeta itemMeta = itemStack.getItemMeta();
        if (itemMeta == null) return;
        List<String> strings = itemMeta.getLore();
        if (strings == null) return;
        if (strings.isEmpty()) return;
        if (!strings.get(0).equals("§eAttributes§8:")) return;
        Material type = event.getBlock().getType();
        OfflinePlayer offlinePlayer = null;
        if (event.getBlock().getState() instanceof Skull) {
            Skull skull = (Skull) event.getBlock().getState();
            offlinePlayer = skull.getOwningPlayer();
        }
        OfflinePlayer finalPlayerProfile = offlinePlayer;
        event.setCancelled(true);
        Bukkit.getScheduler().runTaskLater(BauSystem.getInstance(), () -> {
            Material material = strings.stream()
                    .filter(s -> s.startsWith("§8-§7 Material§8:§7 "))
                    .map(s -> s.replace("§8-§7 Material§8:§7 ", ""))
                    .map(String::toUpperCase)
                    .map(s -> {
                        try {
                            return Material.valueOf(s);
                        } catch (Exception e) {
                            return null;
                        }
                    })
                    .findFirst()
                    .orElse(type);
            event.getBlock().setType(material, false);
            Block block = event.getBlock();
            BlockData blockData = block.getBlockData();
            Bukkit.getScheduler().runTaskLater(BauSystem.getInstance(), () -> {
                if (block.getState() instanceof Skull && finalPlayerProfile != null) {
                    Skull skull = (Skull) block.getState();
                    skull.setOwningPlayer(finalPlayerProfile);
                    skull.update(true, false);
                }
            }, 1);
            AttributeUtils.paste(blockData, strings);
            block.setBlockData(blockData, false);
        }, 1);
    }
}
