#include <aux/compiler.h>
#include <aux/elf.h>
#include <desc.h>
#include <fs/vfs.h>
#include <id/id_alloc.h>
#include <irq/irq.h>
#include <libk/align.h>
#include <libk/list.h>
#include <libk/rbtree.h>
#include <libk/std.h>
#include <libk/string.h>
#include <limine/requests.h>
#include <mm/liballoc.h>
#include <mm/pmm.h>
#include <proc/capability.h>
#include <proc/proc.h>
#include <proc/procgroup.h>
#include <proc/reschedule.h>
#include <proc/resource.h>
#include <status.h>
#include <sync/spin_lock.h>
#include <sys/debug.h>
#include <sys/mm.h>
#include <sys/proc.h>
#include <sys/sched.h>
#include <sys/smp.h>
#include <sys/spin.h>

#if defined(__x86_64__)
#include <amd64/intr_defs.h>
#endif

#define PIDS_MAX 1024

static struct rb_node_link* proc_tree = NULL;
static spin_lock_t proc_tree_lock = SPIN_LOCK_INIT;

static struct id_alloc pid_alloc;

int proc_alloc_pid (void) { return id_alloc (&pid_alloc); }

void proc_free_pid (int pid) { id_free (&pid_alloc, pid); }

void proc_pid_alloc_init (void) { id_alloc_init (&pid_alloc, PIDS_MAX); }

static bool proc_check_elf (uint8_t* elf) {
  if (!((elf[0] == 0x7F) && (elf[1] == 'E') && (elf[2] == 'L') && (elf[3] == 'F')))
    return false;
  return true;
}

struct elf_aux proc_load_segments (struct proc* proc, uint8_t* elf) {
  struct elf_aux aux;

  Elf64_Ehdr* ehdr = (Elf64_Ehdr*)elf;
  aux.entry = ehdr->e_entry;
  aux.phnum = ehdr->e_phnum;
  aux.phent = ehdr->e_phentsize;

  struct limine_hhdm_response* hhdm = limine_hhdm_request.response;

  for (uint64_t segment = 0; segment < ehdr->e_phnum; segment++) {
    Elf64_Phdr* phdr =
        (Elf64_Phdr*)((uintptr_t)elf + ehdr->e_phoff + (ehdr->e_phentsize * segment));

    switch (phdr->p_type) {
    case PT_PHDR: {
      aux.phdr = (uint64_t)phdr->p_vaddr;
    } break;
    case PT_LOAD: {
      uintptr_t v_addr = align_down (phdr->p_vaddr, PAGE_SIZE);
      uintptr_t off = phdr->p_vaddr - v_addr;

      size_t blks = div_align_up (phdr->p_memsz + off, PAGE_SIZE);

      uint32_t pg_flags = MM_PG_USER | MM_PG_PRESENT;
      if (phdr->p_flags & PF_W)
        pg_flags |= MM_PG_RW;

      uintptr_t p_addr;
      procgroup_map (proc->procgroup, v_addr, blks, pg_flags, &p_addr);

      memset ((void*)((uintptr_t)hhdm->offset + p_addr), 0, blks * PAGE_SIZE);
      memcpy ((void*)((uintptr_t)hhdm->offset + p_addr + off),
              (void*)((uintptr_t)elf + phdr->p_offset), phdr->p_filesz);
    } break;
    case PT_TLS: {
#if defined(__x86_64__)
      if (phdr->p_memsz > 0) {
        size_t tls_align = phdr->p_align ? phdr->p_align : sizeof (uintptr_t);
        size_t tls_size = align_up (phdr->p_memsz, tls_align);
        size_t tls_total_needed = tls_size + sizeof (uintptr_t);
        size_t blks = div_align_up (tls_total_needed, PAGE_SIZE);
        proc->procgroup->tls.tls_tmpl_pages = blks;
        proc->procgroup->tls.tls_tmpl_size = tls_size;
        proc->procgroup->tls.tls_tmpl_total_size = tls_total_needed;

        proc->procgroup->tls.tls_tmpl = malloc (blks * PAGE_SIZE);
        memset (proc->procgroup->tls.tls_tmpl, 0, blks * PAGE_SIZE);

        memcpy (proc->procgroup->tls.tls_tmpl, (void*)((uintptr_t)elf + phdr->p_offset),
                phdr->p_filesz);

        proc_init_tls (proc);
      }
#endif
    } break;
    }
  }

  return aux;
}

struct proc* proc_from_file (struct proc* proc1, const char* volume, const char* path,
                             struct reschedule_ctx* rctx) {
  struct desc desc;
  int ret;

  for (;;) {
    ret = vfs_volume_open (proc1, volume, rctx);

    if (ret < 0) {
      if (ret == -ST_TRY_AGAIN)
        continue;
      else
        return NULL;
    } else
      break;
  }

  if ((ret = vfs_describe (proc1, volume, path, &desc)) < 0) {
    vfs_volume_close (proc1, volume, rctx);
    return NULL;
  }

  if (desc.type != FS_FILE) {
    vfs_volume_close (proc1, volume, rctx);
    return NULL;
  }

  uint8_t* temp_buffer = malloc (desc.size);

  if (temp_buffer == NULL) {
    vfs_volume_close (proc1, volume, rctx);
    return NULL;
  }

  if ((ret = vfs_read_file (proc1, volume, path, temp_buffer, 0, desc.size)) < 0) {
    free (temp_buffer);
    vfs_volume_close (proc1, volume, rctx);
    return NULL;
  }

  vfs_volume_close (proc1, volume, rctx);

  if (!proc_check_elf (temp_buffer)) {
    free (temp_buffer);
    return NULL;
  }

  struct proc* proc = proc_from_elf (temp_buffer);

  free (temp_buffer);
  return proc;
}

