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

import de.steamwar.bausystem.BauSystem;
import de.steamwar.bausystem.Permission;
import de.steamwar.bausystem.configplayer.Config;
import de.steamwar.bausystem.region.Region;
import lombok.experimental.UtilityClass;
import org.bukkit.entity.Player;

import java.util.HashMap;
import java.util.Map;

@UtilityClass
public class BlockCounter {

    public final Map<Region, BlockCount> blockCountMap = new HashMap<>();

    public boolean isActive(final Player p) {
        if (!Config.getInstance().get(p).containsKey("blockCounter")) {
            return false;
        }
        return Config.getInstance().get(p).getPlainValue("blockCounter");
    }

    public void enable(final Player p) {
        Config.getInstance().get(p).put("blockCounter", true);
    }

    public void disable(final Player p) {
        Config.getInstance().get(p).put("blockCounter", false);
    }

    public String getMessage(Player player, int count, int tntCount, long tick, long lastTick) {
        if (!Permission.BUILD.hasPermission(player)) return null;

        double countPerTNT = (double) count / tntCount;
        double countPerTick = (double) count / Math.max((lastTick - tick), 1);
        if (isActive(player)) {
            if (countPerTick < 100) {
                return BauSystem.MESSAGE.parse("BLOCK_COUNTER_MESSAGE_SECOND", player, count, tntCount, (int) (countPerTNT * 10) / 10.0, (int) (countPerTick * 100) / 100.0 * 20);
            } else {
                return BauSystem.MESSAGE.parse("BLOCK_COUNTER_MESSAGE", player, count, tntCount, (int) (countPerTNT * 10) / 10.0, (int) (countPerTick * 100) / 100.0);
            }
        }
        return null;
    }
}
