package com.jy.generator.builder;

import com.jy.generator.domain.GeneratorConfig;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
import org.springframework.boot.autoconfigure.jdbc.JdbcTemplateAutoConfiguration;
import org.springframework.boot.autoconfigure.web.servlet.DispatcherServletAutoConfiguration;
import org.springframework.boot.autoconfigure.web.servlet.ServletWebServerFactoryAutoConfiguration;
import org.springframework.boot.autoconfigure.web.servlet.WebMvcAutoConfiguration;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.web.server.WebServerFactoryCustomizer;
import org.springframework.boot.web.servlet.server.ConfigurableServletWebServerFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Import;

import java.util.HashMap;
import java.util.Map;

@Slf4j
@Import(value = {
        DispatcherServletAutoConfiguration.class,
        ServletWebServerFactoryAutoConfiguration.class,
        WebMvcAutoConfiguration.class,
        DataSourceAutoConfiguration.class,
        JdbcTemplateAutoConfiguration.class
})
@ComponentScan("com.jy.generator")
public class GeneratorBuilder {

    public static final GeneratorConfig config = new GeneratorConfig();

    public static GeneratorBuilder builder(String url, String username, String password) {
        config.setUrl(url);
        config.setUsername(username);
        config.setPassword(password);
        return new GeneratorBuilder();
    }

    @Bean
    private WebServerFactoryCustomizer<ConfigurableServletWebServerFactory> containerConfig() {
        return factory -> factory.setPort(config.getPort());
    }

    public GeneratorBuilder dir(String dir) {
        config.setDir(dir);
        return this;
    }

    public GeneratorBuilder port(int port) {
        config.setPort(port);
        return this;
    }

    public GeneratorBuilder module(String module) {
        config.setModule("\\" + module);
        return this;
    }

    public GeneratorBuilder parent(String parent) {
        config.setParent(parent);
        return this;
    }

    public GeneratorBuilder prefixSubcontracting(boolean prefixSubcontracting) {
        config.setPrefixSubcontracting(prefixSubcontracting);
        return this;
    }

    public void build() {
        new SpringApplicationBuilder().sources(GeneratorBuilder.class).properties(getProperties()).run();
        log.info("""
                
                ==================================================================
                >>>>> 代码生成器启动成功,访问地址: http://localhost:{}
                ==================================================================
                """, config.getPort());
    }

    private Map<String, Object> getProperties() {
        HashMap<String, Object> map = new HashMap<>();
        map.put("logging.level.org.apache", "ERROR");
        map.put("logging.level.org.springframework.web", "ERROR");
        map.put("logging.level.com.zaxxer.hikari", "ERROR");
        map.put("spring.output.ansi.enabled", "always");
        map.put("spring.datasource.driver-class-name", "com.mysql.cj.jdbc.Driver");
        map.put("spring.datasource.url", config.getUrl());
        map.put("spring.datasource.username", config.getUsername());
        map.put("spring.datasource.password", config.getPassword());
        map.put("server.port", config.getPort());
        return map;
    }

}