package nl.sbdeveloper.themeparkplus.sbutils;

import com.zaxxer.hikari.HikariConfig;
import com.zaxxer.hikari.HikariDataSource;
import nl.sbdeveloper.themeparkplus.ThemeParkPlus;
import org.bukkit.Bukkit;

import java.io.File;
import java.io.IOException;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.Properties;

public class SQLiteDB {
    private final String dbName;
    private HikariDataSource source;
    private Connection con;

    /**
     * Initialize a new connection
     *
     * @param dbName The database name
     */
    public SQLiteDB(String dbName) {
        this.dbName = dbName;

        File dbFile = new File(ThemeParkPlus.getInstance().getDataFolder(), dbName + ".db");

        if (!dbFile.exists()) {
            try {
                Bukkit.getLogger().info("[ThemeParkPlus] Generating the " + dbName + ".db!");
                if (!dbFile.createNewFile()) {
                    Bukkit.getLogger().severe("[ThemeParkPlus] Couldn't generate the " + dbName + ".db!");
                    return;
                }
            } catch (IOException e) {
                Bukkit.getLogger().severe("[ThemeParkPlus] Couldn't generate the " + dbName + ".db!");
                return;
            }
        }

        HikariConfig config = new HikariConfig();
        config.setPoolName("ThemeParkPlus");
        config.setUsername(null);
        config.setPassword(null);
        config.setDriverClassName("org.sqlite.JDBC");
        config.setConnectionTestQuery("SELECT 1");
        config.setMaximumPoolSize(1);

        Properties prop = new Properties();
        prop.setProperty("date_string_format", "yyyy-MM-dd HH:mm:ss");

        config.setJdbcUrl("jdbc:sqlite:" + dbFile.getAbsolutePath());
        config.setDataSourceProperties(prop);
        this.source = new HikariDataSource(config);

        try {
            this.con = this.source.getConnection();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    /**
     * Get the connection, to execute queries
     *
     * CREATE TABLE -> execute()
     * SELECT -> executeQuery()
     * UPDATE -> executeUpdate()
     *
     * @return Connection
     */
    public Connection getConnection() {
        return this.con;
    }

    /**
     * Close the connection
     */
    public void closeSource() {
        Bukkit.getLogger().info("[ThemeParkPlus] Closing the database connection for " + dbName + ".db!");
        try {
            this.con.close();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        this.source.close();
    }
}
