package org.crunchytorch.coddy.user.runner;

import org.crunchytorch.coddy.application.utils.AppUtils;
import org.crunchytorch.coddy.user.data.in.UpdateUser;
import org.crunchytorch.coddy.user.data.security.Permission;
import org.crunchytorch.coddy.user.service.UserService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;

@Component
public class AdminCreationOnStartup implements ApplicationRunner {

    private static final Logger LOGGER = LoggerFactory.getLogger(AdminCreationOnStartup.class);

    @Autowired
    UserService service;

    @Value("${" + AppUtils.CONF_ADMIN_LOGIN + ":admin}")
    private String adminUsername;

    @Value("${" + AppUtils.CONF_ADMIN_PASSWORD + ":admin}")
    private char[] adminPassword;

    @Value("${" + AppUtils.CONF_ADMIN_EMAIL + ":admin.coddy@crunchy-torch.org}")
    private String adminEmail;

    @Value("${" + AppUtils.CONF_BOT_LOGIN + ":coddyBot}")
    private String botUsername;

    @Value("${" + AppUtils.CONF_BOT_PASSWORD + ":coddyBot}")
    private char[] botPassword;

    @Override
    public void run(ApplicationArguments args) throws Exception {
        List<String> permissions = new ArrayList<String>() {{
            add(Permission.PERSO_ACCOUNT);
            add(Permission.PERSO_SNIPPET);
            add(Permission.ADMIN);
        }};
        LOGGER.info("create admin user if exists");
        if (!service.isExist(adminUsername)) {
            service.create(new UpdateUser(adminUsername, adminPassword, adminEmail), permissions);
        }
        LOGGER.info("create bot user is exists");
        if (!service.isExist(botUsername)) {
            service.create(new UpdateUser(botUsername, botPassword), permissions);
        }
    }
}

@@ -38,16 +38,103 @@
 unner;%0A%0A
+import org.apache.commons.lang.ArrayUtils;%0Aimport org.apache.commons.lang.StringUtils;%0A
 import o
@@ -1002,29 +1002,24 @@
 N_LOGIN + %22:
-admin
 %7D%22)%0A    priv
@@ -1096,21 +1096,16 @@
 ORD + %22:
-admin
 %7D%22)%0A    
@@ -1187,37 +1187,8 @@
 + %22:
-admin.coddy@crunchy-torch.org
 %7D%22)%0A
@@ -1261,32 +1261,24 @@
 T_LOGIN + %22:
-coddyBot
 %7D%22)%0A    priv
@@ -1355,16 +1355,8 @@
 + %22:
-coddyBot
 %7D%22)%0A
@@ -1480,181 +1480,234 @@
     
-List%3CString%3E permissions = new ArrayList%3CString%3E() %7B%7B
+this.createAdmin();%0A        this.createBot();
 %0A    
+%7D%0A%0A
     
-    add(Permission.PERSO_ACCOUNT);%0A            add(Permission.PERSO_SNIPPET);%0A            add(Permission.ADMIN)
+private void createAdmin() %7B%0A        if (StringUtils.isEmpty(adminUsername) && ArrayUtils.isEmpty(adminPassword) && StringUtils.isEmpty(adminEmail)) %7B%0A            return
 ;%0A  
@@ -1717,10 +1717,9 @@
    %7D
-%7D;
+%0A
 %0A   
@@ -1899,28 +1899,342 @@
 Email), 
-permissions)
+new ArrayList%3CString%3E() %7B%7B%0A                add(Permission.PERSO_ACCOUNT);%0A                add(Permission.PERSO_SNIPPET);%0A                add(Permission.ADMIN);%0A            %7D%7D);%0A        %7D%0A    %7D%0A%0A    private void createBot() %7B%0A        if (StringUtils.isEmpty(botUsername) && ArrayUtils.isEmpty(botPassword)) %7B%0A            return
 ;%0A      
@@ -2233,24 +2233,25 @@
 ;%0A        %7D%0A
+%0A
         LOGG
@@ -2406,19 +2406,182 @@
 d), 
-permissions
+new ArrayList%3CString%3E() %7B%7B%0A                add(Permission.PERSO_ACCOUNT);%0A                add(Permission.PERSO_SNIPPET);%0A                add(Permission.ADMIN);%0A            %7D%7D
 );%0A 

make admin creation optional