package HarmonyGen.Util

import org.ini4j.Ini
import java.io.File

/**
 * Method to read the configuration file.
 *
 * This reads the configuration file. The *param* noted below
 * is optional. It defaults to the relative path **./config/settings.dev.ini**.
 * This will be expanded for better environment support.
 *
 * The file is simply read, if log level is at debug it will be printed to the logs.
 *
 * @author soulnothing
 * @since 0.1.0
 * @param java.io.File A file object pointing at the target configuration file.
 * @return org.ini4j.Ini The parse ini file.
 */
fun readConfig(config_file: File = File("./config/settings.dev.ini")): Ini {
  val config: Ini = Ini(config_file)
  // logger.debug("Reading config, received a value of $config")
  return config
}

/**
 * Read the configuration file and return the Config data class.
 *
 * See the read config function noted below. This function is largely a wrapper
 * that does type handling and conversion. Mapping to the data class for Config.
 *
 * @see HarmonyGen.Util.readConfig
 * @see HarmonyGen.Util.Config
 * @author soulnothing
 * @since 0.1.0
 * @return HarmonyGen.Util.Config A config data class.
 */
fun getConfig(): Config {
  val config = readConfig()
  return Config(
      LastFMAPIKey = config.get("LastFM", "api_key"),
      LastFMAPISecret = config.get("LastFM", "api_secret"),
      SpotifyClientID = config.get("Spotify", "client_id"),
      SpotifyClientSecret = config.get("Spotify", "client_secret"),
      SpotifyScopes = config.get("Spotify", "scopes").split(","),
      SpotifyPlaylist = config.get("Spotify", "playlist"),
      Secret = config.get("HarmonyGen", "secret"),
      MaxTracks = config.get("HarmonyGen", "playlist_length").toInt(),
      Applications = config.get("HarmonyGen", "applications").split(",").toList(),
      Database = config.get("Database", "database"),
      DatabaseHost = config.get("Database", "host"),
      DatabaseUser = config.get("Database", "user"),
      DatabasePassword = config.get("Database", "password"),
      DatabasePort = config.get("Database", "port").toInt(),
      MusicBrainzDatabase = config.get("MusicBrainzDatabase", "database"),
      MusicBrainzHost = config.get("MusicBrainzDatabase", "host"),
      MusicBrainzPort = config.get("MusicBrainzDatabase", "port").toInt(),
      MusicBrainzUser = config.get("MusicBrainzDatabase", "user"),
      MusicBrainzPassword = config.get("MusicBrainzDatabase", "password")
  )

}