/*
 *   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.linkage.Linked;
import de.steamwar.lobby.LobbySystem;
import de.steamwar.sql.NodeMember;
import de.steamwar.sql.SteamwarUser;
import org.bukkit.block.Block;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.player.PlayerInteractEvent;
import org.bukkit.event.player.PlayerJoinEvent;

import java.time.LocalDate;

@Linked
public class PresentClickListener implements Listener {

    @EventHandler
    public void onPlayerJoin(PlayerJoinEvent event) {
        LocalDate localDate = LocalDate.now();
        int day = localDate.getDayOfMonth();

        if (!AdventsCalendar.active()) {
            return;
        }
        AdventsCalendar.getPresentList().forEach(present -> {
            if (present.getDay() != day) return;
            if (NodeMember.getNodeMember(present.getSchematicId(), SteamwarUser.get(event.getPlayer().getUniqueId()).getId()) != null) {
                return;
            }
            LobbySystem.getMessage().send("ADVENT_CALENDAR_MESSAGE", event.getPlayer());
        });
    }

    @EventHandler
    public void onPlayerInteract(PlayerInteractEvent event) {
        Block block = event.getClickedBlock();
        if (block == null) return;

        LocalDate localDate = LocalDate.now();
        int day = localDate.getDayOfMonth();

        if (!AdventsCalendar.active()) {
            return;
        }

        AdventsCalendar.getPresentList().forEach(present -> {
            present.click(event.getPlayer(), SteamwarUser.get(event.getPlayer().getUniqueId()), day, block.getLocation());
        });
    }

    // @EventHandler
    public void onPresentSetup(PlayerInteractEvent event) {
        Block block = event.getClickedBlock();
        if (block == null) return;
        AdventsCalendar.getPresentList().forEach(present -> {
            present.edit(event.getPlayer(), block.getLocation());
        });
    }
}
