package com.altamiracorp.lumify.tools;

import com.altamiracorp.lumify.core.cmdline.CommandLineBase;
import com.altamiracorp.lumify.core.model.user.UserRepository;
import com.altamiracorp.lumify.core.model.user.UserRow;
import com.altamiracorp.lumify.core.user.SystemUser;
import com.google.inject.Inject;
import org.apache.commons.cli.CommandLine;
import org.apache.commons.cli.OptionBuilder;
import org.apache.commons.cli.Options;

public class UserAdd extends CommandLineBase {
    private UserRepository userRepository;
    private String username;
    private String password;

    public static void main(String[] args) throws Exception {
        int res = new UserAdd().run(args);
        if (res != 0) {
            System.exit(res);
        }
    }

    @Override
    protected Options getOptions() {
        Options options = super.getOptions();

        options.addOption(
                OptionBuilder
                        .withLongOpt("username")
                        .withDescription("The username")
                        .hasArg(true)
                        .withArgName("username")
                        .create("u")
        );

        options.addOption(
                OptionBuilder
                        .withLongOpt("password")
                        .withDescription("The password")
                        .hasArg(true)
                        .withArgName("password")
                        .create("p")
        );

        return options;
    }

    @Override
    protected void processOptions(CommandLine cmd) throws Exception {
        super.processOptions(cmd);
        this.username = cmd.getOptionValue("username");
        this.password = cmd.getOptionValue("password");
    }

    @Override
    protected int run(CommandLine cmd) throws Exception {
        if (this.username == null || this.username.length() == 0) {
            System.err.println("Invalid username");
            return 1;
        }
        if (this.password == null || this.password.length() == 0) {
            System.err.println("Invalid password");
            return 2;
        }

        System.out.println("Adding user: " + this.username);

        UserRow user = this.userRepository.findByUserName(this.username, new SystemUser());
        if (user != null) {
            System.err.println("username already exists");
            return 3;
        }

        user = this.userRepository.addUser(this.username, this.password, new SystemUser());
        System.out.println("User added: " + user.getRowKey());

        return 0;
    }

    @Inject
    public void setUserRepository(UserRepository userRepository) {
        this.userRepository = userRepository;
    }
}

@@ -1440,32 +1440,260 @@
 p%22)%0A        );%0A%0A
+        options.addOption(%0A                OptionBuilder%0A                        .withLongOpt(%22reset%22)%0A                        .withDescription(%22If the password should be reset%22)%0A                        .create(%22r%22)%0A        );%0A%0A
         return o
@@ -2468,32 +2468,455 @@
  SystemUser());%0A
+%0A        if (cmd.hasOption(%22reset%22)) %7B%0A            if (user == null) %7B%0A                System.err.println(%22password reset requested but user not found%22);%0A                return 4;%0A            %7D%0A            user.setPassword(this.password);%0A            this.userRepository.save(user, SystemUser.getSystemUserContext());%0A            System.out.println(%22User password reset: %22 + user.getRowKey());%0A        %7D%0A        else %7B%0A    
         if (user
@@ -2931,32 +2931,36 @@
 ) %7B%0A            
+   
+ 
 System.err.print
@@ -2998,24 +2998,28 @@
             
+    
 return 3;%0A  
@@ -3016,35 +3016,42 @@
 turn 3;%0A        
-%7D%0A%0A
+    %7D%0A    
         user = t
@@ -3126,32 +3126,36 @@
 ser());%0A        
+    
 System.out.print
@@ -3188,24 +3188,34 @@
 etRowKey());
+%0A        %7D
 %0A%0A        re

allow resetting passwords