
package com.libreecm.db;


import java.util.ArrayList;

import javax.sql.DataSource;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.context.embedded.EmbeddedServletContainerCustomizer;
import org.springframework.boot.context.web.SpringBootServletInitializer;
import org.springframework.boot.orm.jpa.EntityScan;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.jdbc.datasource.DriverManagerDataSource;

import com.libreecm.commonAPI.auxiliars.exceptions.ConnectionDBNotPossibleException;
import com.libreecm.commonAPI.configuration.ComponentConfig;
import com.libreecm.db.jpa.users.LibreECMUser;
import com.libreecm.db.jpa.users.Role;
import com.libreecm.db.jpa.users.repositories.UserRepository;

@SpringBootApplication
@EntityScan(basePackages = "com.libreecm.db.jpa")
@ComponentScan("com.libreecm.db.controllers")
public class Application extends SpringBootServletInitializer  { 

	private static final Logger log = LoggerFactory.getLogger(Application.class);

	@Override
    protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
  			return application.sources(Application.class); 
    }
	
    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }
    
    @Bean
      public EmbeddedServletContainerCustomizer containerCustomizer() {
     
    	return (container -> {
          container.setPort(ComponentConfig.getConfig().getLibreECMDDBBLocalPort());
      });
    }

    /**
     * Reads data from a file XML read by class ComponentConfig
     */
    @Bean
    public DataSource dataSource(){
    	DriverManagerDataSource dataSource = new DriverManagerDataSource();
    	//Gets data from a file XML readed by class ComponentConfig
		try{ 
			dataSource.setDriverClassName(ComponentConfig.getConfig().getDriverClassName());
		    dataSource.setUrl(ComponentConfig.getConfig().getDbDBUrl());
		    dataSource.setUsername(ComponentConfig.getConfig().getDbUsername());
		    dataSource.setPassword(ComponentConfig.getConfig().getDbPassword());	
		} catch (Exception e){
			log.error("Could not access data base. Check your configuration");
			log.warn("Configuration: Driver: "+ComponentConfig.getConfig().getDriverClassName()+" . Url: "+ComponentConfig.getConfig().getDbDBUrl());
			throw new ConnectionDBNotPossibleException();
		}
		return dataSource;
    }
    @Bean
	public CommandLineRunner demo1(final UserRepository userRep) {
    	return (args) -> {
    		log.info("Default users adding data");
    	    //LibreECMUser user=(new LibreECMUser("system", Role.SYSTEM,true,true,true,true, new ArrayList<>()));
    		LibreECMUser user=(new LibreECMUser("system", Role.SYSTEM,true,true,true,true));
    		user.setPassword("system");
    		userRep.saveAndFlush(user);
    		//user=(new LibreECMUser("admin", Role.ADMIN,true,true,true,true, new ArrayList<>()));
    		user=(new LibreECMUser("admin", Role.ADMIN,true,true,true,true));
    		user.setPassword("admin");
    		userRep.saveAndFlush(user);
    		//user=(new LibreECMUser("default", Role.USER,true,true,true,true, new ArrayList<>()));
    		user=(new LibreECMUser("default", Role.USER,true,true,true,true));
    		user.setPassword("default");
    		userRep.saveAndFlush(user);
    	 };
	}

}
