#include "renderer.h"
#include "gpu.h"
#include "types.h"
#include <SDL3/SDL.h>
#include <SDL3/SDL_gpu.h>
#include <SDL3/SDL_gamepad.h>
#include <SDL3/SDL_oldnames.h>
#include <SDL3/SDL_pixels.h>
#include <SDL3/SDL_timer.h>
#include <SDL3_image/SDL_image.h>
#include <iomanip>
#include <iostream>
#include "bsp/BSP.h"

#include "imgui.h"
#include "imgui_impl_sdl3.h"
#include "imgui_impl_sdlgpu3.h"


static uint64_t visit_grid[64];

uint64_t simple_rand(uint64_t *s){
  static uint64_t state = 0xDEADBEEF;
  const uint64_t taps = 0x1BULL;
  uint8_t out = state & 1;
  state >>= 1;
  if(out) state ^= taps << 60;
  return state;
}

extern int post_mode;
extern vec2 atlas_adjust;

bool Renderer::init(SDL_Window* w) {
  win = w;
  dev = SDL_CreateGPUDevice(SDL_GPU_SHADERFORMAT_SPIRV, true, NULL);
  if (!dev) { std::cerr << "GPU device failed: " << SDL_GetError() << std::endl; return false; }

  SDL_ClaimWindowForGPUDevice(dev, win);

  load_sprites();

  SDL_GPUSamplerCreateInfo sprite_sampler_info {
    .min_filter    = SDL_GPU_FILTER_NEAREST,
    .mag_filter    = SDL_GPU_FILTER_NEAREST,
    .mipmap_mode   = SDL_GPU_SAMPLERMIPMAPMODE_NEAREST,
    .address_mode_u = SDL_GPU_SAMPLERADDRESSMODE_MIRRORED_REPEAT,
    .address_mode_v = SDL_GPU_SAMPLERADDRESSMODE_MIRRORED_REPEAT,
    .compare_op    = SDL_GPU_COMPAREOP_INVALID,
    .enable_compare = false,
  };

  atlas_sampler = SDL_CreateGPUSampler(dev, &sprite_sampler_info);
  render_sampler = create_linear_sampler(dev);
  
  resize_render_texture(960, 540);

  setup_imgui();

  fprintf(stderr, "Pipeline creation....");
  ray.create(dev, win);
  post.create(dev, win);
  fprintf(stderr, "finished\n");

  return post.pipeline && ray.pipeline;
}

void Renderer::destroy() {
  if (post_tex)       SDL_ReleaseGPUTexture(dev, post_tex);
  if (render_sampler) SDL_ReleaseGPUSampler(dev, render_sampler);
  if (vert_buff)      SDL_ReleaseGPUBuffer(dev, vert_buff);
  if (atlas_tex)      SDL_ReleaseGPUTexture(dev, atlas_tex);
  if (gamepad)        SDL_CloseGamepad(gamepad);
  ray.destroy(dev);
  post.destroy(dev);
  SDL_DestroyGPUDevice(dev);
}

bool Renderer::load_sprites() {
  const char* tex_path = "assets/bin/hl1.bin.png";

  SDL_GPUCommandBuffer* cmd = SDL_AcquireGPUCommandBuffer(dev);
  SDL_GPUCopyPass* copy_pass = SDL_BeginGPUCopyPass(cmd);
  int w,h;
  atlas_tex = IMG_LoadGPUTexture(dev, copy_pass, tex_path, &w, &h);
  SDL_EndGPUCopyPass(copy_pass);
  SDL_SubmitGPUCommandBuffer(cmd);

  if(atlas_tex) {
    SDL_Log("Loaded sprite atlas: %s, size: %dx%d", tex_path, w, h);
  } else {
    SDL_Log("Failed to load sprite atlas: %s, error: %s", tex_path, SDL_GetError());
  }

  return !!atlas_tex;
}

void Renderer::setup_imgui() {
  IMGUI_CHECKVERSION();
  ImGui::CreateContext();
  ImGuiIO& io = ImGui::GetIO();
  io.ConfigFlags |= ImGuiConfigFlags_NavEnableKeyboard;
  io.ConfigFlags |= ImGuiConfigFlags_NavEnableGamepad;

  ImGui::StyleColorsDark();

  ImGui_ImplSDL3_InitForSDLGPU(win);
  ImGui_ImplSDLGPU3_InitInfo init_info = {
    .Device = dev,
    .ColorTargetFormat = SDL_GetGPUSwapchainTextureFormat(dev, win),
    .MSAASamples = SDL_GPU_SAMPLECOUNT_1,
    .SwapchainComposition = SDL_GPU_SWAPCHAINCOMPOSITION_SDR,
    .PresentMode = SDL_GPU_PRESENTMODE_VSYNC,
  };

  ImGui_ImplSDLGPU3_Init(&init_info);
}

bool Renderer::load_bsp() {
  const std::string& path = "assets/hl1.bsp";
  HLBSP::BSP bsp(path);
  bsp.load_vertices();
  vertices = bsp.textured_vertices;

  SDL_GPUBufferCreateInfo info {
    .usage = SDL_GPU_BUFFERUSAGE_VERTEX,
    .size  = static_cast<uint32_t>(vertices.size() * sizeof(Vertex)),
  };
  vert_buff = SDL_CreateGPUBuffer(dev, &info);
  upload_buffer(dev, vert_buff, vertices.data(), info.size);
  return true;
}

