package net.halbear.Terrain4J.EngineCore.Logic.Rendering;

import com.google.gson.FieldNamingPolicy;
import com.google.gson.GsonBuilder;
import net.halbear.Terrain4J.EngineCore.Vulkan.Rendering.VkModel.MaterialData;
import net.halbear.Terrain4J.EngineCore.Vulkan.Rendering.VkModel.ModelData;
import org.tinylog.Logger;

import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class ModelLoader { // Model Utility class kinda like a model version of VulkanUtils
    public ModelLoader(){

    }

    public static List<MaterialData> LoadMaterials(String[] Path){
        var Result = new ArrayList<MaterialData>();
        for(int i = 0; i < Path.length; i++) {
            Logger.debug("Material loading step [{}], Loading Materials From [{}]", i, Path[i]);
            try {
                String content = new String(Files.readAllBytes(Paths.get(Path[i])));
                var gson = new GsonBuilder().setFieldNamingPolicy(FieldNamingPolicy.UPPER_CAMEL_CASE).create();
                Result.addAll(Arrays.asList(gson.fromJson(content, MaterialData[].class)));
            } catch (Exception e) {
                throw new RuntimeException(e);
            }
            Logger.debug("Loaded Material Data: [{}]", Result.toString());
        }
        return Result;
    }
    public static List<MaterialData> LoadMaterials(String Path){
        Logger.debug("Loading Materials From [{}]",Path);
        var Result = new ArrayList<MaterialData>();
        try{
            String content = new String(Files.readAllBytes(Paths.get(Path)));
            var gson = new GsonBuilder().setFieldNamingPolicy(FieldNamingPolicy.UPPER_CAMEL_CASE).create();
            Result.addAll(Arrays.asList(gson.fromJson(content,MaterialData[].class)));
        }catch(Exception e){
            throw new RuntimeException(e);
        }
        Logger.debug("Loaded Material Data: [{}]",Result.toString());
        return Result;
    }
    public static ModelData LoadModel(String Path){
        Logger.debug("Loading Models From [{}]",Path);
        ModelData Result;
        try{
            String content = new String(Files.readAllBytes(Paths.get(Path)));
            var gson = new GsonBuilder().setFieldNamingPolicy(FieldNamingPolicy.UPPER_CAMEL_CASE).create();
            Result = gson.fromJson(content,ModelData.class);
        }catch(Exception e){
            throw new RuntimeException(e);
        }
        Logger.debug("Loaded Model Data: [{}]",Result.toString());
        return Result;
    }
}
