package Other

import org.tinylog.Logger
import java.io.FileInputStream
import java.io.FileNotFoundException
import java.io.FileOutputStream
import java.nio.file.Files
import java.nio.file.Paths
import java.time.LocalDateTime
import java.util.Properties
import kotlin.io.path.Path

@Suppress("unused")
class Config {
    private val prop = Properties()
    // config.properties is saved in current working directory
    private val filename = Paths.get(System.getProperty("user.dir"), "config.properties")
    var WebPort : Int
        get() = prop.getProperty("webport").toInt()
        set(value) {prop.setProperty("webport", value.toString())}
    var WebUsername : String
        get() = prop.getProperty("webusername")
        set(value) {prop.setProperty("webusername", value)}
    var WebPassword : String
        get() = prop.getProperty("webpassword")
        set(value) {prop.setProperty("webpassword", value)}
    var ActiveMQ_BrokerURL : String
        get() = prop.getProperty("activemq_brokerurl")
        set(value) {prop.setProperty("activemq_brokerurl", value)}
    var ActiveMQ_Username : String
        get() = prop.getProperty("activemq_username")
        set(value) {prop.setProperty("activemq_username", value)}
    var ActiveMQ_Password : String
        get() = prop.getProperty("activemq_password")
        set(value) {prop.setProperty("activemq_password", value)}
    var ActiveMQ_QueueName : String
        get() = prop.getProperty("activemq_queuename")
        set(value) {prop.setProperty("activemq_queuename", value)}
    var MySQL_AAS1 : String
        get() = prop.getProperty("mysql_aas1")
        set(value) {prop.setProperty("mysql_aas1", value)}
    var MySQL_AAS2 : String
        get() = prop.getProperty("mysql_aas2")
        set(value) {prop.setProperty("mysql_aas2", value)}
    var MySQL_AAS3 : String
        get() = prop.getProperty("mysql_aas3")
        set(value) {prop.setProperty("mysql_aas3", value)}
    var MySQL_AAS1_Username : String
        get() = prop.getProperty("mysql_aas1_username")
        set(value) {prop.setProperty("mysql_aas1_username", value)}
    var MySQL_AAS1_Password : String
        get() = prop.getProperty("mysql_aas1_password")
        set(value) {prop.setProperty("mysql_aas1_password", value)}
    var MySQL_AAS2_Username : String
        get() = prop.getProperty("mysql_aas2_username")
        set(value) {prop.setProperty("mysql_aas2_username", value)}
    var MySQL_AAS2_Password : String
        get() = prop.getProperty("mysql_aas2_password")
        set(value) {prop.setProperty("mysql_aas2_password", value)}
    var MySQL_AAS3_Username : String
        get() = prop.getProperty("mysql_aas3_username")
        set(value) {prop.setProperty("mysql_aas3_username", value)}
    var MySQL_AAS3_Password : String
        get() = prop.getProperty("mysql_aas3_password")
        set(value) {prop.setProperty("mysql_aas3_password", value)}

    fun Load(){
        try{
            if (Files.isRegularFile(filename)){
                prop.load(FileInputStream(filename.toString()))
                if (!prop.containsKey("webport")) throw Exception("Invalid config file: missing 'webport'")
                if (!prop.containsKey("webusername")) throw Exception("Invalid config file: missing 'webusername'")
                if (!prop.containsKey("webpassword")) throw Exception("Invalid config file: missing 'webpassword'")
                if (!prop.containsKey("activemq_brokerurl")) throw Exception("Invalid config file: missing 'activemq_brokerurl'")
                if (!prop.containsKey("activemq_username")) throw Exception("Invalid config file: missing 'activemq_username'")
                if (!prop.containsKey("activemq_password")) throw Exception("Invalid config file: missing 'activemq_password'")
                if (!prop.containsKey("activemq_queuename")) throw Exception("Invalid config file: missing 'activemq_queuename'")
                if (!prop.containsKey("mysql_aas1")) throw Exception("Invalid config file: missing 'mysql_aas1'")
                if (!prop.containsKey("mysql_aas2")) throw Exception("Invalid config file: missing 'mysql_aas2'")
                if (!prop.containsKey("mysql_aas3")) throw Exception("Invalid config file: missing 'mysql_aas3'")
                if (!prop.containsKey("mysql_aas1_username")) throw Exception("Invalid config file: missing 'mysql_aas1_username'")
                if (!prop.containsKey("mysql_aas1_password")) throw Exception("Invalid config file: missing 'mysql_aas1_password'")
                if (!prop.containsKey("mysql_aas2_username")) throw Exception("Invalid config file: missing 'mysql_aas2_username'")
                if (!prop.containsKey("mysql_aas2_password")) throw Exception("Invalid config file: missing 'mysql_aas2_password'")
                if (!prop.containsKey("mysql_aas3_username")) throw Exception("Invalid config file: missing 'mysql_aas3_username'")
                if (!prop.containsKey("mysql_aas3_password")) throw Exception("Invalid config file: missing 'mysql_aas3_password'")
                Logger.info { "Configuration loaded from config.properties" }
            } else throw FileNotFoundException("Config file not found")
        } catch (e: FileNotFoundException){
            Logger.error(e) { "Config file not found, Message : ${e.message}" }
            CreateDefault()
            Save()
        }
        catch (e: Exception){
            Logger.error(e) { "Error while loading config, Message : ${e.message}" }
            CreateDefault()
            Save()
        }


    }
    fun Save(){
        // save prop to file "save.properties"
        // comment is save date and time
        val comment = "Configuration saved on ${LocalDateTime.now()}"
        try{
            prop.store(FileOutputStream(filename.toString()), comment)
            Logger.info { "Configuration saved to config.properties" }
        } catch (ex: Exception){
            Logger.error(ex) { "Error while saving config, Message : ${ex.message}" }
        }
    }
    fun CreateDefault(){
        prop.clear()
        prop.setProperty("webport", "7000")
        prop.setProperty("webusername", "admin")
        prop.setProperty("webpassword", "admin")
        prop.setProperty("activemq_brokerurl", "tcp://localhost:61616")
        prop.setProperty("activemq_username", "admin")
        prop.setProperty("activemq_password", "admin")
        prop.setProperty("activemq_queuename", "TEST.QUEUE")
        prop.setProperty("mysql_aas1", "jdbc:mysql://192.168.10.10:3306/aas")
        prop.setProperty("mysql_aas2", "jdbc:mysql://192.168.10.11:3306/aas")
        prop.setProperty("mysql_aas3"," jdbc:mysql://192.168.10.12:3306/aas")
        prop.setProperty("mysql_aas1_username","admin")
        prop.setProperty("mysql_aas1_password","admin")
        prop.setProperty("mysql_aas2_username","admin")
        prop.setProperty("mysql_aas2_password","admin")
        prop.setProperty("mysql_aas3_username","admin")
        prop.setProperty("mysql_aas3_password","admin")
        Logger.info {"Default configuration created" }
    }
}