package squeek.creativeblocks.config;

import java.io.*;
import java.util.ArrayList;
import java.util.List;
import org.apache.commons.io.FilenameUtils;
import squeek.creativeblocks.CreativeBlocks;
import squeek.creativeblocks.ModInfo;
import squeek.creativeblocks.helpers.FileHelper;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;

public class JSONConfigHandler
{
	private static final Gson gson = new GsonBuilder().enableComplexMapKeySerialization().setPrettyPrinting().create();
	private static List<File> configFiles = new ArrayList<File>();
	public static final String defaultConfigFileName = "default.json";
	public static final String defaultConfigRelativePath = "config/" + defaultConfigFileName;

	public static void setup(File configDirectory)
	{
		File modConfigDirectory = new File(configDirectory, ModInfo.MODID);
		if (!modConfigDirectory.exists())
		{
			modConfigDirectory.mkdirs();
		}
		for (File potentialConfigFile : modConfigDirectory.listFiles())
		{
			if (!FilenameUtils.getExtension(potentialConfigFile.getName()).equalsIgnoreCase("json"))
				continue;

			configFiles.add(potentialConfigFile);
		}
		if (shouldWriteDefaultConfig())
		{
			File defaultConfigDest = new File(configDirectory, defaultConfigFileName);
			boolean wasOverwritten = defaultConfigDest.exists();

			writeDefaultConfig(defaultConfigDest);

			if (!wasOverwritten && defaultConfigDest.exists())
				configFiles.add(defaultConfigDest);
		}
	}

	public static void writeDefaultConfig(File defaultConfigDest)
	{
		try
		{
			if (CreativeBlocks.instance.sourceFile.isDirectory())
			{
				File sourceFile = new File(CreativeBlocks.instance.sourceFile, defaultConfigRelativePath);
				FileHelper.copyFile(sourceFile, defaultConfigDest, true);
			}
			else
			{
				InputStream defaultConfigInputStream = JSONConfigHandler.class.getClassLoader().getResourceAsStream(defaultConfigRelativePath);
				FileHelper.copyFile(defaultConfigInputStream, defaultConfigDest, true);
				defaultConfigInputStream.close();
			}
		}
		catch (IOException e)
		{
			e.printStackTrace();
		}
	}

	public static boolean shouldWriteDefaultConfig()
	{
		// create default if there are no other .json files
		if (configFiles.isEmpty())
			return true;

		// update default if it already exists
		for (File configFile : configFiles)
		{
			if (configFile.getName().equalsIgnoreCase(defaultConfigFileName))
				return true;
		}

		return false;
	}

	public static void load()
	{
		for (File configFile : configFiles)
		{
			try
			{
				FileReader reader = new FileReader(configFile);
				JSONConfigFormat parsedConfig = gson.fromJson(reader, JSONConfigFormat.class);
				if (parsedConfig != null)
				{
					for (String blockToWhitelist : parsedConfig.blockWhitelist)
					{
						CreativeBlocksRegistry.whitelists.add(BlocksSpecifier.fromString(blockToWhitelist));
					}
					for (String blockToBlacklist : parsedConfig.blockBlacklist)
					{
						CreativeBlocksRegistry.blacklists.add(BlocksSpecifier.fromString(blockToBlacklist));
					}
				}
				reader.close();
			}
			catch (FileNotFoundException e)
			{
				e.printStackTrace();
			}
			catch (IOException e)
			{
				e.printStackTrace();
			}
		}
	}

}
@@ -1140,47 +1140,8 @@
 %09%09%7D%0A
-%09%09if (shouldWriteDefaultConfig())%0A%09%09%7B%0A%09
 %09%09Fi
@@ -1164,33 +1164,36 @@
 Dest = new File(
-c
+modC
 onfigDirectory, 
@@ -1216,16 +1216,71 @@
 eName);%0A
+%09%09if (shouldWriteDefaultConfig(defaultConfigDest))%0A%09%09%7B%0A
 %09%09%09boole
@@ -1383,26 +1383,8 @@
 f (!
-wasOverwritten && 
 defa
@@ -1407,16 +1407,113 @@
 ists())%0A
+%09%09%09%09CreativeBlocks.Log.warn(%22Default config failed to be written%22);%0A%09%09%09else if (!wasOverwritten)%0A
 %09%09%09%09conf
@@ -2228,16 +2228,38 @@
 tConfig(
+File defaultConfigDest
 )%0A%09%7B%0A%09%09/
@@ -2260,19 +2260,19 @@
 %09%7B%0A%09%09// 
-cre
+upd
 ate defa
@@ -2282,64 +2282,56 @@
  if 
+i
 t
-here
  a
+l
 re
- no other .json files%0A%09%09if (configFiles.isEmpty
+ady exists%0A%09%09if (defaultConfigDest.exists
 ())%0A
@@ -2346,25 +2346,24 @@
 n true;%0A
-%0A
 %09%09// 
-upd
+cre
 ate defa
@@ -2373,71 +2373,46 @@
  if 
-i
 t
+here
  a
-l
 re
-ady exists%0A%09%09for (File configFile : configF
+ no other .json f
 iles
-)
 %0A%09%09
-%7B%0A%09%09%09
+else 
 if (
@@ -2425,82 +2425,119 @@
 File
-.getName().equalsIgnoreCase(defaultConfigFileName))%0A%09%09%09%09return true;%0A%09%09%7D%0A%0A
+s.isEmpty())%0A%09%09%09return true;%0A%09%09// don't reinstate default if it doesn't exist and other .json files do%0A%09%09else%0A%09
 %09%09re

Fix default config being put in the wrong place * Was being written to config/default.json instead of config/CreativeBlocks/default.json * Fixes #5