#include <aux/compiler.h>
#include <libk/std.h>
#include <limine/requests.h>
#include <m/syscall_defs.h>
#include <mm/pmm.h>
#include <proc/proc.h>
#include <sys/debug.h>
#include <sys/mm.h>
#include <syscall/syscall.h>

#define DEFINE_SYSCALL(name)                                                                       \
  int name (struct proc* proc, uintptr_t UNUSED a1, uintptr_t UNUSED a2, uintptr_t UNUSED a3,      \
            uintptr_t UNUSED a4, uintptr_t UNUSED a5, uintptr_t UNUSED a6)

/* int proc_quit (void) */
DEFINE_SYSCALL (sys_proc_quit) {
  proc_kill (proc);
  proc_sched ();
  return SR_OK;
}

/* int proc_test (void) */
DEFINE_SYSCALL (sys_proc_test) {
  DEBUG ("test syscall message!\n");
  return SR_OK;
}

/* int proc_map (uintptr_t paddr, uintptr_t vaddr, size_t pages, uint32_t flags) */
DEFINE_SYSCALL (sys_proc_map) {
  uintptr_t paddr = a1;
  uintptr_t vaddr = a2;
  size_t pages = (size_t)a3;
  uint32_t flags = (uint32_t)a4;

  if (vaddr % PAGE_SIZE != 0)
    return -SR_UNALIGNED;

  if (paddr % PAGE_SIZE != 0)
    return -SR_UNALIGNED;

  bool ok = proc_map (proc, paddr, vaddr, pages, flags);
  return ok ? SR_OK : -SR_OOM_ERROR;
}

/* int proc_unmap (uintptr_t vaddr, size_t pages) */
DEFINE_SYSCALL (sys_proc_unmap) {
  uintptr_t vaddr = a1;
  size_t pages = (size_t)a2;

  if (vaddr % PAGE_SIZE != 0)
    return -SR_UNALIGNED;

  bool ok = proc_unmap (proc, vaddr, pages);
  return ok ? SR_OK : -SR_OOM_ERROR;
}

/* int proc_create_resource_mem (size_t pages, int vis, uintptr_t* out_paddr) */
DEFINE_SYSCALL (sys_proc_create_resource_mem) {
  struct limine_hhdm_response* hhdm = limine_hhdm_request.response;

  size_t pages = (size_t)a1;
  int vis = (int)a2;
  uintptr_t* out_paddr_buf = (uintptr_t*)a3;

  spin_lock (&proc->pd.lock);

  uintptr_t out_paddr_buf_paddr = mm_v2p (&proc->pd, (uintptr_t)out_paddr_buf, 0);

  if (!mm_validate_buffer (&proc->pd, (uintptr_t)out_paddr_buf, sizeof (uintptr_t), 0)) {
    spin_unlock (&proc->pd.lock);
    return -SR_BAD_ADDRESS_SPACE;
  }

  spin_unlock (&proc->pd.lock);

  uintptr_t* out_paddr_buf_vaddr = (uintptr_t*)((uintptr_t)hhdm->offset + out_paddr_buf_paddr);

  int rid = atomic_fetch_add (&proc->rids, 1);
  struct proc_resource_mem_init mem_init = {.pages = pages};
  struct proc_resource* r = proc_create_resource (proc, rid, PR_MEM, vis, &mem_init);

  if (r != NULL) {
    *out_paddr_buf_vaddr = r->u.mem.paddr;
    return r->rid;
  } else {
    return -SR_OOM_ERROR;
  }
}

/* int proc_drop_resource (int rid) */
DEFINE_SYSCALL (sys_proc_drop_resource) {
  int rid = (int)a1;

  struct proc_resource* resource;
  spin_lock (&proc->lock);
  rbtree_find (struct proc_resource, &proc->resource_tree, rid, resource, proc_resource_tree_link,
               rid);
  spin_unlock (&proc->lock);

  if (resource == NULL)
    return -SR_NOT_FOUND;

  proc_drop_resource (proc, resource);

  return SR_OK;
}

static syscall_handler_func_t handler_table[] = {
    [SYS_PROC_QUIT] = &sys_proc_quit,
    [SYS_PROC_TEST] = &sys_proc_test,
    [SYS_PROC_MAP] = &sys_proc_map,
    [SYS_PROC_UNMAP] = &sys_proc_unmap,
    [SYS_PROC_CREATE_RESOURCE_MEM] = &sys_proc_create_resource_mem,
    [SYS_PROC_DROP_RESOURCE] = &sys_proc_drop_resource,
};

syscall_handler_func_t syscall_find_handler (int syscall_num) {
  if (!(syscall_num >= 0 &&
        syscall_num < (int)(sizeof (handler_table) / sizeof (handler_table[0])))) {
    return NULL;
  }

  return handler_table[syscall_num];
}
