package net.mcreator.cawezsmantletostratus.procedures;


import net.neoforged.neoforge.event.tick.PlayerTickEvent;
import net.neoforged.fml.common.EventBusSubscriber;
import net.neoforged.bus.api.SubscribeEvent;
import net.neoforged.bus.api.Event;
import com.google.gson.Gson;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;

import net.neoforged.neoforge.event.tick.PlayerTickEvent;
import net.neoforged.fml.common.EventBusSubscriber;
import net.neoforged.bus.api.SubscribeEvent;
import net.neoforged.bus.api.Event;

import net.minecraft.world.level.LevelAccessor;
import net.minecraft.world.entity.Entity;
import net.minecraft.network.chat.Component;

import javax.annotation.Nullable;
import net.neoforged.neoforge.event.tick.PlayerTickEvent;
import net.neoforged.fml.common.EventBusSubscriber;
import net.neoforged.bus.api.SubscribeEvent;
import net.neoforged.bus.api.Event;

import net.minecraft.world.entity.Entity;
import net.minecraft.server.level.ServerLevel;
import net.minecraft.commands.CommandSourceStack;
import net.minecraft.commands.CommandSource;

import javax.annotation.Nullable;



import java.io.FileReader;
import java.io.Reader;

import javax.annotation.Nullable;

import java.io.File;   
import java.io.FileWriter;   
@EventBusSubscriber
public class DetectCDCProcedure {

	static String fileName = "config/cc_dim_stack.json";
	static String file = "";
	
	@SubscribeEvent
	public static void onWorldLoad(net.neoforged.neoforge.event.level.LevelEvent.Load event) {
		execute(event);
	}

	public static void execute() {
		execute(null);
	}

	private static void execute(@Nullable Event event) {


		// Loads!
		if (file.equals("")) {
			try (Reader reader = new FileReader(fileName)) {
	            Gson gson = new Gson();
	            JsonElement jsonElement = JsonParser.parseReader(reader);
	            JsonObject jsonObject = jsonElement.getAsJsonObject();
	            String name = jsonObject.get("name").getAsString();
	            System.out.println("CAWEZ's Mantle to Startus found configuration: " + name);
	            file = jsonObject.toString();
	            if (name.equals("default")) {
	            	System.out.println("Default configuration found, overwriting...");
	            	FileWriter myWriter = new FileWriter(fileName);
					myWriter.write("{\n\t\"name\": \"cawezs_mantle_to_stratus_autowrite\",\n\t\"stack\": {\n\n\n\t\t\"cawezs_mantle_to_stratus:inverted\":{\n\t\t\t\"dim_height\": 256,\n\t\t\t\"ceiling_replace\": \"minecraft:bedrock\",\n\t\t\t\"ceiling_with\": \"minecraft:obsidian\",\n\t\t\t\"dim_above\": \"minecraft:the_nether\",\n\n\t\t\t\"dim_depth\": 0,\n\t\t\t\"floor_replace\": \"none\",\n\t\t\t\"floor_with\": \"none\",\n\t\t\t\"dim_below\": \"minecraft:the_end\"\n\t\t},\n\t\n\t\t\"minecraft:the_end\":{\n\t\t\t\"dim_height\": 256,\n\t\t\t\"ceiling_replace\": \"none\",\n\t\t\t\"ceiling_with\": \"none\",\n\t\t\t\"dim_above\": \"cawezs_mantle_to_stratus:inverted\",\n\n\t\t\t\"dim_depth\": 0,\n\t\t\t\"floor_replace\": \"none\",\n\t\t\t\"floor_with\": \"none\",\n\t\t\t\"dim_below\": \"cawezs_mantle_to_stratus:heights\"\n\t\t},\n\n\t\t\"cawezs_mantle_to_stratus:heights\":{\n\t\t\t\"dim_height\": 300,\n\t\t\t\"ceiling_replace\": \"none\",\n\t\t\t\"ceiling_with\": \"none\",\n\t\t\t\"dim_above\": \"minecraft:the_end\",\n\n\t\t\t\"dim_depth\": 0,\n\t\t\t\"floor_replace\": \"none\",\n\t\t\t\"floor_with\": \"none\",\n\t\t\t\"dim_below\": \"minecraft:overworld\"\n\t\t},\n\n\t\t\"minecraft:overworld\":{\n\t\t\t\"dim_height\": 320,\n\t\t\t\"ceiling_replace\": \"none\",\n\t\t\t\"ceiling_with\": \"none\",\n\t\t\t\"dim_above\": \"cawezs_mantle_to_stratus:heights\",\n\n\t\t\t\"dim_depth\": -64,\n\t\t\t\"floor_replace\": \"minecraft:bedrock\",\n\t\t\t\"floor_with\": \"minecraft:obsidian\",\n\t\t\t\"dim_below\": \"cawezs_mantle_to_stratus:depths\"\n\t\t},\n\t\t\n\t\t\"cawezs_mantle_to_stratus:depths\":{\n\t\t\t\"dim_height\": 256,\n\t\t\t\"ceiling_replace\": \"minecraft:bedrock\",\n\t\t\t\"ceiling_with\": \"minecraft:obsidian\",\n\t\t\t\"dim_above\": \"minecraft:overworld\",\n\n\t\t\t\"dim_depth\": 0,\n\t\t\t\"floor_replace\": \"minecraft:bedrock\",\n\t\t\t\"floor_with\": \"minecraft:obsidian\",\n\t\t\t\"dim_below\":\"minecraft:the_nether\"\n\t\t},\n\t\t\n\t\t\"minecraft:the_nether\":{\n\t\t\t\"dim_height\": 128,\n\t\t\t\"ceiling_replace\": \"minecraft:bedrock\",\n\t\t\t\"ceiling_with\": \"minecraft:obsidian\",\n\t\t\t\"dim_above\": \"cawezs_mantle_to_stratus:depths\",\n\n\t\t\t\"dim_depth\": 0,\n\t\t\t\"floor_replace\": \"minecraft:barrier\",\n\t\t\t\"floor_with\": \"minecraft:air\",\n\t\t\t\"dim_below\": \"cawezs_mantle_to_stratus:inverted\"\n\t\t}\n\t}\n}\n");
      				myWriter.close();
      		
	            	
	            }
	        } catch (Exception e) {
				System.out.println("Not using CAWEZ's Connectivity!!");
	            e.printStackTrace();
	            file = "null";
	        }
		}
	

	}
}