void Renderer::resize_render_texture(uint32_t w, uint32_t h) {
  SDL_WaitForGPUIdle(dev);
  if (post_tex) SDL_ReleaseGPUTexture(dev, post_tex);

  post_tex = create_render_texture(dev, win, w, h);
  render_w = w;
  render_h = h;
}


void Renderer::pass_ray(SDL_GPUCommandBuffer* cmd, const CameraUBO& ubo) {
  SDL_PushGPUFragmentUniformData(cmd, 0, &ubo, sizeof(ubo));

  SDL_GPUColorTargetInfo tgt {
    .texture  = post_tex,
    .load_op  = SDL_GPU_LOADOP_CLEAR,
    .store_op = SDL_GPU_STOREOP_STORE,
  };

  SDL_GPURenderPass* pass = SDL_BeginGPURenderPass(cmd, &tgt, 1, NULL);
  SDL_BindGPUGraphicsPipeline(pass, ray.pipeline);
  SDL_GPUTextureSamplerBinding texs[1] = {
    { .texture = atlas_tex, .sampler = atlas_sampler },
  };

  SDL_BindGPUFragmentSamplers(pass, 0, texs, 1);
  SDL_DrawGPUPrimitives(pass, 3, 1, 0, 0);
  SDL_EndGPURenderPass(pass);
}

void Renderer::pass_post(SDL_GPUCommandBuffer* cmd, SDL_GPUTexture* swapchain, const CameraUBO& ubo) {
  SDL_PushGPUFragmentUniformData(cmd, 0, &ubo, sizeof(ubo));

  SDL_GPUColorTargetInfo tgt {
    .texture  = swapchain,
    .load_op  = SDL_GPU_LOADOP_CLEAR,
    .store_op = SDL_GPU_STOREOP_STORE,
  };

  SDL_GPURenderPass* pass = SDL_BeginGPURenderPass(cmd, &tgt, 1, NULL);
  SDL_BindGPUGraphicsPipeline(pass, post.pipeline);
  SDL_GPUTextureSamplerBinding tex { .texture = post_tex, .sampler = render_sampler };
  SDL_BindGPUFragmentSamplers(pass, 0, &tex, 1);
  SDL_DrawGPUPrimitives(pass, 3, 1, 0, 0);
  SDL_EndGPURenderPass(pass);
}

void Renderer::draw_imgui(SDL_GPUCommandBuffer* cmd, SDL_GPUTexture* tex) {
  ImGui_ImplSDLGPU3_NewFrame();
  ImGui_ImplSDL3_NewFrame();
  ImGui::NewFrame();

  ImGui::Begin("Kale Renderer");
  ImGui::Text("Fps: %f", fps);

  if(ImGui::RadioButton("Standard", (post_mode == 0)))
    post_mode = 0;
  if(ImGui::RadioButton("Black/White", (post_mode == 1)))
    post_mode = 1;
  if(ImGui::RadioButton("Dithered", (post_mode == 2)))
    post_mode = 2;


  if(ImGui::CollapsingHeader("Magnification")) {
    ImGui::SliderFloat("Zoom", &zoom, 0.1f, 100.0f);
    ImGui::SliderFloat("Radius", &radius, 0.1f, 1.0f);
  }
  
  ImGui::End();

  ImGui::Render();
  ImDrawData* draw_data = ImGui::GetDrawData();
  const bool is_minimized = (draw_data->DisplaySize.x <= 0.0f || draw_data->DisplaySize.y <= 0.0f);

  if(tex && !is_minimized) {
    ImGui_ImplSDLGPU3_PrepareDrawData(draw_data, cmd);

    SDL_GPUColorTargetInfo tgt_info = {
      .texture = tex,
      .mip_level = 0,
      .layer_or_depth_plane = 0,
      .load_op = SDL_GPU_LOADOP_LOAD,
      .store_op = SDL_GPU_STOREOP_STORE,
      .cycle = false
    };

    SDL_GPURenderPass* pass = SDL_BeginGPURenderPass(cmd, &tgt_info, 1, NULL);

    ImGui_ImplSDLGPU3_RenderDrawData(draw_data, cmd, pass);

    SDL_EndGPURenderPass(pass);
  }
}

void Renderer::draw(CameraUBO& ubo) {
  ubo.post_mode = post_mode;
  ubo.zoom = zoom;
  ubo.radius = radius;
  static std::array<float, 25> frame_times;

  static float last_time = (float)SDL_GetPerformanceCounter()/(float)SDL_GetPerformanceFrequency();

  float now = (float)SDL_GetPerformanceCounter()/(float)SDL_GetPerformanceFrequency();
  frame_times[frame % 25] = 1.0f/(25.0f*(now-last_time));
  last_time = now;

  if(frame > 25) {
    fps = 0.0f;
    for(const auto& t : frame_times) {
      fps += t;
    }
  }

  SDL_GPUCommandBuffer* cmd = SDL_AcquireGPUCommandBuffer(dev);

  SDL_GPUTexture* swapchain;
  uint32_t sw_w, sw_h;
  SDL_WaitAndAcquireGPUSwapchainTexture(cmd, win, &swapchain, &sw_w, &sw_h);

  if (!swapchain) {
    SDL_SubmitGPUCommandBuffer(cmd);
    return;
  }

  if (sw_w != render_w || sw_h != render_h)
    resize_render_texture(sw_w, sw_h);

  pass_ray(cmd, ubo);
  pass_post(cmd, swapchain, ubo);
  draw_imgui(cmd, swapchain);

  SDL_SubmitGPUCommandBuffer(cmd);

  frame++;
}
