/*
 * Copyright (C) 2016-2019 Dennis Neufeld
 *
 * 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 <http://www.gnu.org/licenses/>.
 */

package space.npstr.wolfia.domain.oauth2;

import space.npstr.wolfia.commands.BaseCommand;
import space.npstr.wolfia.commands.CommandContext;
import space.npstr.wolfia.commands.PublicCommand;
import space.npstr.wolfia.domain.Command;

import javax.annotation.Nonnull;

@Command
public class AuthCommand implements BaseCommand, PublicCommand {

    public static final String TRIGGER = "auth";


    private final AuthStateCache stateCache;
    private final OAuth2Requester requester;

    public AuthCommand(AuthStateCache stateCache, OAuth2Requester requester) {
        this.stateCache = stateCache;
        this.requester = requester;
    }

    @Override
    public String getTrigger() {
        return TRIGGER;
    }

    @Override
    public boolean execute(@Nonnull CommandContext context) {

        String jumpUrl = context.getMessage().getJumpUrl();

        AuthState authState = ImmutableAuthState.builder()
                .userId(context.getInvoker().getIdLong())
                .redirectUrl(jumpUrl)
                .build();

        String state = this.stateCache.generateStateParam(authState);
        String response = "Click here to auth with Wolfia: " + this.requester.getAuthorizationUrl(state);

        context.replyPrivate(response, __ -> context.replyWithMention("check your direct messages!"),
                t -> context.replyWithMention("I cannot send you a private message, please unblock me and/or adjust your discord privacy settings."));
        return true;
    }

    @Nonnull
    @Override
    public String help() {
        return "Start the authorization flow with Wolfia. Once you have successfully authorized with me, "
                + "I will be able to automatically add you to wolf chat or global games.";
    }
}

@@ -756,16 +756,49 @@
 auth2;%0A%0A
+import javax.annotation.Nonnull;%0A
 import s
@@ -985,42 +985,8 @@
 d;%0A%0A
-import javax.annotation.Nonnull;%0A%0A
 @Com
@@ -1841,23 +1841,29 @@
  String 
-respons
+privateMessag
 e = %22Cli
@@ -1869,34 +1869,147 @@
 ick 
+t
 he
-re to auth with Wolfia: 
+ following link to connect with Wolfia. This will allow me to automatically %22%0A                + %22add you to wolf chat or global games.%5Cn
 %22 + 
@@ -2085,15 +2085,21 @@
 ate(
-respons
+privateMessag
 e, _

Clarify in DM what the auth command does