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

import de.steamwar.bausystem.BauSystem;
import de.steamwar.bausystem.config.BauServer;
import de.steamwar.bausystem.region.Region;
import de.steamwar.bausystem.region.RegionUtils;
import de.steamwar.bausystem.region.flags.ColorMode;
import de.steamwar.bausystem.region.flags.Flag;
import de.steamwar.bausystem.utils.PasteBuilder;
import de.steamwar.command.SWCommand;
import de.steamwar.command.TypeValidator;
import de.steamwar.linkage.Linked;
import de.steamwar.linkage.LinkedInstance;
import org.bukkit.entity.Player;

@Linked
public class ColorCommand extends SWCommand {

    @LinkedInstance
    public BauServer bauServer;

    public ColorCommand() {
        super("color");
    }

    @Register(description = "REGION_COLOR_HELP_COLOR")
    public void genericColor(@Validator Player p, ColorMode color) {
        genericColorSet(p, color, ColorizationType.LOCAL);
    }

    @Register(description = "REGION_COLOR_HELP_COLOR_TYPE")
    public void genericColorSet(@Validator Player p, ColorMode color, ColorizationType colorizationType) {
        if (colorizationType == ColorizationType.GLOBAL) {
            Region.getRegions().forEach(region -> {
                region.getRegionData().set(Flag.COLOR, color);
            });
            BauSystem.MESSAGE.send("REGION_COLOR_GLOBAL", p, BauSystem.MESSAGE.parse(color.getChatValue(), p));
            return;
        }
        Region region = Region.getRegion(p.getLocation());
        if (!region.getRegionData().has(Flag.COLOR).isWritable()) {
            BauSystem.MESSAGE.send("REGION_COLOR_NO_REGION", p);
            return;
        }
        region.getRegionData().set(Flag.COLOR, color);
        try {
            PasteBuilder pasteBuilder = new PasteBuilder(new PasteBuilder.FileProvider(region.getArea().getResetFile()))
                    .ignoreAir(true)
                    .onlyColors(true)
                    .color(color);
            region.getArea().reset(pasteBuilder, false);
            RegionUtils.message(region, "REGION_REGION_COLORED");
            RegionUtils.message(region, "REGION_REGION_COLORED_FAILED");
        } catch (SecurityException e) {
            BauSystem.MESSAGE.send("REGION_REGION_FAILED_COLORED", p);
        }
    }

    @Register
    public void genericColorSet(@Validator Player p, ColorizationType colorizationType, ColorMode color) {
        genericColorSet(p, color, colorizationType);
    }

    @ClassValidator(value = Player.class, local = true)
    public TypeValidator<Player> validator() {
        return (commandSender, player, messageSender) -> {
            return !messageSender.send(!bauServer.getOwner().equals(player.getUniqueId()), "NO_PERMISSION");
        };
    }

    public enum ColorizationType {
        LOCAL,
        GLOBAL
    }
}
