package sample;

import java.sql.*;
import java.util.ArrayList;
import java.util.Iterator;

/**
 * Created by sebastian on 08.05.17.
 */
public class DataHandler {
    private static final String CLASS_NAME = DataHandler.class.getName();

    private final String host;
    private final int port;
    private final String user;
    private final String pwd;
    private String database;
    private final Connection connect;

    public DataHandler(String host, int port, String user, String pwd, String database) throws SQLException {
        this.host = host;
        this.port = port;
        this.user = user;
        this.pwd = pwd;
        this.database = database;

        // This will load the MySQL driver, each DB has its own driver
        try {
            Class.forName("com.mysql.jdbc.Driver");
        } catch (ClassNotFoundException e) {
            Log.severe(CLASS_NAME, "MySQL driver not found!");
            e.printStackTrace();
        }
        // Setup the connection with the DB
        connect = DriverManager
                .getConnection("jdbc:mysql://"+host+":"+port+"/"+database+"?"
                        + "user="+user+"&password="+pwd+"");

    }

    public ArrayList<ChoiceValue> getPresentations() throws SQLException {
        ArrayList<ChoiceValue> choices = new ArrayList<>();

        Statement stmt = connect.createStatement();
        ResultSet rs = stmt.executeQuery("SELECT ID, Saalname, Beginn, Filmname FROM v_Vorstellung");

        while (rs.next()) {
            choices.add(new ChoiceValue(rs.getInt("ID"), rs.getString("Saalname")+" - "+rs.getString("Beginn")+" - "+rs.getString("Filmname")));
        }
        return choices;
    }


    public HallDimens getHallDimentions(int presID) throws SQLException {
        PreparedStatement stmt = connect.prepareStatement(
                "SELECT MIN(Sitzplatz.Reihe) `MinReihe`, MIN(Sitzplatz.Platz) `MinPlatz`, " +
                        "MAX(Sitzplatz.Reihe) `MaxReihe`, MAX(Sitzplatz.Platz) `MaxPlatz` " +
                        "FROM Saal " +
                        "JOIN Sitzplatz ON Saal.ID = Sitzplatz.fk_Saal_ID " +
                        "JOIN Vorstellung ON Saal.ID = Vorstellung.fk_Saal_ID " +
                        "WHERE Vorstellung.ID=? " +
                        "GROUP BY Saal.ID"
        );
        stmt.setInt(1, presID);
        Log.finer(CLASS_NAME, stmt.toString());
        ResultSet rs = stmt.executeQuery();
        if (rs.next())
            return new HallDimens(
                    rs.getInt("MinPlatz"),
                    rs.getInt("MinReihe"),
                    rs.getInt("MaxPlatz"),
                    rs.getInt("MaxReihe")
            );
        else
            return null;
    }


    public ChairIterator getChairIterator(int presID) throws SQLException {
        PreparedStatement stmt = connect.prepareStatement(
                "SELECT *, COUNT(*)=2 `isUsed` FROM ((" +
                        "SELECT Sitzplatz.Reihe `Row`, Sitzplatz.Platz `Pos`, Sitzplatz.Preis `Price` " +
                        "FROM Vorstellung " +
                        "JOIN Saal ON Vorstellung.fk_Saal_ID = Saal.ID " +
                        "JOIN Sitzplatz ON Saal.ID = Sitzplatz.fk_Saal_ID " +
                        "WHERE Vorstellung.ID=?" +
                ") UNION ALL (" +
                        "SELECT Sitzplatz.Reihe `Row`, Sitzplatz.Platz `Pos`, Sitzplatz.Preis `Price` " +
                        "FROM Vorstellung " +
                        "JOIN Reservierung ON Vorstellung.ID = Reservierung.fk_Vorstellung_ID " +
                        "JOIN nm_Reservierung_Sitzplatz ON Reservierung.ID = nm_Reservierung_Sitzplatz.fk_Reservierung_ID " +
                        "JOIN Sitzplatz ON nm_Reservierung_Sitzplatz.fk_Sitzplatz_ID = Sitzplatz.ID " +
                        "WHERE Vorstellung.ID=?" +
                ")) t GROUP BY `Row`, `Pos`, `Price`"
        );
        stmt.setInt(1, presID);
        stmt.setInt(2, presID);
        ResultSet rs = stmt.executeQuery();
        return new ChairIterator(rs);
    }

}
