package database

import org.mariadb.jdbc.Connection
import org.tinylog.Logger
import java.sql.DriverManager
import java.util.function.Consumer

/**
 * A class to manage a connection to a MariaDB database.
 *
 * @property address The address of the MariaDB server.
 * @property port The port number of the MariaDB server.
 * @property dbName The name of the database to connect to.
 * @property username The username for the database connection.
 * @property password The password for the database connection.
 */
@Suppress("unused")
class MariaDB (
    address : String = "localhost",
    port : Int = 3306,
    dbName : String = "aas",
    username : String = "admin",
    password : String = "admin"
) {
    private var connection : Connection? = null
    var connected : Boolean = false
    var SoundbankList : ArrayList<Soundbank> = ArrayList()
    var MessagebankList : ArrayList<Messagebank> = ArrayList()

    companion object {
        fun ValidDate(date: String): Boolean {
            // Check if the date is in the format DD/MM/YYYY
            val regex = Regex("""^\d{2}/\d{2}/\d{4}$""")
            return regex.matches(date)
        }
        fun ValidTime(time: String): Boolean {
            // Check if the time is in the format HH:MM:SS
            val regex = Regex("""^\d{2}:\d{2}:\d{2}$""")
            return regex.matches(time)
        }
    }

    init {
        try {
           connection = DriverManager.getConnection("jdbc:mariadb://$address:$port/$dbName", username, password) as Connection
            Logger.info("Connected to MariaDB" as Any)
            connected = true

            val loadthread = Thread {
                // Load soundbank and messagebank lists
                Reload_Messagebank()
                Logger.info { "Messagebank loaded"  }
                Reload_Soundbank()
                Logger.info { "Soundbank loaded"  }

            }
            loadthread.name = "LoadMariaDBThread"
            loadthread.isDaemon = true
            loadthread.start()

            loadthread.join()
            Logger.info { "Loading MariaDB completed"  }
            Logger.info { "Soundbank count: ${SoundbankList.size}"  }
            Logger.info { "Messagebank count: ${MessagebankList.size}"  }


        } catch (e : Exception) {
            Logger.error("Failed to connect to MariaDB: ${e.message}" as Any)
        }
    }

    /**
     * Closes the MariaDB connection.
     */
    fun close() {
        try {
            connection?.close()

            Logger.info("Connection to MariaDB closed" as Any)
        } catch (e : Exception) {
            Logger.error("Error closing MariaDB connection: ${e.message}" as Any)
        }
        connected = false
    }

    /**
     * Get All Log from database
     * @param consumer A Consumer that will receive the list of logs
     */
    fun GetLog(consumer : Consumer<ArrayList<Log>>)  {
        val logList = ArrayList<Log>()
        try {
            val statement = connection?.createStatement()
            val resultSet = statement?.executeQuery("SELECT * FROM log")
            while (resultSet?.next() == true) {
                val log = Log(
                    resultSet.getLong("index").toULong(),
                    resultSet.getString("datenya"),
                    resultSet.getString("timenya"),
                    resultSet.getString("machine"),
                    resultSet.getString("description")
                )
                logList.add(log)
            }
        } catch (e : Exception) {
            Logger.error("Error fetching logs: ${e.message}" as Any)
        }
        consumer.accept(logList)
    }

    /**
     * Get Log from database by date
     * @param date The date to filter logs by (format: DD/MM/YYYY)
     * @param consumer A Consumer that will receive the list of logs for the specified date
     */
    fun GetLog(date : String, consumer: Consumer<ArrayList<Log>>) {
        val logList = ArrayList<Log>()
        if (ValidDate(date)){
            try {
                val statement = connection?.prepareStatement("SELECT * FROM log WHERE datenya = ?")
                statement?.setString(1, date)
                val resultSet = statement?.executeQuery()
                while (resultSet?.next() == true) {
                    val log = Log(
                        resultSet.getLong("index").toULong(),
                        resultSet.getString("datenya"),
                        resultSet.getString("timenya"),
                        resultSet.getString("machine"),
                        resultSet.getString("description")
                    )
                    logList.add(log)
                }
            } catch (e : Exception) {
                Logger.error("Error fetching logs for date $date: ${e.message}" as Any)
            }
        }

        consumer.accept(logList)
    }

    fun GetLog(date : String, filter : String, consumer: Consumer<ArrayList<Log>>) {
        val logList = ArrayList<Log>()
        if (ValidDate(date)){
            try {
                val statement = connection?.prepareStatement("SELECT * FROM log WHERE datenya = ? AND description LIKE ?")
                statement?.setString(1, date)
                statement?.setString(2, "%$filter%")
                val resultSet = statement?.executeQuery()
                while (resultSet?.next() == true) {
                    val log = Log(
                        resultSet.getLong("index").toULong(),
                        resultSet.getString("datenya"),
                        resultSet.getString("timenya"),
                        resultSet.getString("machine"),
                        resultSet.getString("description")
                    )
                    logList.add(log)
                }
            } catch (e : Exception) {
                Logger.error("Error fetching logs for date $date with filter $filter: ${e.message}" as Any)
            }
        }

        consumer.accept(logList)
    }

    /**
     * Reloads the soundbank list from the database.
     */
    private fun Reload_Soundbank() {

        SoundbankList.clear()
        try {
            val statement = connection?.createStatement()
            val resultSet = statement?.executeQuery("SELECT * FROM soundbank")
            while (resultSet?.next() == true) {
                val soundbank = Soundbank(
                    resultSet.getLong("index").toUInt(),
                    resultSet.getString("Description"),
                    resultSet.getString("TAG"),
                    resultSet.getString("Category"),
                    resultSet.getString("Language"),
                    resultSet.getString("VoiceType"),
                    resultSet.getString("Path")
                )
                SoundbankList.add(soundbank)
            }
        } catch (e : Exception) {
            Logger.error("Error fetching soundbanks: ${e.message}" as Any)
        }
    }

    /**
     * Reloads the messagebank list from the database.
     */
    private fun Reload_Messagebank() {
        MessagebankList.clear()
        try {
            val statement = connection?.createStatement()
            val resultSet = statement?.executeQuery("SELECT * FROM messagebank")
            while (resultSet?.next() == true) {
                val messagebank = Messagebank(
                    resultSet.getLong("index").toUInt(),
                    resultSet.getString("Description"),
                    resultSet.getString("Language"),
                    resultSet.getInt("ANN_ID").toUInt(),
                    resultSet.getString("Voice_Type"),
                    resultSet.getString("Message_Detail"),
                    resultSet.getString("Message_TAGS")
                )

                MessagebankList.add(messagebank)
            }
        } catch (e : Exception) {
            Logger.error("Error fetching messagebanks: ${e.message}" as Any)
        }
    }

}