package pl.grzegorz2047.survivalcg.managers;

import com.zaxxer.hikari.HikariDataSource;
import org.bukkit.Bukkit;
import pl.grzegorz2047.survivalcg.SCG;
import pl.grzegorz2047.survivalcg.mysql.*;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;

/**
 * Created by Grzegorz2047. 06.12.2015.
 */
public class MysqlManager {
    private final SCG plugin;
    private String host, user, password, db, usertable;
    private String guildstable, bantable;
    private Integer port;
    private HikariDataSource hikari;
    //Uzywaj hikari
    DeathQuery deathQuery;
    GuildQuery guildQuery;
    RankingQuery rankingQuery;
    UserQuery userQuery;
    private RelationQuery relationQuery;
    private String relationTable;

    public MysqlManager(String host, int port, String user, String password, String db, String usertable, String guildTable, String bantable, String relationTable, SCG plugin) {
        SettingsManager settings = plugin.getManager().getSettingsManager();
        String prefix = settings.getSqlprefix();
        this.host = host;
        this.port = port;
        this.user = user;
        this.password = password;
        this.db = db;
        this.usertable = prefix + usertable;
        this.guildstable = prefix + guildTable;
        this.bantable = prefix + bantable;
        this.relationTable = prefix + relationTable;
        this.plugin = plugin;
        connectToDB();
        initiateQueries();
    }


    private void initiateQueries() {
        this.deathQuery = new DeathQuery(this);
        this.deathQuery.checkIfTableExists();
        this.guildQuery = new GuildQuery(this, plugin);
        this.guildQuery.checkIfTableExists();
        this.rankingQuery = new RankingQuery(this);
        this.userQuery = new UserQuery(this, plugin);
        this.userQuery.checkIfTableExists();
        this.relationQuery = new RelationQuery(this, plugin);
        this.relationQuery.checkIfTableExists();
    }

    private void connectToDB() {
        hikari = new HikariDataSource();
        hikari.setMaximumPoolSize(3);
        hikari.setDataSourceClassName("com.mysql.jdbc.jdbc2.optional.MysqlDataSource");
        hikari.addDataSourceProperty("serverName", host);
        hikari.addDataSourceProperty("port", port);
        hikari.addDataSourceProperty("databaseName", db);
        hikari.addDataSourceProperty("user", user);
        hikari.addDataSourceProperty("password", password);
        hikari.addDataSourceProperty("cachePrepStmts", true);
        hikari.addDataSourceProperty("prepStmtCacheSize", 250);
        hikari.addDataSourceProperty("prepStmtCacheSqlLimit", 2048);
    }

    public String getHost() {
        return host;
    }

    public String getUser() {
        return user;
    }

    public String getPassword() {
        return password;
    }

    public String getDb() {
        return db;
    }

    public String getUsertable() {
        return usertable;
    }

    public String getGuildTable() {
        return guildstable;
    }

    public Integer getPort() {
        return port;
    }

    public HikariDataSource getHikari() {
        return hikari;
    }

    public DeathQuery getDeathQuery() {
        return deathQuery;
    }

    public GuildQuery getGuildQuery() {
        return guildQuery;
    }

    public RankingQuery getRankingQuery() {
        return rankingQuery;
    }

    public UserQuery getUserQuery() {
        return userQuery;
    }

    public void dispose() {
        this.getHikari().close();
    }

    public String getBantable() {
        return bantable;
    }

    public void setBantable(String bantable) {
        this.bantable = bantable;
    }

    public RelationQuery getRelationQuery() {
        return relationQuery;

    }

    public String getRelationTable() {
        return relationTable;
    }

    public void setRelationTable(String relationTable) {
        this.relationTable = relationTable;
    }
}

@@ -2004,24 +2004,43 @@
 ectToDB() %7B%0A
+        //if mysql%0A
         hika
@@ -2669,16 +2669,38 @@
  2048);%0A
+        //else sqlite%0A
     %7D%0A%0A 

Create methods for creating tables.