#include <desc.h>
#include <devices.h>
#include <fs/fat1.h>
#include <fs/fatfs.h>
#include <fs/fatfs_ctx.h>
#include <fs/path.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 <mm/liballoc.h>
#include <path_defs.h>
#include <status.h>
#include <sys/debug.h>
#include <write_file.h>

static int fat1_diskio_read (struct fatfs_ctx* ctx, uint32_t sector, uint8_t* buffer,
                             uint32_t sector_count) {
  struct vfs_volume* volume = ctx->udata;
  struct device* back_device = volume->back_device;

  if (sector_count == 0)
    return 0;

  size_t sector_size;
  size_t phys_sector, phys_sector_count;
  int ret;

  spin_lock (&back_device->lock);

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

  vfs_translate (sector, sector_count, FAT_SECTOR_SIZE, sector_size, &phys_sector,
                 &phys_sector_count);

  ret = device_op (back_device, XDRV_READ, NULL, NULL, &phys_sector, &phys_sector_count, buffer);
  if (ret < 0) {
    spin_unlock (&back_device->lock);
    return 0;
  }

  spin_unlock (&back_device->lock);

  if (ret < 0)
    return 0;
  return 1;
}

static int fat1_diskio_write (struct fatfs_ctx* ctx, uint32_t sector, uint8_t* buffer,
                              uint32_t sector_count) {
  struct vfs_volume* volume = ctx->udata;
  struct device* back_device = volume->back_device;

  if (sector_count == 0)
    return 0;

  size_t sector_size;
  size_t phys_sector, phys_sector_count;
  int ret;

  spin_lock (&back_device->lock);

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

  vfs_translate (sector, sector_count, FAT_SECTOR_SIZE, sector_size, &phys_sector,
                 &phys_sector_count);

  ret = device_op (back_device, XDRV_WRITE, NULL, NULL, &phys_sector, &phys_sector_count, buffer);
  if (ret < 0) {
    spin_unlock (&back_device->lock);
    return 0;
  }

  spin_unlock (&back_device->lock);

  if (ret < 0)
    return 0;
  return 1;
}

int fatfs_mount (struct vfs_volume* volume, bool format) {
  struct fatfs_ctx* fatfs_ctx = malloc (sizeof (*fatfs_ctx));
  int r;

  if (fatfs_ctx == NULL)
    return -ST_OOM_ERROR;

  memset (fatfs_ctx, 0, sizeof (*fatfs_ctx));
  fatfs_ctx->udata = volume;
  volume->udata = fatfs_ctx;

  fl_init (fatfs_ctx);
  fatfs_ctx->_fs.disk_io.read_media = &fat1_diskio_read;
  fatfs_ctx->_fs.disk_io.write_media = &fat1_diskio_write;

  if (format) {
    r = volume->driver_ops.format (volume);
    if (r < 0)
      return -ST_FORMAT_ERROR;
  }

  r = fl_attach_media (fatfs_ctx, &fat1_diskio_read, &fat1_diskio_write);
  if (r < 0)
    return -ST_MOUNT_ERROR;

  return ST_OK;
}

int fatfs16_format (struct vfs_volume* volume) {
  struct fatfs_ctx* fatfs_ctx = volume->udata;
  struct device* back_device = volume->back_device;
  size_t total_size;

  spin_lock (&back_device->lock);
  device_op (back_device, XDRV_GET_SIZE, NULL, NULL, &total_size);
  spin_unlock (&back_device->lock);

  size_t sectors = div_align_up (total_size, FAT_SECTOR_SIZE);
  int r = fatfs_format_fat16 (fatfs_ctx, &fatfs_ctx->_fs, sectors, "mop3 fat16");
  return r < 0 ? -ST_FORMAT_ERROR : ST_OK;
}

