package autovermietung.helper;

import autovermietung.models.Kunde;
import autovermietung.models.Modell;
import autovermietung.models.Pkw;

import java.io.*;
import java.sql.*;
import java.util.ArrayList;
import java.util.Properties;

public class Database {
    private static String strUsername, strPassword, ipStr, dbStr;

    public static ArrayList<Kunde> liesKunden() {
        ArrayList<Kunde> Kunden = new ArrayList<>();
        try {
            Connection conn = getConnection();

            PreparedStatement kunden = conn.prepareStatement("SELECT * FROM kunde");
            ResultSet result = kunden.executeQuery();

            while(result.next()) {
                Kunden.add(new Kunde(result.getInt(1), result.getString(2), result.getString(3), result.getString(4), result.getString(5), result.getInt(6)));
            }

            conn.close();

        } catch(Exception e) {
            System.out.println(e);
        }
        return Kunden;
    }

    public static ArrayList<Modell> liesModell() {
        ArrayList<Modell> Modelle = new ArrayList<>();
        try {
            Connection conn = getConnection();

            PreparedStatement modelle = conn.prepareStatement("SELECT * FROM modell");
            ResultSet result = modelle.executeQuery();

            while(result.next()) {
                Modelle.add(new Modell(result.getInt(1), result.getString(2), result.getInt(3), result.getInt(4), result.getFloat(5), result.getFloat(6)));
            }

            conn.close();

        } catch(Exception e) {
            System.out.println(e);
        }
        return Modelle;
    }

    public static ArrayList<Pkw> liesPkw() {
        ArrayList<Pkw> Pkws = new ArrayList<>();
        try {
            Connection conn = getConnection();

            PreparedStatement pkws = conn.prepareStatement("SELECT * FROM pkw");
            ResultSet result = pkws.executeQuery();

            while(result.next()) {
                Pkws.add(new Pkw(result.getString(1), result.getDate(2), result.getInt(3), result.getString(4), result.getDate(5), result.getInt(6), result.getInt(7)));
            }

            conn.close();

        } catch(Exception e) {
            System.out.println(e);
        }
        return Pkws;
    }

    public static void updatePkw(Pkw auto) {
        try {
            Connection conn = getConnection();

            String kennzeichen = auto.getKennzeichen();
            Date erstzulassung = auto.getErstzulassung();
            int tachostand = auto.getTachostand();
            String farbe = auto.getFarbe();
            Date ausleihdatum = auto.getAusleihdatum();
            int mnr = auto.getMnr();
            int knr = auto.getKnr();

            String updatePkwSQL = "REPLACE INTO pkw VALUES(?, ?, ?, ?, ?, ?, ?);";
            PreparedStatement updatePkw = conn.prepareStatement(updatePkwSQL);
            updatePkw.setString(1, kennzeichen);
            updatePkw.setDate(2, erstzulassung);
            updatePkw.setInt(3, tachostand);
            updatePkw.setString(4, farbe);
            updatePkw.setDate(5, ausleihdatum);
            updatePkw.setInt(6, mnr);
            updatePkw.setInt(7, knr);
            updatePkw.executeQuery();

            conn.close();
        } catch(Exception e) {
            System.out.println(e);
        }
    }

    public static void deletePkw(Pkw auto) {
        try {
           Connection conn = getConnection();

           String kennzeichen = auto.getKennzeichen();

           String deletePkwSQL = "DELETE FROM pkw WHERE kennzeichen = ?;";
           PreparedStatement deletePkw = conn.prepareStatement(deletePkwSQL);
           deletePkw.setString(1, kennzeichen);
           deletePkw.executeUpdate();

           conn.close();
        } catch(Exception e) {
            System.out.println(e);
        }

    }

