package org.nwapw.abacus.config;

import com.moandjiezana.toml.Toml;
import com.moandjiezana.toml.TomlWriter;

import java.io.File;
import java.io.IOException;
import java.util.Arrays;
import java.util.HashSet;
import java.util.Set;

/**
 * The configuration object that stores
 * options that the user can change.
 */
public class Configuration {

    /**
     * The TOML writer used to write this configuration to a file.
     */
    private static final TomlWriter TOML_WRITER = new TomlWriter();
    /**
     * The TOML reader used to load this config from a file.
     */
    private static final Toml TOML_READER = new Toml();

    /**
     * The implementation of the number that should be used.
     */
    private String numberImplementation = "naive";
    /**
     * The list of disabled plugins in this Configuration.
     */
    private Set<String> disabledPlugins = new HashSet<>();

    /**
     * Creates a new configuration with the given values.
     * @param numberImplementation the number implementation, like "naive" or "precise"
     * @param disabledPlugins the list of disabled plugins.
     */
    public Configuration(String numberImplementation, String[] disabledPlugins){
        this.numberImplementation = numberImplementation;
        this.disabledPlugins.addAll(Arrays.asList(disabledPlugins));
    }

    /**
     * Loads a configuration from a given file, keeping non-specified fields default.
     * @param fromFile the file to load from.
     */
    public Configuration(File fromFile){
        if(!fromFile.exists()) return;
        copyFrom(TOML_READER.read(fromFile).to(Configuration.class));
    }

    /**
     * Copies the values from the given configuration into this one.
     * @param otherConfiguration the configuration to copy from.
     */
    public void copyFrom(Configuration otherConfiguration){
        this.numberImplementation = otherConfiguration.numberImplementation;
        this.disabledPlugins.addAll(otherConfiguration.disabledPlugins);
    }

    /**
     * Saves this configuration to the given file, creating
     * any directories that do not exist.
     * @param file the file to save to.
     */
    public void saveTo(File file){
        if(file.getParentFile() != null) file.getParentFile().mkdirs();
        try {
            TOML_WRITER.write(this, file);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * Gets the number implementation from this configuration.
     * @return the number implementation.
     */
    public String getNumberImplementation() {
        return numberImplementation;
    }

    /**
     * Sets the number implementation for the configuration
     * @param numberImplementation the number implementation.
     */
    public void setNumberImplementation(String numberImplementation) {
        this.numberImplementation = numberImplementation;
    }

    /**
     * Gets the list of disabled plugins.
     * @return the list of disabled plugins.
     */
    public Set<String> getDisabledPlugins() {
        return disabledPlugins;
    }

}

@@ -365,137 +365,322 @@
 The 
-TOML writer used to write this configuration to a file.%0A     */%0A    private static final TomlWriter TOML_WRITER = new TomlWriter(
+defaults TOML string.%0A     */%0A    private static final String DEFAULT_CONFIG =%0A            %22numberImplementation = %5C%22naive%5C%22%5Cn%22 +%0A            %22disabledPlugins = %5B%5D%22;%0A    /**%0A     * The defaults TOML object, parsed from the string.%0A     */%0A    private static final Toml DEFAULT_TOML = new Toml().read(DEFAULT_CONFIG
 );%0A 
@@ -702,20 +702,20 @@
 he TOML 
-read
+writ
 er used 
@@ -717,20 +717,21 @@
 used to 
-load
+write
  this co
@@ -734,21 +734,26 @@
 s config
- from
+uration to
  a file.
@@ -794,18 +794,24 @@
 Toml
+Writer
  TOML_
-READ
+WRIT
 ER =
@@ -819,16 +819,22 @@
 new Toml
+Writer
 ();%0A%0A   
@@ -950,21 +950,25 @@
 tion = %22
-naive
+%3Cdefault%3E
 %22;%0A    /
@@ -1783,19 +1783,30 @@
 rom(
-TOML_READER
+new Toml(DEFAULT_TOML)
 .rea

Add defaults that actually work.