import ActiveMQ.ActiveMQClient
import Other.Config
import Web.WebUI
import database.MySQLAdapter
import org.tinylog.Logger
import org.tinylog.provider.ProviderRegistry
import java.util.function.Consumer

lateinit var config : Config
var aas1 : MySQLAdapter? = null
var aas2 : MySQLAdapter? = null
var aas3 : MySQLAdapter? = null
fun main() {
    config = Config()
    config.Load()
    val webUI = WebUI()
    webUI.Start()
    val activeclient = ActiveMQClient()
    activeclient.Start()

    aas1 = MySQLAdapter(1)
    aas1!!.VerifyConnection { success ->
        if (success) {
            Logger.info {"Database 1 connection verified."}
            aas1!!.CreateFlightFilterTable { success ->
                if (success) {
                    Logger.info {"Flight Filter Table ensured in Database 1."}
                } else {
                    Logger.error {"Failed to ensure Flight Filter Table in Database 1."}
                }
            }
        } else {
            Logger.error {"Failed to verify Database 1 connection."  }
        }
    }

    aas2 = MySQLAdapter(2)
    aas2!!.VerifyConnection { success ->
        if (success) {
            Logger.info { "Database 2 connection verified." }
            aas2!!.CreateFlightFilterTable { success ->
                if (success) {
                    Logger.info { "Flight Filter Table ensured in Database 2." }
                } else {
                    Logger.error { "Failed to ensure Flight Filter Table in Database 2." }
                }
            }
        } else {
            Logger.error { "Failed to verify Database 2 connection." }
        }
    }

    aas3 = MySQLAdapter(3)
    aas3!!.VerifyConnection { success ->
        if (success) {
            Logger.info {"Database 3 connection verified."}
            aas3!!.CreateFlightFilterTable { success ->
                if (success) {
                    Logger.info { "Flight Filter Table ensured in Database 3." }
                } else {
                    Logger.error { "Failed to ensure Flight Filter Table in Database 3." }
                }
            }
        } else {
            Logger.error { "Failed to verify Database 3 connection." }
        }
    }

    activeclient.MessageConsumer = Consumer{ message ->

    }

    Runtime.getRuntime().addShutdownHook(Thread {
        webUI.Stop()
        activeclient.Stop()
        // shutdown tinylog properly
        ProviderRegistry.getLoggingProvider().shutdown()
    })
}