package com.sequenceiq.cloudbreak.shell.configuration;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.support.PropertySourcesPlaceholderConfigurer;
import org.springframework.scheduling.concurrent.ThreadPoolExecutorFactoryBean;
import org.springframework.shell.CommandLine;
import org.springframework.shell.SimpleShellCommandLineOptions;
import org.springframework.shell.commands.ExitCommands;
import org.springframework.shell.commands.HelpCommands;
import org.springframework.shell.commands.ScriptCommands;
import org.springframework.shell.commands.VersionCommands;
import org.springframework.shell.core.CommandMarker;
import org.springframework.shell.core.JLineShellComponent;
import org.springframework.shell.plugin.HistoryFileNameProvider;
import org.springframework.shell.plugin.support.DefaultHistoryFileNameProvider;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.sequenceiq.cloudbreak.api.exception.SSLConnectionException;
import com.sequenceiq.cloudbreak.client.CloudbreakClient;
import com.sequenceiq.cloudbreak.client.CloudbreakClient.CloudbreakClientBuilder;
import com.sequenceiq.cloudbreak.shell.transformer.ResponseTransformer;

/**
 * Spring bean definitions.
 */
@Configuration
public class ShellConfiguration {

    private static final String CLIENT_ID = "cloudbreak_shell";

    @Value("${cloudbreak.address:https://cloudbreak-api.sequenceiq.com}")
    private String cloudbreakAddress;

    @Value("${identity.address:https://identity.sequenceiq.com}")
    private String identityServerAddress;

    @Value("${sequenceiq.user:}")
    private String user;

    @Value("${sequenceiq.password:}")
    private String password;

    @Value("${shell.debug:false}")
    private boolean debug;

    @Value("${cert.validation:true}")
    private boolean certificateValidation;

    @Value("${cmdfile:}")
    private String cmdFile;

    @Bean
    static PropertySourcesPlaceholderConfigurer propertyPlaceholderConfigurer() {
        return new PropertySourcesPlaceholderConfigurer();
    }

    @Bean
    public CloudbreakClient cloudbreakClient() {
        try {
            return new CloudbreakClientBuilder(cloudbreakAddress, identityServerAddress, CLIENT_ID).withCredential(user, password).
                    withDebug(debug).withCertificateValidation(certificateValidation).build();
        } catch (SSLConnectionException e) {
            System.out.println(String.format("%s Try to start the shell with --shell.cert.validation=false parameter.", e.getMessage()));
            System.exit(1);
        }
        return null;
    }

    @Bean
    ResponseTransformer responseTransformer() {
        return new ResponseTransformer();
    }

    @Bean
    HistoryFileNameProvider defaultHistoryFileNameProvider() {
        return new DefaultHistoryFileNameProvider();
    }

    @Bean(name = "shell")
    JLineShellComponent shell() {
        return new JLineShellComponent();
    }

    @Bean
    CommandLine commandLine() throws Exception {
        String[] args = cmdFile.length() > 0 ? new String[]{"--cmdfile", cmdFile} : new String[0];
        return SimpleShellCommandLineOptions.parseCommandLine(args);
    }

    @Bean
    ThreadPoolExecutorFactoryBean getThreadPoolExecutorFactoryBean() {
        return new ThreadPoolExecutorFactoryBean();
    }

    @Bean
    ObjectMapper objectMapper() {
        return new ObjectMapper();
    }

    @Bean
    CommandMarker exitCommand() {
        return new ExitCommands();
    }

    @Bean
    CommandMarker versionCommands() {
        return new VersionCommands();
    }

    @Bean
    CommandMarker helpCommands() {
        return new HelpCommands();
    }

    @Bean
    CommandMarker scriptCommands() {
        return new ScriptCommands();
    }

}

@@ -2598,22 +2598,16 @@
  with --
-shell.
 cert.val

CLOUD-52131 remove 'shell.' prefix from the message