/*
 * This file is part of FlexibleLogin
 *
 * The MIT License (MIT)
 *
 * Copyright (c) 2015-2018 contributors
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in all
 * copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */
package com.github.games647.flexiblelogin.command;

import com.github.games647.flexiblelogin.AttemptManager;
import com.github.games647.flexiblelogin.FlexibleLogin;
import com.github.games647.flexiblelogin.ProtectionManager;
import com.github.games647.flexiblelogin.config.Settings;
import com.github.games647.flexiblelogin.tasks.LoginTask;
import com.google.inject.Inject;

import java.util.UUID;
import java.util.concurrent.TimeUnit;

import org.slf4j.Logger;
import org.spongepowered.api.Sponge;
import org.spongepowered.api.command.CommandException;
import org.spongepowered.api.command.CommandManager;
import org.spongepowered.api.command.CommandResult;
import org.spongepowered.api.command.CommandSource;
import org.spongepowered.api.command.args.CommandContext;
import org.spongepowered.api.command.spec.CommandSpec;
import org.spongepowered.api.entity.living.player.Player;
import org.spongepowered.api.scheduler.Task;

import static org.spongepowered.api.command.args.GenericArguments.onlyOne;
import static org.spongepowered.api.command.args.GenericArguments.string;
import static org.spongepowered.api.text.Text.of;

public class LoginCommand extends AbstractCommand {

    @Inject
    private CommandManager commandManager;

    @Inject
    private AttemptManager attemptManager;

    @Inject
    private ProtectionManager protectionManager;

    @Inject
    LoginCommand(FlexibleLogin plugin, Logger logger, Settings settings) {
        super(plugin, logger, settings);
    }

    @Override
    public CommandResult execute(CommandSource src, CommandContext args) throws CommandException {
        if (!(src instanceof Player)) {
            throw new CommandException(settings.getText().getPlayersOnly());
        }

        Player player = (Player) src;
        if (plugin.getDatabase().isLoggedIn(player)) {
            throw new CommandException(settings.getText().getAlreadyLoggedIn());
        }

        UUID uniqueId = player.getUniqueId();
        if (!attemptManager.isAllowed(uniqueId)) {
            String lockCommand = settings.getGeneral().getLockCommand();
            if (!lockCommand.isEmpty()) {
                commandManager.process(Sponge.getServer().getConsole(), lockCommand);
            }

            Task.builder()
                    .delay(settings.getGeneral().getWaitTime().getSeconds(), TimeUnit.SECONDS)
                    .execute(() -> attemptManager.clearAttempts(uniqueId))
                    .submit(plugin);

            throw new CommandException(settings.getText().getMaxAttempts());
        }

        attemptManager.increaseAttempt(uniqueId);

        //the arg isn't optional. We can be sure there is value
        String password = args.<String>getOne("password").get();

        Task.builder()
                //we are executing a SQL Query which is blocking
                .async()
                .execute(new LoginTask(plugin, attemptManager, protectionManager, (Player) src, password))
                .name("Login Query")
                .submit(plugin);

        return CommandResult.success();
    }

    @Override
    public CommandSpec buildSpec(Settings settings) {
        return buildPlayerCommand(settings, "login")
                .executor(this)
                .arguments(onlyOne(string(of("password"))))
                .build();
    }
}