package somecodes

import com.fasterxml.jackson.module.kotlin.jacksonObjectMapper
import java.nio.file.Path
import kotlin.io.path.Path

class configFile {
    var ZelloUsername: String? = "gtcdevice01"
    var ZelloPassword: String? = "GtcDev2025"
    var ZelloChannel: String? = "GtcDev2025"
    var ZelloServer: String? = "community"
    var ZelloWorkNetworkName: String? = ""
    var ZelloEnterpriseServerDomain: String? = ""
    var M1: String? = ""
    var M2: String? = ""
    var M3: String? = ""
    var M4: String? = ""
    var M5: String? = ""
    var M6: String? = ""
    var M7: String? = ""
    var M8: String? = ""

    private val filepath : Path = Path(System.getProperty("user.dir"), "config.json")

    @Suppress("UNCHECKED_CAST")
    fun Load(){
        if (filepath.toFile().exists()){
            // file found, then load the configuration to configFile object
            try{
                val json = filepath.toFile().readText()
                val objectMapper = jacksonObjectMapper()
                val configMap = objectMapper.readValue(json, Map::class.java) as Map<String, String>

                ZelloUsername = configMap["ZelloUsername"]
                ZelloPassword = configMap["ZelloPassword"]
                ZelloChannel = configMap["ZelloChannel"]
                ZelloServer = configMap["ZelloServer"]
                ZelloWorkNetworkName = configMap["ZelloWorkNetworkName"]
                ZelloEnterpriseServerDomain = configMap["ZelloEnterpriseServerDomain"]
                M1 = configMap["M1"]
                M2 = configMap["M2"]
                M3 = configMap["M3"]
                M4 = configMap["M4"]
                M5 = configMap["M5"]
                M6 = configMap["M6"]
                M7 = configMap["M7"]
                M8 = configMap["M8"]
            } catch (e: Exception) {
                println("Error loading configuration: ${e.message}")
            }
        } else CreateDefaultConfig()
    }

    fun CreateDefaultConfig() {
        ZelloUsername = "gtcdevice01"
        ZelloPassword = "GtcDev2025"
        ZelloChannel = "GtcDev2025"
        ZelloServer = "community"
        ZelloWorkNetworkName = ""
        ZelloEnterpriseServerDomain = ""
        M1 = ""
        M2 = ""
        M3 = ""
        M4 = ""
        M5 = ""
        M6 = ""
        M7 = ""
        M8 = ""
        Save()
    }

    fun Save(){
        try {
            // Convert the configFile object to JSON and write it to the file
            val js = mapOf(
                "ZelloUsername" to ZelloUsername,
                "ZelloPassword" to ZelloPassword,
                "ZelloChannel" to ZelloChannel,
                "ZelloServer" to ZelloServer,
                "ZelloWorkNetworkName" to ZelloWorkNetworkName,
                "ZelloEnterpriseServerDomain" to ZelloEnterpriseServerDomain,
                "M1" to M1,
                "M2" to M2,
                "M3" to M3,
                "M4" to M4,
                "M5" to M5,
                "M6" to M6,
                "M7" to M7,
                "M8" to M8
            )

            val mapper = jacksonObjectMapper()
            mapper.writerWithDefaultPrettyPrinter().writeValue(filepath.toFile(), js)
        } catch (e: Exception) {
            println("Error saving configuration: ${e.message}")
        }
    }
}