package fyloz.trial.ColorRecipesExplorer.core.io;

import fyloz.trial.ColorRecipesExplorer.ColorRecipesExplorerApplication;
import org.slf4j.Logger;

import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;

import static fyloz.trial.ColorRecipesExplorer.web.StringBank.UPLOAD_LOCATION;

public class FileHandler {

    protected String name;
    protected Logger logger = ColorRecipesExplorerApplication.logger;

    private FileContext context;
    private FileExtension extension;
    private File file;

    public FileHandler(String name, FileContext context, FileExtension extension) {
        this.context = context;
        this.extension = extension;

        setName(name);
        file = getFile();
    }

    public boolean createFile() {
        File parent = file.getParentFile();

        if (!isValid()) {
            if ((!parent.exists() || !parent.isDirectory()) && !parent.mkdirs()) {
                return false;
            }

            try {
                logger.info("Création du fichier " + file.getAbsolutePath());
                return file.createNewFile();
            } catch (IOException e) {
                logger.error("Erreur à la création d'un fichier", e);
                return false;
            }
        } else {
            logger.warn("Tentative de création du fichier existant " + file.getAbsolutePath());
            return false;
        }
    }

    public byte[] readFile() {
        if (isValid()) {
            try {
                return Files.readAllBytes(getPath());
            } catch (IOException e) {
                logger.error("Erreur lors de la lecture d'un fichier", e);
            }
        }

        logger.warn("Tentative de lecture du fichier invalide " + file.getAbsolutePath());
        return null;
    }

    public boolean deleteFile() {
        if (isValid()) {
            logger.info("Suppression du fichier " + file.getAbsolutePath());
            return file.delete();
        }

        logger.warn("Tentative de suppression du fichier invalide " + file.getAbsolutePath());
        return false;
    }

    public boolean isValid() {
        return file.exists() && file.isFile();
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;

        file = getFile();
    }

    public FileContext getContext() {
        return context;
    }

    public Path getPath() {
        return Paths.get(String.format("%s/%s/%s%s", UPLOAD_LOCATION, context.getPath(), name, extension.getExtension()));
    }

    public File getFile() {
        return getPath().toFile();
    }

    public enum FileContext {
        IMAGE("images"),
        SIMDUT("simdut"),
        PDF("pdf"),
        OTHERS("");

        private String path;

        FileContext(String path) {
            this.path = path;
        }

        public String getPath() {
            return path;
        }
    }

    public enum FileExtension {
        JPEG("jpeg"),
        PDF("pdf"),
        TXT("txt"),
        DB("db");

        private String extension;

        FileExtension(String extension) {
            this.extension = extension;
        }

        public String getExtension() {
            return "." + extension;
        }
    }
}
