package de.viper.survivalplus.commands;

import de.viper.survivalplus.fun.FunChallenge;
import de.viper.survivalplus.fun.FunChallengeManager;
import de.viper.survivalplus.SurvivalPlus;
import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;

public class StartFunChallengeCommand implements CommandExecutor {

    private final SurvivalPlus plugin;
    private final FunChallengeManager challengeManager;

    public StartFunChallengeCommand(SurvivalPlus plugin, FunChallengeManager challengeManager) {
        this.plugin = plugin;
        this.challengeManager = challengeManager;
    }

    @Override
    public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
        if (!(sender instanceof Player)) {
            sender.sendMessage(plugin.getLangConfig().getString("challenge.only_players"));
            return true;
        }
        if (args.length < 1) {
            sender.sendMessage(plugin.getLangConfig().getString("challenge.usage"));
            return true;
        }
        String challengeName = args[0];
        for (FunChallenge fc : challengeManager.getChallenges()) {
            if (fc.getName().equalsIgnoreCase(challengeName)) {
                fc.setActive(true);
                String msg = plugin.getLangConfig()
                    .getString("challenge.start_success")
                    .replace("%challenge%", fc.getDescription());
                sender.sendMessage(msg);
                // Hier ggf. Timer und Tracking starten!
                plugin.saveConfig();
                return true;
            }
        }
        String failMsg = plugin.getLangConfig()
            .getString("challenge.start_fail")
            .replace("%challenge%", challengeName);
        sender.sendMessage(failMsg);
        return true;
    }
}