/* Default config
{
    "name": "cawezs_mantle_to_stratus_autowrite",
    "stack": {


        "cawezs_mantle_to_stratus:inverted":{
            "dim_height": 256,
            "ceiling_replace": "minecraft:bedrock",
            "ceiling_with": "minecraft:obsidian",
            "dim_above": "minecraft:the_nether",

            "dim_depth": 0,
            "floor_replace": "none",
            "floor_with": "none",
            "dim_below": "minecraft:the_end"
        },
    
        "minecraft:the_end":{
            "dim_height": 256,
            "ceiling_replace": "none",
            "ceiling_with": "none",
            "dim_above": "cawezs_mantle_to_stratus:inverted",

            "dim_depth": 0,
            "floor_replace": "none",
            "floor_with": "none",
            "dim_below": "cawezs_mantle_to_stratus:heights"
        },

        "cawezs_mantle_to_stratus:heights":{
            "dim_height": 300,
            "ceiling_replace": "none",
            "ceiling_with": "none",
            "dim_above": "minecraft:the_end",

            "dim_depth": 0,
            "floor_replace": "none",
            "floor_with": "none",
            "dim_below": "minecraft:overworld"
        },

        "minecraft:overworld":{
            "dim_height": 320,
            "ceiling_replace": "none",
            "ceiling_with": "none",
            "dim_above": "cawezs_mantle_to_stratus:heights",

            "dim_depth": -64,
            "floor_replace": "minecraft:bedrock",
            "floor_with": "minecraft:obsidian",
            "dim_below": "cawezs_mantle_to_stratus:depths"
        },
        
        "cawezs_mantle_to_stratus:depths":{
            "dim_height": 256,
            "ceiling_replace": "minecraft:bedrock",
            "ceiling_with": "minecraft:obsidian",
            "dim_above": "minecraft:overworld",

            "dim_depth": 0,
            "floor_replace": "minecraft:bedrock",
            "floor_with": "minecraft:obsidian",
            "dim_below": "minecraft:the_nether"
        },
        
        "minecraft:the_nether":{
            "dim_height": 128,
            "ceiling_replace": "minecraft:bedrock",
            "ceiling_with": "minecraft:obsidian",
            "dim_above": "cawezs_mantle_to_stratus:depths",

            "dim_depth": 0,
            "floor_replace": "minecraft:barrier",
            "floor_with": "minecraft:air",
            "dim_below": "cawezs_mantle_to_stratus:inverted"
        }
    }
}
 */
