package com.airlock.iam.plugin.framework.application.service;

import com.airlock.iam.base.api.domain.model.exception.FailureException;
import com.airlock.iam.plugin.api.exception.InitializeException;
import com.airlock.iam.plugin.framework.domain.model.MedusaConfigurationElement;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.nio.file.Files;

public interface ConfigReader {
   MedusaConfigurationElement read(InputStream var1) throws ConfigReader.ConfigLoaderException;

   default MedusaConfigurationElement readFromFile(File configFile) throws InitializeException {
      try (InputStream inputStream = Files.newInputStream(configFile.toPath())) {
         return this.read(inputStream);
      } catch (FileNotFoundException e) {
         throw new InitializeException("Configuration file to load was not found: " + configFile.getAbsolutePath(), e);
      } catch (FailureException | ConfigReader.ConfigLoaderException e) {
         throw new InitializeException("Unable to load the configuration from file: " + configFile.getName() + ": " + e.getMessage(), e);
      } catch (IOException e) {
         throw new InitializeException("Failed to read configuration file: " + configFile.getName() + ": " + e.getMessage(), e);
      }
   }

   class ConfigLoaderException extends Exception {
      public ConfigLoaderException(String message, Throwable cause) {
         super(message, cause);
      }

      public ConfigLoaderException(String message) {
         super(message);
      }

      public ConfigLoaderException(Throwable cause) {
         super(cause);
      }
   }
}
