/*
 *   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.lobby.special.advent;

import de.steamwar.lobby.LobbySystem;
import de.steamwar.sql.NodeMember;
import de.steamwar.sql.SchematicNode;
import de.steamwar.sql.SteamwarUser;
import lombok.Data;
import lombok.Getter;
import org.bukkit.*;
import org.bukkit.block.data.BlockData;
import org.bukkit.configuration.ConfigurationSection;
import org.bukkit.entity.Player;

import java.util.*;

public class Present {

    private static final World WORLD = Bukkit.getWorlds().get(0);

    @Data
    private static class Point {
        private final int x;
        private final int y;
        private final int z;

        public static Point toPoint(Location location) {
            return new Point(location.getBlockX(), location.getBlockY(), location.getBlockZ());
        }
    }

    private final List<Map<String, Integer>> blockList;
    private final Set<Point> locations = new HashSet<>();

    @Getter
    private final int day;

    @Getter
    private final int schematicId;

    private Set<Player> players = new HashSet<>();

    public Present(int day, ConfigurationSection configurationSection) {
        this.day = day;
        if (configurationSection.contains("schematicId")) {
            schematicId = configurationSection.getInt("schematicId");
        } else {
            configurationSection.set("schematicId", -1);
            schematicId = -1;
        }
        List<?> blockList = configurationSection.getList("blocks");
        if (blockList == null) {
            blockList = new ArrayList<>();
            configurationSection.set("blocks", blockList);
        }
        this.blockList = (List<Map<String, Integer>>) blockList;
        blockList.forEach(object -> {
            if (!(object instanceof Map)) return;
            Map<String, ?> map = (Map<String, ?>) object;
            locations.add(new Point((int) map.get("x"), (int) map.get("y"), (int) map.get("z")));
        });
        // generate();
    }

    public void click(Player player, SteamwarUser user, int day, Location location) {
        if (day != this.day) return;
        if (!locations.contains(Point.toPoint(location))) return;
        if (NodeMember.getNodeMember(schematicId, user.getId()) != null) return;
        NodeMember.createNodeMember(schematicId, user.getId());
        LobbySystem.getMessage().send("ADVENT_CALENDAR_OPEN", player, SchematicNode.getSchematicNode(schematicId).getName());
        player.playSound(location, Sound.ENTITY_EXPERIENCE_ORB_PICKUP, 1, 1);
    }

    public void removePlayer(Player player) {
        players.remove(player);
        locations.forEach(p -> {
            Location loc = new Location(WORLD, p.x, p.y, p.z);
            BlockData blockData = loc.getBlock().getBlockData();
            player.sendBlockChange(loc, blockData);
        });
    }

    public void addPlayer(Player player) {
        players.add(player);
        locations.forEach(p -> {
            Location loc = new Location(WORLD, p.x, p.y, p.z);
            player.sendBlockChange(loc, BEDROCK);
        });
    }

    public void edit(Player player, Location location) {
        if (!players.contains(player)) return;
        Point point = Point.toPoint(location);
        if (locations.contains(point)) {
            locations.remove(point);
        } else {
            locations.add(point);
        }
        generate();
        Bukkit.getScheduler().runTaskLater(LobbySystem.getInstance(), () -> {
            BlockData blockData = location.getBlock().getBlockData();
            players.forEach(pl -> {
                if (pl == player) return;
                if (locations.contains(point)) {
                    pl.sendBlockChange(location, blockData);
                } else {
                    pl.sendBlockChange(location, BEDROCK);
                }
            });
            player.sendBlockChange(location, blockData);
            locations.forEach(p -> {
                Location loc = new Location(WORLD, p.x, p.y, p.z);
                player.sendBlockChange(loc, BEDROCK);
            });
        }, 4);
    }

    private static final BlockData BEDROCK = Material.BEDROCK.createBlockData();

    private void generate() {
        blockList.clear();
        locations.forEach(point -> {
            Map<String, Integer> pos = new HashMap<>();
            pos.put("x", point.x);
            pos.put("y", point.y);
            pos.put("z", point.z);
            blockList.add(pos);
        });
        AdventsCalendar.save();
    }
}