    public static void updateKunde(Kunde kunde) {
        try {
            Connection conn = getConnection();

            int knr = kunde.getKnr();
            String vorname = kunde.getVorname();
            String nachname = kunde.getNachname();
            String anschrift = kunde.getAnschrift();
            String telefon = kunde.getTelefon();
            int personr = kunde.getPersonr();

            String updateKundeSQL = "REPLACE INTO kunde VALUES(?, ?, ?, ?, ?, ?);";
            PreparedStatement updateKunde = conn.prepareStatement(updateKundeSQL);
            updateKunde.setInt(1, knr);
            updateKunde.setString(2, vorname);
            updateKunde.setString(3, nachname);
            updateKunde.setString(4, anschrift);
            updateKunde.setString(5, telefon);
            updateKunde.setInt(6, personr);
            updateKunde.executeUpdate();

            conn.close();
        } catch(Exception e) {
            System.out.println(e);
        }
    }

    public static void deleteKunde(Kunde kunde) {
        try {
            Connection conn = getConnection();

            int knr = kunde.getKnr();

            String deleteKundeSQL = "DELETE FROM kunde WHERE knr = ?;";
            PreparedStatement deleteKunde = conn.prepareStatement(deleteKundeSQL);
            deleteKunde.setInt(1, knr);
            deleteKunde.executeUpdate();

            conn.close();
        } catch(Exception e) {
            System.out.println(e);
        }

    }

    public static void updateModell(Modell modell) {
        try {
           Connection conn = getConnection();

           int mnr = modell.getMnr();
           String modellname = modell.getModellname();
           int hubraum = modell.getHubraum();
           int leistung = modell.getLeistung();
           float kmpreis = modell.getKmpreis();
           float pauschale = modell.getPauschale();

           String replaceModellSQL = "REPLACE INTO modell VALUES(?, ?, ?, ?, ?, ?);";
           PreparedStatement updateModell = conn.prepareStatement(replaceModellSQL);
           updateModell.setInt(1, mnr);
           updateModell.setString(2, modellname);
           updateModell.setInt(3, hubraum);
           updateModell.setInt(4, leistung);
           updateModell.setFloat(5, kmpreis);
           updateModell.setFloat(6, pauschale);
           updateModell.executeUpdate();

           conn.close();
        } catch(Exception e) {
            if(e.equals("java.sql.SQLIntegrityConstraintViolationException")) {
                System.out.println("Diese Aktion kann nicht augeführt werden, weil diese Modell in Benutzung ist.");
            }
            System.out.println(e);
        }
    }

    public static void deleteModell(Modell modell) {
        try {
            Connection conn = getConnection();

            int mnr = modell.getMnr();

            String deleteModellSQL = "DELETE FROM modell WHERE mnr = ?;";
            PreparedStatement deleteModell = conn.prepareStatement(deleteModellSQL);
            deleteModell.setInt(1, mnr);
            deleteModell.executeUpdate();

            conn.close();
        } catch(Exception e) {
            System.out.println(e);
        }

    }

    private static Connection getConnection() throws Exception {
        Properties prop = new Properties();
        InputStream input = null;

        try {
            input = new FileInputStream("database.properties");

            // load input
            prop.load(input);

            // get all values
            ipStr = prop.getProperty("ip");
            dbStr = prop.getProperty("db");
            strUsername = prop.getProperty("username");
            strPassword = prop.getProperty("password");

        } catch(IOException ex) {
            ex.printStackTrace();
        } finally {
            if(input != null) {
                try {
                    input.close();
                } catch(IOException e) {
                    e.printStackTrace();
                }
            }
        }

        try {
            String driver = "com.mysql.cj.jdbc.Driver";
            String url = "jdbc:mysql://" + ipStr + "/" + dbStr + "?useUnicode=true&useJDBCCompliantTimezoneShift=true&useLegacyDatetimeCode=false&serverTimezone=UTC";
            String username = strUsername;
            String password = strPassword;
            Class.forName(driver);

            Connection conn = DriverManager.getConnection(url, username, password);
            return conn;
        } catch(Exception e) {
            System.out.println(e);
        }
        return null;
    }
}
