package net.sevecek.dailyplanet;

import java.sql.Driver;
import java.sql.*;
import javax.annotation.*;
import javax.sql.*;
import org.mariadb.jdbc.*;
import org.springframework.beans.factory.annotation.*;
import org.springframework.context.annotation.*;
import org.springframework.jdbc.core.*;
import org.springframework.web.servlet.config.annotation.*;
import net.sevecek.boot.war.*;

@Configuration
@ComponentScan
@EnableWebMvc
public class ApplicationConfig extends DefaultWebMvcConfigurer {

    @Value("jdbc:mysql://localhost/DailyPlanet")
    private String databaseUrl;

    @Value("student")
    private String databaseUsername;

    @Value("password")
    private String databasePassword;

    @Bean
    public DataSource dataSource() throws SQLException {
        MariaDbDataSource dataSource;
        dataSource = new MariaDbDataSource();
        dataSource.setUrl(databaseUrl);
        dataSource.setUser(databaseUsername);
        dataSource.setPassword(databasePassword);
        return dataSource;
    }

    @Bean
    public JdbcTemplate pokladacDotazu(DataSource dataSource) {
        JdbcTemplate pokladacDotazu;
        pokladacDotazu = new JdbcTemplate(dataSource);
        return pokladacDotazu;
    }

    @Bean
    public RowMapper<Autor> prevodnikTridyAutor() {
        RowMapper<Autor> prevodnikTridyAutor;
        prevodnikTridyAutor = BeanPropertyRowMapper.newInstance(Autor.class);
        return prevodnikTridyAutor;
    }

/*
    @PreDestroy
    public void close() throws SQLException {
        Driver mariaDbDriver = DriverManager.getDriver(databaseUrl);
        DriverManager.deregisterDriver(mariaDbDriver);
    }
*/

}
