#include <stddef.h>
#include <stdint.h>
#include "ipcpipe.h"
#include "ipc/pipe/pipe.h"
#include "dlmalloc/malloc.h"
#include "proc/proc.h"
#include "spinlock/spinlock.h"
#include "errors.h"
#include "util/util.h"
#include "kprintf.h"

int32_t SYSCALL4(sys_ipc_piperead, pid1, pipenum1, buffer1, len1) {
  uint64_t pid = pid1;
  uint64_t pipenum = pipenum1;
  int32_t ret = E_OK;
  
  if (pid == (uint64_t)-1) {
    pid = _caller_pid;
  }

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

  if (proc == NULL) {
    ret = E_INVALIDARGUMENT;
    goto done;
  }
  
  if (pipenum >= PROC_PIPEHANDLES_MAX) {
    ret = E_INVALIDARGUMENT;
    goto done;
  }

  uint8_t *const buffer = (uint8_t *const)buffer1;
  if (buffer == NULL) {
    ret = E_INVALIDARGUMENT;
    goto done;
  }

  spinlock_acquire(&proc->pipes_spinlock);
  IpcPipe *pipe = proc->pipes[pipenum];
  spinlock_release(&proc->pipes_spinlock);
  
  if (pipe == NULL) {
    ret = E_INVALIDARGUMENT;
    goto done;
  }

  ret = ipc_piperead(pipe, buffer, len1);
done:
  return ret;
}

int32_t SYSCALL4(sys_ipc_pipewrite, pid1, pipenum1, buffer1, len1) {
  uint64_t pid = pid1;
  uint64_t pipenum = pipenum1;
  int32_t ret = E_OK;
  
  if (pid == (uint64_t)-1) {
    pid = _caller_pid;
  }

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

  if (proc == NULL) {
    ret = E_INVALIDARGUMENT;
    goto done;
  }
      
  if (pipenum >= PROC_PIPEHANDLES_MAX) {
    ret = E_INVALIDARGUMENT;
    goto done;
  }

  const uint8_t *const buffer = (const uint8_t *const)buffer1;
  if (buffer == NULL) {
    ret = E_INVALIDARGUMENT;
    goto done;
  }

  spinlock_acquire(&proc->pipes_spinlock);
  IpcPipe *pipe = proc->pipes[pipenum];
  spinlock_release(&proc->pipes_spinlock);

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

  ret = ipc_pipewrite(pipe, buffer, len1);
done:
  return ret;
}

int32_t SYSCALL1(sys_ipc_pipemake, pipenum1) {
  uint64_t pipenum = pipenum1;
  int32_t ret = E_OK;
  
  spinlock_acquire(&PROCS.spinlock);
  Proc *proc = NULL;
  LL_FINDPROP(PROCS.procs, proc, pid, _caller_pid);
  spinlock_release(&PROCS.spinlock);

  if (pipenum >= PROC_PIPEHANDLES_MAX) {
    ret = E_NOMEMORY;
    goto done;
  }

  IpcPipe *pipe = dlmalloc(sizeof(*pipe));
  if (pipe == NULL) {
    ret = E_NOMEMORY;
    goto done;
  }

  if ((ret = ipc_pipeinit(pipe, proc->pid)) < 0) {
    ret = E_NOMEMORY;
    goto done;
  }

  spinlock_acquire(&proc->pipes_spinlock);
  if (proc->pipes[pipenum] != NULL) {
    spinlock_release(&proc->pipes_spinlock);
    ipc_pipefree(pipe);
    dlfree(pipe);
    ret = E_RESOURCEAVAIL;
    goto done;
  }
  proc->pipes[pipenum] = pipe;
  spinlock_release(&proc->pipes_spinlock);

  ret = E_OK;

done:
  return ret;
}

int32_t SYSCALL1(sys_ipc_pipedelete, pipenum1) {
  uint64_t pipenum = pipenum1;
  int32_t ret = E_OK;
  
  spinlock_acquire(&PROCS.spinlock);
  Proc *proc = NULL;
  LL_FINDPROP(PROCS.procs, proc, pid, _caller_pid);
  spinlock_release(&PROCS.spinlock);

  if (pipenum >= PROC_PIPEHANDLES_MAX) {
    ret = E_NOMEMORY;
    goto done;
  }

  spinlock_acquire(&proc->pipes_spinlock);
  if (proc->pipes[pipenum] != NULL && proc->pid == proc->pipes[pipenum]->ownerpid) {
    ipc_pipefree(proc->pipes[pipenum]);
    dlfree(proc->pipes[pipenum]);
  }
  proc->pipes[pipenum] = NULL;
  spinlock_release(&proc->pipes_spinlock);
  ret = E_OK;

done:
  return ret;
}

int32_t SYSCALL4(sys_ipc_pipeconnect, pid1, pipenum1, pid2, pipenum2) {
  uint64_t pid = pid1;
  uint64_t pipenum = pipenum1;
  int32_t ret = E_OK;
  
  if (pid == (uint64_t)-1) {
    pid = _caller_pid;
  }

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

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

  if (pipenum >= PROC_PIPEHANDLES_MAX) {
    ret = E_NOMEMORY;
    goto done;
  }

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

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

  if (pipenum2 >= PROC_PIPEHANDLES_MAX) {
    ret = E_NOMEMORY;
    goto done;
  }
  
  spinlock_acquire(&proc2->pipes_spinlock);
  spinlock_acquire(&proc->pipes_spinlock);

  if (proc->pipes[pipenum] != NULL && proc->pid == proc->pipes[pipenum]->ownerpid) {
    ipc_pipefree(proc->pipes[pipenum]);
    dlfree(proc->pipes[pipenum]);
  }
  proc->pipes[pipenum] = proc2->pipes[pipenum2];

  spinlock_release(&proc->pipes_spinlock);
  spinlock_release(&proc2->pipes_spinlock);

done:
  return ret;
}
