package org.adrianvictor.lib.impl.b1_7_3.configuration;

import java.io.BufferedWriter;
import java.io.File;
import java.io.IOException;
import java.io.Reader;
import java.nio.file.Files;
import java.nio.file.Path;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.List;
import java.util.Map;

public class Configuration implements org.adrianvictor.lib.configuration.provider.ConfigurationProvider {
    org.bukkit.util.config.Configuration config;

    @Override
    public void load(File file) {
        config = new org.bukkit.util.config.Configuration(file);
    }

    @Override
    public void load(String contents) throws IOException {
        String timestamp = LocalDateTime.now()
                .format(DateTimeFormatter.ofPattern("HH-mm-dd-MM-yyyy"));
        Path temp = Files.createTempFile("tlib-configprovider-tmp-%s".formatted(timestamp), ".yml");
        Files.writeString(temp, contents);
        File file = temp.toFile();
        load(file);
    }

    @Override
    public void load(Reader reader) throws IOException {
        String timestamp = LocalDateTime.now()
                .format(DateTimeFormatter.ofPattern("HH-mm-dd-MM-yyyy"));
        Path temp = Files.createTempFile("tlib-configprovider-tmp-%s".formatted(timestamp), ".yml");

        try (BufferedWriter writer = Files.newBufferedWriter(temp)) {
            reader.transferTo(writer);
        }

        File file = temp.toFile();
        load(file);
    }

    @Override
    public boolean save(File file) {
        return config.save();
    }

    @Override
    public boolean save() {
        return config.save();
    }

    @Override
    public String saveToString() {
        return config.toString();
    }

    @Override
    public Object getProperty(String path) {
        return config.getProperty(path);
    }

    @Override
    public void setProperty(String path, Object value) {
        config.setProperty(path, value);
    }

    @Override
    public Map<String, Object> getAll() {
        return config.getAll();
    }

    @Override
    public boolean getBoolean(String path, boolean def) {
        return config.getBoolean(path, def);
    }

    @Override
    public List<Boolean> getBooleanList(String path, List<Boolean> def) {
        return config.getBooleanList(path, def);
    }

    @Override
    public double getDouble(String path, double def) {
        return config.getDouble(path, def);
    }

    @Override
    public List<Double> getDoubleList(String path, List<Double> def) {
        return config.getDoubleList(path, def);
    }

    @Override
    public int getInt(String path, int def) {
        return config.getInt(path, def);
    }

    @Override
    public String getString(String path) {
        return config.getString(path);
    }

    @Override
    public String getString(String path, String def) {
        return config.getString(path, def);
    }

    @Override
    public List<String> getStringList(String path, List<String> def) {
        return config.getStringList(path, def);
    }

    @Override
    public List<Integer> getIntList(String path, List<Integer> def) {
        return config.getIntList(path, def);
    }

    @Override
    public List<String> getKeys(String path) {
        return config.getKeys(path);
    }

    @Override
    public List<String> getKeys() {
        return config.getKeys();
    }

    @Override
    public List<Object> getList(String path) {
        return config.getList(path);
    }
}