int fatfs32_format (struct vfs_volume* volume) {
  struct fatfs_ctx* fatfs_ctx = volume->udata;
  struct device* back_device = volume->back_device;
  size_t total_size;

  spin_lock (&back_device->lock);
  device_op (back_device, XDRV_GET_SIZE, NULL, NULL, &total_size);
  spin_unlock (&back_device->lock);

  size_t sectors = div_align_up (total_size, FAT_SECTOR_SIZE);
  int r = fatfs_format_fat32 (fatfs_ctx, &fatfs_ctx->_fs, sectors, "mop3 fat32");
  return r < 0 ? -ST_FORMAT_ERROR : ST_OK;
}

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

  if (fl_is_dir (fatfs_ctx, path)) {
    FL_DIR dir;

    if (fl_opendir (fatfs_ctx, path, &dir) == NULL)
      return -ST_NOT_FOUND;

    desc->type = FS_DIR;
    desc->size = 0;

    fl_dirent dirent;
    while (fl_readdir (fatfs_ctx, &dir, &dirent) == 0)
      desc->size++;

    fl_closedir (fatfs_ctx, &dir);
  } else {
    FL_FILE* file = fl_fopen (fatfs_ctx, path, "r");

    if (file == NULL)
      return -ST_NOT_FOUND;

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

    fl_fclose (fatfs_ctx, file);
  }

  return ST_OK;
}

int fatfs_read_file (struct vfs_volume* volume, const char* path, uint8_t* buffer, size_t off,
                     size_t size) {
  struct fatfs_ctx* fatfs_ctx = volume->udata;

  if (fl_is_dir (fatfs_ctx, path))
    return -ST_NOT_FOUND;

  FL_FILE* file = fl_fopen (fatfs_ctx, path, "rb");

  if (file == NULL)
    return -ST_NOT_FOUND;

  fl_fseek (fatfs_ctx, file, off, SEEK_SET);
  fl_fread (fatfs_ctx, buffer, 1, size, file);

  fl_fclose (fatfs_ctx, file);

  return ST_OK;
}

int fatfs_write_file (struct vfs_volume* volume, const char* path, uint8_t* buffer, size_t off,
                      size_t size, uint32_t flags) {
  struct fatfs_ctx* fatfs_ctx = volume->udata;

  if (fl_is_dir (fatfs_ctx, path))
    return -ST_NOT_FOUND;

  FL_FILE* file;

  if ((flags & WF_APPEND))
    file = fl_fopen (fatfs_ctx, path, "rb+");
  else
    file = fl_fopen (fatfs_ctx, path, "wb");

  if (file == NULL)
    return -ST_NOT_FOUND;

  fl_fseek (fatfs_ctx, file, off, SEEK_SET);
  fl_fwrite (fatfs_ctx, buffer, 1, size, file);

  fl_fclose (fatfs_ctx, file);

  return ST_OK;
}

int fatfs_read_dir_entry (struct vfs_volume* volume, const char* path, struct dir_entry* entry,
                          size_t entry_num) {
  struct fatfs_ctx* fatfs_ctx = volume->udata;
  FL_DIR dir;

  if (!fl_is_dir (fatfs_ctx, path))
    return -ST_NOT_FOUND;

  if (fl_opendir (fatfs_ctx, path, &dir) == NULL)
    return -ST_NOT_FOUND;

  fl_dirent dirent;
  size_t dirent_num = 0;
  while (fl_readdir (fatfs_ctx, &dir, &dirent) == 0) {
    if (dirent_num == entry_num) {
      strncat (entry->path, path, PATH_MAX);
      strncat (entry->path, dirent.filename, PATH_MAX);
      break;
    }

    dirent_num++;
  }

  fl_closedir (fatfs_ctx, &dir);
  return ST_OK;
}

int fatfs_create_file (struct vfs_volume* volume, const char* path) {
  struct fatfs_ctx* fatfs_ctx = volume->udata;

  FL_FILE* file = fl_fopen (fatfs_ctx, path, "wb+");

  if (file == NULL)
    return -ST_NOT_FOUND;

  fl_fclose (fatfs_ctx, file);
  return ST_OK;
}

int fatfs_create_dir (struct vfs_volume* volume, const char* path) {
  struct fatfs_ctx* fatfs_ctx = volume->udata;

  int r = fl_createdirectory (fatfs_ctx, path);
  return r == 0 ? ST_OK : -ST_CREATE_DIR_ERROR;
}

int fatfs_remove (struct vfs_volume* volume, const char* path) {
  struct fatfs_ctx* fatfs_ctx = volume->udata;

  int r = fl_remove (fatfs_ctx, path);
  return r == 0 ? ST_OK : -ST_REMOVE_ERROR;
}
