#include <stdint.h>
#include <stddef.h>
#include "syscall.h"
#include "sysdefs/fs.h"
#include "errors.h"
#include "path/path.h"
#include "vfs/vfs.h"
#include "kprintf.h"
#include "proc/proc.h"
#include "spinlock/spinlock.h"
#include "util/util.h"

#define _MP_MAX 0xff
#define _PATH_MAX VFS_PATH_MAX

int32_t SYSCALL2(sys_fs_openf, opath1, oflags1) {
  int32_t ret = E_BADIO;

  const char *opath = (const char *)opath1;
  uint64_t oflags = oflags1;
  
  if (opath == NULL) {
    ret = E_INVALIDARGUMENT;
    goto done;
  }

  char mp[_MP_MAX];
  char path[_PATH_MAX];

  path_parse(opath, mp, path);

  VfsObj *vobj = vfs_open(mp, path, oflags);
  if (vobj == NULL) {
    ret = E_NOENTRY;
    goto done;
  }

  spinlock_acquire(&PROCS.spinlock);
  Proc *proc = NULL;
  LL_FINDPROP(PROCS.procs, proc, pid, _caller_pid);
  spinlock_release(&PROCS.spinlock);

  for (size_t i = 0; i < PROC_VFSHANDLES_MAX; i++) {
    if (proc->vobjs[i] == NULL) {
      proc->vobjs[i] = vobj;
      ret = i;
      goto done;
    }
  }

  ret = E_NOMEMORY;

done:
  return ret;
}

int32_t SYSCALL1(sys_fs_closef, fsh1) {
  uint64_t fsh = fsh1;
  int32_t ret = E_BADIO;
      
  if (fsh >= PROC_VFSHANDLES_MAX) {
    ret = E_INVALIDARGUMENT;
    goto done;
  }

  spinlock_acquire(&PROCS.spinlock);
  Proc *proc = NULL;
  LL_FINDPROP(PROCS.procs, proc, pid, _caller_pid);
  spinlock_release(&PROCS.spinlock);

  VfsObj *vobj = proc->vobjs[fsh];

  if (vobj == NULL) {
    ret = E_INVALIDARGUMENT;
    goto done;
  }

  vfs_close(vobj);
  proc->vobjs[fsh] = NULL;
  ret = E_OK;

done:
  return ret;
}

int32_t SYSCALL4(sys_fs_write, fsh1, buffer1, len1, off1) {
  uint64_t fsh = fsh1;
  int32_t ret = E_BADIO;
      
  const uint8_t *const buffer = (const uint8_t *const)buffer1;
  size_t len = (size_t)len1;
  size_t off = (size_t)off1;

  if (buffer == NULL) {
    ret = E_INVALIDARGUMENT;
    goto done;
  }

  if (fsh >= PROC_VFSHANDLES_MAX) {
    ret = E_INVALIDARGUMENT;
    goto done;
  }

  spinlock_acquire(&PROCS.spinlock);
  Proc *proc = NULL;
  LL_FINDPROP(PROCS.procs, proc, pid, _caller_pid);
  spinlock_release(&PROCS.spinlock);
  
  VfsObj *vobj = proc->vobjs[fsh];

  if (vobj == NULL) {
    ret = E_INVALIDARGUMENT;
    goto done;
  }

  ret = vobj->write(vobj, buffer, len, off);

done:
  return ret;
}

int32_t SYSCALL4(sys_fs_read, fsh1, buffer1, len1, off1) {
  uint64_t fsh = fsh1;
  int32_t ret = E_BADIO;
      
  uint8_t *const buffer = (uint8_t *const)buffer1;
  size_t len = (size_t)len1;
  size_t off = (size_t)off1;

  if (buffer == NULL) {
    ret = E_INVALIDARGUMENT;
    goto done;
  }

  if (fsh >= PROC_VFSHANDLES_MAX) {
    ret = E_INVALIDARGUMENT;
    goto done;
  }

  spinlock_acquire(&PROCS.spinlock);
  Proc *proc = NULL;
  LL_FINDPROP(PROCS.procs, proc, pid, _caller_pid);
  spinlock_release(&PROCS.spinlock);
  
  VfsObj *vobj = proc->vobjs[fsh];

  if (vobj == NULL) {
    ret = E_INVALIDARGUMENT;
    goto done;
  }

  ret = vobj->read(vobj, buffer, len, off);

done:
  return ret;
}

int32_t SYSCALL2(sys_fs_stat, opath1, fsstat1) {
  int32_t ret = E_BADIO;

  const char *opath = (const char *)opath1;
  
  if (opath == NULL) {
    ret = E_INVALIDARGUMENT;
    goto done;
  }
  
  FsStat *fsstat = (FsStat *)fsstat1;
  if (fsstat == NULL) {
    ret = E_INVALIDARGUMENT;
    goto done;
  }

  char mp[_MP_MAX];
  char path[_PATH_MAX];

  path_parse(opath, mp, path);

  ret = vfs_stat(mp, path, fsstat);

done:
  return ret;
}

int32_t SYSCALL3(sys_fs_fetchdirent, opath1, direntbuf1, idx1) {
  int32_t ret = E_BADIO;
  const char *opath = (const char *)opath1;
  
  if (opath == NULL) {
    ret = E_INVALIDARGUMENT;
    goto done;
  }

  FsDirent *direntbuf = (FsDirent *)direntbuf1;
  if (direntbuf == NULL) {
    ret = E_INVALIDARGUMENT;
    goto done;
  }

  size_t idx = (size_t)idx1;
  
  char mp[_MP_MAX];
  char path[_PATH_MAX];

  path_parse(opath, mp, path);

  ret = vfs_fetchdirent(mp, path, direntbuf, idx);

done:
  return ret;
}

int32_t SYSCALL1(sys_fs_mkdir, opath1) {
  int32_t ret = E_BADIO;
  const char *opath = (const char *)opath1;
  
  if (opath == NULL) {
    ret = E_INVALIDARGUMENT;
    goto done;
  }

  char mp[_MP_MAX];
  char path[_PATH_MAX];

  path_parse(opath, mp, path);

  ret = vfs_mkdir(mp, path);

done:
  return ret;
}

int32_t SYSCALL1(sys_fs_delete, opath1) {
  int32_t ret = E_BADIO;
  const char *opath = (const char *)opath1;
  
  if (opath == NULL) {
    ret = E_INVALIDARGUMENT;
    goto done;
  }

  char mp[_MP_MAX];
  char path[_PATH_MAX];

  path_parse(opath, mp, path);

  ret = vfs_delete(mp, path);

done:
  return ret;
}
