#include "dowa.h"

// --- Arena --- //
Dowa_PArena Dowa_Arena_Initialize(size_t capacity)
{
  Dowa_PArena p_arena;
  p_arena = malloc(capacity);
  if (p_arena == NULL)
  {
    perror("malloc");
    return NULL;
  }
  p_arena->offset = 0;
  p_arena->capacity = capacity;
  return p_arena;
}

void *Dowa_Arena_Allocate(Dowa_PArena p_arena, size_t size)
{
  if (p_arena->offset + size > p_arena->capacity)
  {
    return NULL;
  }
  void *currnet_ptr = p_arena->buffer + p_arena->offset;
  p_arena->offset += size;
  return currnet_ptr;
}

void Dowa_Arena_Free(Dowa_PArena p_arena)
{
  if (p_arena) {
    free(p_arena->buffer);
    free(p_arena);
  }
}

// --- HashMap --- //
Dowa_PHashMap Dowa_HashMap_Create(size_t capacity)
{
  Dowa_PHashMap p_hash_map;
  p_hash_map = malloc(sizeof(Dowa_HashMap));
  if (p_hash_map == NULL)
  {
    return NULL;
  }
  p_hash_map->entries = calloc(capacity, sizeof *p_hash_map->entries);
  if (p_hash_map->entries == NULL)
  {
    free(p_hash_map);
    return NULL;
  }
  p_hash_map->capacity = capacity;
  p_hash_map->current_capacity = 0;
  return p_hash_map;
}

int32 Dowa_HashMap_GetPosition(Dowa_PHashMap p_hash_map, char *key)
{
  int32 hash_val = HASH_KEY_NUMBER;
  int32 c;
  while ((c = *key++))
  {
    hash_val = (hash_val << 5) + hash_val + c;
  }
  return hash_val % p_hash_map->capacity;
}

void Dowa_HashMap_PushValueWithType(Dowa_PHashMap p_hash_map, char *key, void *value, size_t value_size, Dowa_ValueType type)
{
  int idx = Dowa_HashMap_GetPosition(p_hash_map, key);
  Dowa_PHashEntry entry = p_hash_map->entries[idx];
  if (entry)
  {
    free(entry->key);
    free(entry->buffer);
  }
  else 
  {
    entry = malloc(sizeof(Dowa_HashEntry));
    if (entry == NULL)
    {
      perror("malloc");
      return;
    }
    p_hash_map->entries[idx] = entry;
  }
  entry->key = strdup(key);
  entry->buffer = malloc(value_size);
  if (entry->buffer == NULL)
  {
    perror("malloc");
    return;
  }
  entry->capacity = value_size;
  entry->type = type;
  memcpy(entry->buffer, value, value_size);
  p_hash_map->current_capacity++;
}

void Dowa_HashMap_PushValue(Dowa_PHashMap p_hash_map, char *key, void *value, size_t value_size)
{
  Dowa_HashMap_PushValueWithType(p_hash_map, key, value, value_size, DOWA_TYPE_BUFFER);
}

void Dowa_HashMap_PopKey(Dowa_PHashMap p_hash_map, char *key)
{
  int idx = Dowa_HashMap_GetPosition(p_hash_map, key);
  Dowa_PHashEntry entry = p_hash_map->entries[idx];
  if (entry)
  {
    free(entry->key);
    free(entry->buffer);
    free(entry);
  }
  p_hash_map->entries[idx] = NULL;
  if (p_hash_map->current_capacity > 0)
  {
    p_hash_map->current_capacity--;
  }
}

#include <stdio.h>

void Dowa_HashMap_Print(Dowa_PHashMap map)
{
  for (size_t i = 0; i < map->capacity; ++i)
  {
    Dowa_PHashEntry e = map->entries[i];
    if (!e) continue;
    printf("%s: ", e->key);
    switch (e->type)
    {
      case DOWA_TYPE_BUFFER:
      {
        unsigned char *p = e->buffer;
        for (size_t j = 0; j < e->capacity; ++j) {
          printf("%02x", p[j]);
        }
        printf("\n");
      }
      break;
      case DOWA_TYPE_STRING:
      {
        printf("%s\n", (char*)e->buffer);
      }
      break;
      case DOWA_TYPE_INT:
      {
        printf("%d\n", *(int32_t*)e->buffer);
      }
      break;
      default:
      {
        printf("<unknown type>\n");
      }
    }
  }
}



int Dowa_Cache_Folder(Dowa_PHashMap map, const char *folder_path)
{
  DIR *dir = opendir(folder_path);
  if (!dir) {
    perror("opendir");
    return -1;
  }

  struct dirent *entry;
  while ((entry = readdir(dir))) {
    // skip "." and ".."
    if (entry->d_name[0] == '.' &&
       (entry->d_name[1] == '\0' ||
      (entry->d_name[1] == '.' && entry->d_name[2] == '\0')))
      continue;

    char fullpath[PATH_MAX];
    snprintf(fullpath, sizeof fullpath, "%s/%s", folder_path, entry->d_name);

    struct stat st;
    if (stat(fullpath, &st) < 0 || !S_ISREG(st.st_mode))
      continue;  // skip non-files or errors

    size_t size = (size_t)st.st_size;
    FILE *f = fopen(fullpath, "rb");
    if (!f)
    {
      perror("fopen");
      continue;
    }

    void *buf = malloc(size);
    if (!buf)
    {
      perror("malloc");
      fclose(f);
      closedir(dir);
      return -1;
    }

    if (fread(buf, 1, size, f) != size)
    {
      perror("fread");
      free(buf);
      fclose(f);
      continue;
    }
    fclose(f);

    // key = filename (d_name), value = file contents
    Dowa_HashMap_PushValueWithType(map, entry->d_name, buf, size, DOWA_TYPE_STRING);

    free(buf);  // Dowa_HashMap_PushValue made its own copy
  }

  closedir(dir);
  return 0;
}