struct proc* proc_find_pid (int pid) {
  struct proc* proc = NULL;

  spin_lock (&proc_tree_lock);
  rbtree_find (struct proc, &proc_tree, pid, proc, proc_tree_link, pid);
  spin_unlock (&proc_tree_lock);

  return proc;
}

void proc_register (struct proc* proc, struct cpu* register_cpu, struct reschedule_ctx* rctx) {
  struct cpu* cpu = register_cpu != NULL ? register_cpu : cpu_find_lightest ();

  spin_lock (&proc_tree_lock);
  spin_lock (&cpu->lock);
  spin_lock (&proc->lock);

  proc->cpu = cpu;

  rbtree_insert (struct proc, &proc_tree, &proc->proc_tree_link, proc_tree_link, pid);

  cpu->proc_run_q_count++;
  list_append (cpu->proc_run_q, &proc->cpu_run_q_link);
  if (cpu->proc_current == NULL)
    cpu->proc_current = proc;

  spin_unlock (&proc->lock);
  spin_unlock (&cpu->lock);
  spin_unlock (&proc_tree_lock);

  rctx_insert_cpu (rctx, cpu);
}

/* caller holds cpu->lock */
static struct proc* proc_find_sched (struct cpu* cpu) {
  if (!cpu->proc_run_q)
    return NULL;

  struct list_node_link *current, *start;

  if (cpu->proc_current)
    current = cpu->proc_current->cpu_run_q_link.next;
  else
    current = cpu->proc_run_q;

  if (!current)
    current = cpu->proc_run_q;

  start = current;

  do {
    struct proc* proc = list_entry (current, struct proc, cpu_run_q_link);

    spin_lock (&proc->lock);

    int state = proc->state;

    if (state == PROC_READY) {
      spin_unlock (&proc->lock);
      return proc;
    }

    spin_unlock (&proc->lock);

    current = current->next ? current->next : cpu->proc_run_q;
  } while (current != start);

  return NULL;
}

void proc_sched (void) {
  struct proc* next = NULL;
  struct cpu* cpu = thiscpu;

  spin_lock (&cpu->lock);

  next = proc_find_sched (cpu);

  if (next) {
    cpu->proc_current = next;

    do_sched (next, &cpu->lock);
  } else {
    cpu->proc_current = NULL;
    spin_unlock (&cpu->lock);

    spin ();
  }
}

void proc_kill (struct proc* proc, struct reschedule_ctx* rctx) {
  spin_lock (&proc->lock);
  struct cpu* cpu = proc->cpu;
  spin_unlock (&proc->lock);

  spin_lock (&cpu->lock);
  spin_lock (&proc->lock);

  proc->state = PROC_DEAD;
  proc->cpu = NULL;

  list_remove (cpu->proc_run_q, &proc->cpu_run_q_link);
  cpu->proc_run_q_count--;
  if (cpu->proc_current == proc)
    cpu->proc_current = NULL;

  spin_unlock (&proc->lock);
  spin_unlock (&cpu->lock);

  spin_lock (&proc_tree_lock);
  spin_lock (&proc->lock);

  rbtree_delete (&proc_tree, &proc->proc_tree_link);

  spin_unlock (&proc->lock);
  spin_unlock (&proc_tree_lock);

  proc_cleanup (proc, rctx);

  rctx_insert_cpu (rctx, cpu);

  DEBUG ("killed PID %d\n", proc->pid);
}

void proc_wait_for (struct proc* proc, struct reschedule_ctx* rctx, struct proc* wait_proc) {
  proc_sq_suspend (proc, &wait_proc->done_sq, NULL, rctx);
}

static void proc_irq_sched (void* arg, void* regs, struct reschedule_ctx* rctx) {
  (void)arg, (void)regs, (void)rctx;

#if defined(__x86_64__)
  struct saved_regs* sr = regs;
  if (sr->cs != (GDT_UCODE | 0x3))
    return;
#endif

  proc_sched ();
}

void proc_init (void) {
#if defined(__x86_64__)
  irq_attach (&proc_irq_sched, NULL, SCHED_PREEMPT_TIMER);
  irq_attach (&proc_irq_sched, NULL, CPU_REQUEST_SCHED);
#endif

  struct reschedule_ctx rctx;
  memset (&rctx, 0, sizeof (rctx));

  struct proc* spin_proc = proc_from_file (VFS_KERNEL, "RD", "/spin", &rctx);
  proc_register (spin_proc, thiscpu, &rctx);

  struct proc* init = proc_from_file (VFS_KERNEL, "RD", "/init", &rctx);
  init->procgroup->capabilities |= (PROC_CAP_TERMINAL | PROC_CAP_KB);
  proc_register (init, thiscpu, &rctx);

  spin_lock (&spin_proc->cpu->lock);
  do_sched (spin_proc, &spin_proc->cpu->lock);
}
