#include <desc.h>
#include <devices.h>
#include <fs/path.h>
#include <fs/tarfs.h>
#include <fs/vfs.h>
#include <libk/align.h>
#include <libk/minmax.h>
#include <libk/printf.h>
#include <libk/std.h>
#include <libk/string.h>
#include <limine/requests.h>
#include <mm/liballoc.h>
#include <path_defs.h>
#include <status.h>
#include <sys/debug.h>

static struct tar_file* tar_get_file (struct tarfs* tarfs, const char* filename) {
  for (size_t i = 0; i < TARFS_FILES_MAX; i++) {
    if ((tarfs->tarfs_files[i].header != NULL) &&
        (strncmp (tarfs->tarfs_files[i].header->filename, filename, PATH_MAX) == 0))
      return &tarfs->tarfs_files[i];
  }
  return NULL;
}

static size_t tar_get_size (uint8_t* in) {
  size_t size = 0;
  for (size_t i = 0; i < 11; i++) {
    if (in[i] < '0' || in[i] > '7')
      break;

    size = (size * 8) + (in[i] - '0');
  }

  return size;
}

static size_t tar_parse (struct tarfs* tarfs, uint8_t* addr, size_t max_size) {
  size_t i;
  uint8_t* ptr = addr;

  for (i = 0; i < TARFS_FILES_MAX; i++) {
    struct tar_header* hdr = (struct tar_header*)ptr;

    if (hdr->filename[0] == '\0')
      break;

    if ((size_t)(ptr - addr) + 512 > max_size)
      break;

    size_t size = tar_get_size (hdr->size);

    tarfs->tarfs_files[i].header = hdr;
    tarfs->tarfs_files[i].content = ptr + 512;
    tarfs->tarfs_files[i].size = size;

    ptr += 512 + ((size + 511) & ~511);

    if ((size_t)(ptr - addr) > max_size)
      break;
  }

  return i;
}

int tarfs_mount (struct vfs_volume* volume, bool format) {
  (void)format;

  struct tarfs* tarfs = malloc (sizeof (*tarfs));

  if (tarfs == NULL)
    return -ST_OOM_ERROR;

  memset (tarfs, 0, sizeof (*tarfs));

  volume->udata = tarfs;

  struct device* back_device = volume->back_device;
  size_t total_size, sector_size;
  int ret;

  spin_lock (&back_device->lock);

  ret = device_op (back_device, XDRV_GET_SIZE, NULL, NULL, &total_size);
  if (ret < 0) {
    spin_unlock (&back_device->lock);
    free (volume->udata);
    return ret;
  }

  ret = device_op (back_device, XDRV_GET_SECTOR_SIZE, NULL, NULL, &sector_size);
  if (ret < 0) {
    spin_unlock (&back_device->lock);
    free (volume->udata);
    return ret;
  }

  uint8_t* buffer = malloc (total_size);

  if (buffer == NULL) {
    spin_unlock (&back_device->lock);
    free (volume->udata);
    volume->udata = NULL;
    return ret;
  }

  size_t sector_count = 1;
  for (size_t sector = 0; sector < total_size / sector_size; sector++) {
    uint8_t* dest = (uint8_t*)((uintptr_t)buffer + (sector * sector_size));
    ret = device_op (back_device, XDRV_READ, NULL, NULL, &sector, &sector_count, dest);
  }

  spin_unlock (&back_device->lock);

  if (ret < 0) {
    free (buffer);
    free (volume->udata);
    volume->udata = NULL;
    return ret;
  }

  tarfs->buffer = buffer;

  tar_parse (tarfs, tarfs->buffer, total_size);

  return ret;
}

int tarfs_format (struct vfs_volume* volume) {
  (void)volume;
  return ST_OK;
}

int tarfs_describe (struct vfs_volume* volume, const char* path, struct desc* desc) {
  struct tarfs* tarfs = volume->udata;

  if ((path[0] == '/') && (path[1] == '\0')) {
    desc->size = 0;
    desc->type = FS_DIR;

    for (size_t i = 0; i < TARFS_FILES_MAX; i++) {
      if (tarfs->tarfs_files[i].header != NULL) {
        desc->size++;
      }
    }

    return ST_OK;
  } else {
    const char* filename = path_basename (path);

    if (filename == NULL)
      return -ST_BAD_PATH;

    struct tar_file* file = tar_get_file (tarfs, filename);

    if (file == NULL)
      return -ST_NOT_FOUND;

    desc->size = file->size;
    desc->type = FS_FILE;

    return ST_OK;
  }
}

int tarfs_read_file (struct vfs_volume* volume, const char* path, uint8_t* buffer, size_t off,
                     size_t size) {
  (void)volume;

  const char* filename = path_basename (path);

  if (filename == NULL)
    return -ST_BAD_PATH;

  struct tar_file* file = tar_get_file ((struct tarfs*)volume->udata, filename);

  if (file == NULL)
    return -ST_NOT_FOUND;

  if (off >= file->size)
    return -ST_OOB_ERROR;

  memcpy (buffer, (void*)((uintptr_t)file->content + off), min (size, file->size));

  return ST_OK;
}

int tarfs_read_dir_entry (struct vfs_volume* volume, const char* path, struct dir_entry* entry,
                          size_t entry_num) {
  struct tarfs* tarfs = volume->udata;

  if (strncmp (path, "/", PATH_MAX) != 0) {
    return -ST_NOT_DIR;
  }

  struct tar_file* tar_file = NULL;

  size_t entry_counter = 0;
  for (size_t i = 0; i < TARFS_FILES_MAX; i++) {
    if (tarfs->tarfs_files[i].header != NULL) {
      if (entry_num == entry_counter) {
        tar_file = &tarfs->tarfs_files[i];
        break;
      }
      entry_counter++;
    }
  }

  if (tar_file != NULL) {
    sprintf (entry->path, "/%s", tar_file->header->filename);
    return ST_OK;
  }

  return -ST_DIR_NO_ENTRIES;
}

int tarfs_write_file (struct vfs_volume* volume, const char* path, uint8_t* buffer, size_t off,
                      size_t size, uint32_t flags) {
  (void)volume, (void)path, (void)buffer, (void)off, (void)size, (void)flags;
  return ST_OK;
}

int tarfs_create_file (struct vfs_volume* volume, const char* path) {
  (void)volume, (void)path;
  return ST_OK;
}

int tarfs_create_dir (struct vfs_volume* volume, const char* path) {
  (void)volume, (void)path;
  return ST_OK;
}

int tarfs_remove (struct vfs_volume* volume, const char* path) {
  (void)volume, (void)path;
  return ST_OK;
}
