/*
 *   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.command.SWCommand;
import de.steamwar.command.SWCommandUtils;
import de.steamwar.command.TypeMapper;
import de.steamwar.linkage.Linked;
import org.bukkit.Bukkit;
import org.bukkit.entity.Player;

import java.util.Arrays;
import java.util.List;

@Linked
public class TimeCommand extends SWCommand {

    private static final List<String> tabCompletions = Arrays.asList("0", "6000", "12000", "18000");

    public TimeCommand() {
        super("time");
        addDefaultHelpMessage("OTHER_TIME_HELP");
    }

    @Register
    public void genericCommand(@Validator Player p, @OptionalValue("") @StaticValue({"set", ""}) String set, Time time) {
        Bukkit.getWorlds().get(0).setTime(time.getValue());
        BauSystem.MESSAGE.send("OTHER_TIME_RESULT", p);
    }

    @Register
    public void genericCommand(@Validator Player p, @OptionalValue("") @StaticValue({"set", ""}) String set, int time) {
        if (time < 0 || time > 24000) {
            BauSystem.MESSAGE.send("OTHER_TIME_INVALID", p);
            return;
        }
        Bukkit.getWorlds().get(0).setTime(time);
        BauSystem.MESSAGE.send("OTHER_TIME_RESULT", p);
    }

    @ClassMapper(value = int.class, local = true)
    public TypeMapper<Integer> intTypeMapper() {
        return SWCommandUtils.createMapper(s -> {
            try {
                return Integer.parseInt(s);
            } catch (NumberFormatException e) {
                return null;
            }
        }, s -> tabCompletions);
    }

    public enum Time {
        NIGHT(18000),
        DAY(6000),
        DAWN(0),
        SUNSET(12000),
        NACHT(18000),
        TAG(6000),
        MORGEN(0),
        ABEND(12000);

        private final int value;

        Time(int value) {
            this.value = value;
        }

        public int getValue() {
            return value;
        }
    }
}