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

import de.steamwar.bausystem.BauSystem;
import de.steamwar.bausystem.configplayer.Config;
import de.steamwar.command.SWCommand;
import de.steamwar.linkage.Linked;
import net.md_5.bungee.api.ChatMessageType;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.player.PlayerJoinEvent;
import org.bukkit.potion.PotionEffect;
import org.bukkit.potion.PotionEffectType;
import yapion.hierarchy.types.YAPIONObject;

@Linked
public class NightVisionCommand extends SWCommand implements Listener {

    public NightVisionCommand() {
        super("nightvision", "nv", "light");
    }

    @Register(description = "NIGHT_VISION_HELP")
    public void genericCommand(Player p) {
        YAPIONObject yapionObject = Config.getInstance().get(p);
        boolean value = !yapionObject.getBooleanOrDefault("nightvision", false);
        yapionObject.put("nightvision", value);
        setNightVision(p, value);
    }

    @EventHandler
    public void onPlayerJoin(PlayerJoinEvent event) {
        setNightVision(event.getPlayer(), Config.getInstance().get(event.getPlayer()).getBooleanOrDefault("nightvision", false));
    }

    private void setNightVision(Player p, boolean value) {
        if (p.hasPotionEffect(PotionEffectType.NIGHT_VISION) && !value) {
            p.removePotionEffect(PotionEffectType.NIGHT_VISION);
            BauSystem.MESSAGE.sendPrefixless("NIGHT_VISION_OFF", p, ChatMessageType.ACTION_BAR);
        } else if (!p.hasPotionEffect(PotionEffectType.NIGHT_VISION) && value) {
            p.addPotionEffect(new PotionEffect(PotionEffectType.NIGHT_VISION, 1000000, 255, false, false));
            BauSystem.MESSAGE.sendPrefixless("NIGHT_VISION_ON", p, ChatMessageType.ACTION_BAR);
        }
    }
}