#include "glslang/Include/glslang_c_interface.h"
#include "glslang/Public/resource_limits_c.h"
#include "vulkan/VulkanUtils.h"
#include <cstdio>
#include <shared/HelpersGLFW.h>
#include <shared/Utils.h>
#include <sys/types.h>
#include <vector>

void saveSpirvBinaryFile(const char *filename, const uint8_t *code,
                         size_t size) {
  FILE *f = fopen(filename, "wb");
  fwrite(code, sizeof(uint8_t), size, f);
  fclose(f);
}

void testShaderCompilation(const char *sourceFilename,
                           const char *destFilename) {
  std::string shaderSource = readShaderFile(sourceFilename);
  std::vector<uint8_t> spirv;

  lvk::Result res = lvk::compileShader(
      vkShaderStageFromFileName(sourceFilename), shaderSource.c_str(), &spirv,
      glslang_default_resource());

  saveSpirvBinaryFile(destFilename, spirv.data(), spirv.size());
}

int main(int argc, char *argv[]) {
  glslang_initialize_process();

  testShaderCompilation("Chapter01/04_GLSLang/src/main.vert",
                        ".cache/04_GLSLang.vert.bin");
  testShaderCompilation("Chapter01/04_GLSLang/src/main.frag",
                        ".cache/04_GLSLang.frag.bin");

  glslang_finalize_process();
  return 0;
}
