package net.pietru.omni_power;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.utils.Json;
import com.badlogic.gdx.utils.JsonValue;
import finalforeach.cosmicreach.GameAssetLoader;
import finalforeach.cosmicreach.blockevents.BlockEvents;
import finalforeach.cosmicreach.blocks.Block;
import finalforeach.cosmicreach.blocks.BlockStateGenerator;
import finalforeach.cosmicreach.items.ItemThing;
import net.pietru.omni_power.blockevents.Connect;
import net.pietru.omni_power.blockevents.OpenCustomUI;
import net.pietru.omni_power.blockevents.Power;
import dev.crmodders.cosmicquilt.api.entrypoint.ModInitializer;
import net.pietru.omni_power.blocks.CustomUIBlockEntity;
import net.pietru.omni_power.mixins.BlockStateGeneratorMixin;
import org.quiltmc.loader.api.ModContainer;

import java.io.*;
import java.nio.file.DirectoryStream;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.*;
import java.util.regex.Pattern;
import java.util.zip.ZipEntry;
import java.util.zip.ZipException;
import java.util.zip.ZipFile;

public class OmniPower implements ModInitializer {
	public static final String MOD_ID = "omni_power";
	public static boolean block_entities = true;
	public static final String[] blocks = {
			"cable",
			"lever",
			"door_cube",
			"cable_merger",
			"cable_adapter",
			"custom_crate",
			"custom_boombox"
	};

	public static Map<String,JsonValue> uis = new HashMap<>();
	@Override
	public void onInitialize(ModContainer mod) {
		//if (QuiltLoader.isModLoaded("becraft"))
		System.out.println("OmniPower Mod Initialized!");

		List<String> paths = List.of(Gdx.files.internal(MOD_ID + "/assets.txt").readString().split("\n"));
		Json json = new Json();
        for (String path : paths){
			if (path.contains("block_events")) {
				System.out.println("Loading " + path);
				BlockEvents.loadBlockEventsFromAsset(json,GameAssetLoader.loadAsset(path)); //Gdx.files.classpath(path)
			}
        }

        System.out.println("Registered OmniPower Events");

		CustomUIBlockEntity.registerBlockEntityCreator();

		System.out.println("Registered OmniPower Entities");

		for (String block:blocks) {
			System.out.println(block);
			try {
				Block.loadBlock(GameAssetLoader.loadAsset("omni_power:blocks/"+block+".json"));
			} catch (Exception var3) {
				throw new RuntimeException("Error parsing block: " + block, var3);
			}
		}

		System.out.println("Registered OmniPower Blocks");

		load_custom_ui("omni_power:gui/example.json");
		load_custom_ui("omni_power:gui/example2.json");
		load_custom_ui("omni_power:gui/example-boombox.json");
	}

	public static void load_power_event(){
		try {
			BlockEvents.registerBlockEventAction(Power.class);
			BlockEvents.registerBlockEventAction(OpenCustomUI.class);
			BlockEvents.registerBlockEventAction(Connect.class);
		} catch (Exception e){
			System.out.println(e);
			System.out.println("If msg above says something Duplicate block event action key it might be intended behaviour...");
		}
	}

	public static void load_custom_ui(final String path){
		JsonValue loadJson = GameAssetLoader.loadJson(path);
		if (loadJson==null)
			throw new RuntimeException("Error parsing ui: " + path);
		String id = loadJson.getString("id");
		if (id==null || id.isEmpty())
			throw new RuntimeException("Invalid UI ID: " + path);
		JsonValue layout = loadJson.get("layout");
		if (layout==null || !layout.isArray())
			throw new RuntimeException("Error parsing ui layout: " + path);
		uis.put(id, loadJson);
	}
}

