package com.loganmagnan.eventcore.commands;

import com.loganmagnan.eventcore.EventCore;
import com.loganmagnan.eventcore.utils.ColorUtils;
import com.loganmagnan.eventcore.utils.Constants;
import com.loganmagnan.eventcore.utils.CustomLocation;
import com.loganmagnan.eventcore.utils.command.BaseCommand;
import com.loganmagnan.eventcore.utils.command.Command;
import com.loganmagnan.eventcore.utils.command.CommandArguments;
import org.bukkit.configuration.file.FileConfiguration;
import org.bukkit.entity.Player;

public class SetSpawnCommand extends BaseCommand {

    private EventCore main = EventCore.getInstance();

    @Command(name = "setspawn", permission = "eventcore.command.setspawn")
    @Override
    public void executeAs(CommandArguments command) {
        Player player = command.getPlayer();

        String[] args = command.getArgs();

        if (args.length == 0) {
            this.main.getSpawnManager().setSpawnLocation(CustomLocation.fromBukkitLocation(player.getLocation()));

            this.saveLocation(player, "SPAWN.LOCATION");

            player.sendMessage(ColorUtils.getMessageType(Constants.COMMAND_MESSAGES.get("set-spawn.spawn-set").get(0)));
        }
    }

    public void saveLocation(Player player, String location) {
        FileConfiguration fileConfiguration = this.main.getMainConfig().getConfig();
        fileConfiguration.set(location, CustomLocation.locationToString(CustomLocation.fromBukkitLocation(player.getLocation())));

        this.main.getMainConfig().save();
    }
}