package dev.fyloz.trial.colorrecipesexplorer.config;

import dev.fyloz.trial.colorrecipesexplorer.ColorRecipesExplorerApplication;
import dev.fyloz.trial.colorrecipesexplorer.config.properties.CREProperties;
import dev.fyloz.trial.colorrecipesexplorer.config.properties.MaterialTypeProperties;
import dev.fyloz.trial.colorrecipesexplorer.service.PasswordService;
import dev.fyloz.trial.colorrecipesexplorer.service.files.FilesService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.MessageSource;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.io.IOException;
import java.util.List;

@Configuration
public class SpringConfiguration {

    private FilesService filesService;
    private MessageSource messageSource;
    private CREProperties creProperties;
    private MaterialTypeProperties materialTypeProperties;

    @Autowired
    public void setFilesService(FilesService filesService) {
        this.filesService = filesService;
    }

    @Autowired
    public void setMessageSource(MessageSource messageSource) {
        this.messageSource = messageSource;
    }

    @Autowired
    @Qualifier("CREProperties")
    public void setCreProperties(CREProperties creProperties) {
        this.creProperties = creProperties;
    }

    @Autowired
    public void setMaterialTypeProperties(MaterialTypeProperties materialTypeProperties) {
        this.materialTypeProperties = materialTypeProperties;
    }

    @Bean
    public Logger getLogger() {
        return LoggerFactory.getLogger(ColorRecipesExplorerApplication.class);
    }

    @Bean
    public void setPreferences() {
        Preferences.urlUsePort = creProperties.isUrlUsePort();
        Preferences.urlUseHttps = creProperties.isUrlUseHttps();
        Preferences.uploadDirectory = creProperties.getUploadDirectory();
        Preferences.passwordsFileName = creProperties.getPasswordFile();
        Preferences.logger = getLogger();
        Preferences.messageSource = messageSource;
        Preferences.baseMaterialTypeName = materialTypeProperties.getBaseName();
    }

    @Bean
    public void initializePasswords() {
        Logger logger = Preferences.logger;
        String filePath = filesService.getPath(Preferences.passwordsFileName);

        logger.info("Le fichier des utilisateurs se situe à: " + filesService.getFile(filePath).getAbsolutePath());

        try {
            if (!filesService.exists(filePath)) filesService.create(filePath);
            List<String> fileContent = filesService.readAsStrings(filePath);

            if (fileContent.size() < 1) {
                logger.warn("Aucun mot de passe trouvé. Il sera impossible d'utiliser certaines fonctionnalités de l'application.");
            }

            for (String line : fileContent) {
                PasswordService.addPassword(line);
            }
        } catch (IOException e) {
            logger.error("Une erreur est survenue lors du chargement du fichier des utilisateurs", e);
            logger.warn("Il sera impossible d'utiliser certaines fonctionnalités de l'application.");
        }
    }

}
