package electrosphere.game.server.db;

import electrosphere.logger.LoggerInterface;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Properties;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author amaterasu
 */
public class DatabaseController {
    
    Connection conn;
    
    public DatabaseController(){
        
    }
    
    public void connect(String path){
        String dbms = "sqlite";
        Properties connectionProps = new Properties();
        String fullAddress = "jdbc:" + dbms + ":" + path;
        try {
            conn = DriverManager.getConnection(fullAddress, connectionProps);
        } catch (SQLException ex) {
            LoggerInterface.loggerFileIO.ERROR("Failure to connect to db", ex);
        }
    }
    
//    public boolean executeStatement(String statementRaw){
//        boolean rVal = false;
//        try {
//            PreparedStatement statement = conn.prepareStatement(statementRaw);
//            rVal = statement.execute();
//        } catch (SQLException ex) {
//            LoggerInterface.loggerFileIO.ERROR("SQL statement execution error", ex);
//        }
//        return rVal;
//    }
    
    public DatabaseResult executeStatement(String statementRaw){
        DatabaseResult rVal = DatabaseResult.createStatement(statementRaw);
        try {
            PreparedStatement statement = conn.prepareStatement(statementRaw);
            statement.execute();
            ResultSet results = statement.getResultSet();
            if(results != null){
                rVal.addResultSet(results);
            }
        } catch (SQLException ex) {
            rVal.succeeded = false;
            rVal.hasResultSet = false;
            LoggerInterface.loggerFileIO.ERROR("SQL statement execution error", ex);
        }
        return rVal;
    }
    
    public DatabaseResult executeQuery(String statementRaw){
        DatabaseResult rVal = DatabaseResult.createStatement(statementRaw);
        try {
            PreparedStatement statement = conn.prepareStatement(statementRaw);
            ResultSet results = statement.executeQuery();
            if(results != null){
                rVal.addResultSet(results);
            }
        } catch (SQLException ex) {
            rVal.succeeded = false;
            rVal.hasResultSet = false;
            LoggerInterface.loggerFileIO.ERROR("SQL query execution error", ex);
        }
        return rVal;
    }
    
    public boolean isConnected(){
        boolean rVal = false;
        try {
            rVal = conn.isValid(100);
        } catch (SQLException ex) {
            LoggerInterface.loggerFileIO.ERROR("SQL error validating status of db", ex);
        }
        return rVal;
    }
    
    public void disconnect(){
        try {
            conn.close();
        } catch (SQLException ex) {
            LoggerInterface.loggerEngine.ERROR("Error disconnecting from DB", ex);
        }
    }
    
}
