package net.cobem.grizzly.storage;

import net.cobem.grizzly.Grizzly;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;

public class DatabaseResult {
    private ResultSet Result;
    private Connection Connection;

    private boolean ErrorOccured;
    public boolean Opened;
    public boolean RanQuery;

    private String Query;

    public DatabaseResult(String SQL, Connection Connection, ResultSet Result) {
        this.Query = SQL;
        this.Result = Result;
        this.Connection = Connection;
        this.Opened = true;
    }

    public boolean errorCheck() {
        if (ErrorOccured) {
            Grizzly.write("Error trying to run damage query: " + Query);
        }

        return ErrorOccured;
    }

    public boolean close() {
        try {
            //Grizzly.WriteOut("Database Result with Query: " + this.Result.getStatement().toString() + " has been cleared");

            this.Result.getStatement().close();
            this.Connection.close();
        } catch (SQLException e) {
            //
        }

        return true;
    }

    public String getString() {
        if (errorCheck()) {
            return null;
        }

        try {
            this.Result.first();
            this.RanQuery = true;
            return this.Result.getString(1);
        } catch (SQLException ex) {
            Grizzly.write(ex.getMessage());
            this.ErrorOccured = true;

            Grizzly.write("Error trying to run damage query: " + Query);

            return null;
        }
    }

    public Integer getInt() {
        if (errorCheck()) {
            return 0;
        }

        try {
            this.Result.first();
            this.RanQuery = true;
            return this.Result.getInt(1);
        } catch (SQLException ex) {
            Grizzly.write(ex.getMessage());
            this.ErrorOccured = true;

            Grizzly.write("Error trying to run damage query: " + Query);

            return 0;
        }
    }

    public ResultSet getRow() {
        if (errorCheck()) {
            return null;
        }

        try {
            this.RanQuery = true;
            while (this.Result.next()) {
                return this.Result;
            }
        } catch (SQLException ex) {
            Grizzly.write(ex.getMessage());
            this.ErrorOccured = true;

            Grizzly.write("Error trying to run damage query: " + Query);

            return null;
        }

        return null;
    }

    public ResultSet getTable() {
        if (errorCheck()) {
            return null;
        }

        try {
            this.RanQuery = true;
            return this.Result;
        } catch (Exception ex) {
            Grizzly.write(ex.getMessage());
            this.ErrorOccured = true;

            Grizzly.write("Error trying to run damage query: " + Query);

            return null;
        }
    }

    public int rowCount() {
        if (errorCheck()) {
            return 0;
        }

        try {
            this.RanQuery = true;

            int RowCount = 0;

            while (Result.next()) {
                RowCount++;
            }

            return RowCount;
        } catch (Exception ex) {
            Grizzly.write(ex.getMessage());
            this.ErrorOccured = true;

            Grizzly.write("Error trying to run damage query: " + Query);

            return 0;
        }
    }
}
