#include <aux/compiler.h>
#include <aux/elf.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/proc.h>
#include <rd/rd.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

/*
 * Lock ordering:
 * 1. proc_tree_lock
 * 2. [cpu]->lock
 */

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

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;
}

bool proc_map (struct proc* proc, uintptr_t start_paddr, uintptr_t start_vaddr, size_t pages,
               uint32_t flags) {
  struct proc_mapping* mapping = malloc (sizeof (*mapping));

  if (mapping == NULL)
    return false;

  mapping->paddr = start_paddr;
  mapping->vaddr = start_vaddr;
  mapping->size = pages * PAGE_SIZE;

  flags &= ~MM_PD_LOCK; /* clear LOCK flag if present, because we lock manualy */

  spin_lock (&proc->pd.lock);

  list_append (proc->mappings, &mapping->proc_mappings_link);

  for (uintptr_t vpage = start_vaddr, ppage = start_paddr; vpage < start_vaddr + pages * PAGE_SIZE;
       vpage += PAGE_SIZE, ppage += PAGE_SIZE) {
    mm_map_page (&proc->pd, ppage, vpage, flags);
  }

  spin_unlock (&proc->pd.lock);

  return true;
}

bool proc_unmap (struct proc* proc, uintptr_t start_vaddr, size_t pages) {
  size_t unmap_size = pages * PAGE_SIZE;
  uintptr_t end_vaddr = start_vaddr + unmap_size;
  struct list_node_link *mapping_link, *mapping_link_tmp;
  bool used_tail_mapping = false;

  struct proc_mapping* tail_mapping = malloc (sizeof (*tail_mapping));
  if (tail_mapping == NULL)
    return false;

  spin_lock (&proc->pd.lock);

  list_foreach (proc->mappings, mapping_link, mapping_link_tmp) {
    struct proc_mapping* mapping =
        list_entry (mapping_link, struct proc_mapping, proc_mappings_link);

    uintptr_t m_end = mapping->vaddr + mapping->size;

    /* check overlap */
    if ((start_vaddr < m_end) && (end_vaddr > mapping->vaddr)) {
      /* split in the middle */
      if ((start_vaddr > mapping->vaddr) && (end_vaddr < m_end)) {
        tail_mapping->vaddr = end_vaddr;
        tail_mapping->paddr = mapping->paddr + (end_vaddr - mapping->vaddr);
        tail_mapping->size = m_end - end_vaddr;

        mapping->size = start_vaddr - mapping->vaddr;

        list_insert_after (proc->mappings, &mapping->proc_mappings_link,
                           &tail_mapping->proc_mappings_link);

        used_tail_mapping = true;

        break;
      } else if ((start_vaddr <= mapping->vaddr) && (end_vaddr < m_end)) { /* shrink left */
        size_t diff = end_vaddr - mapping->vaddr;
        mapping->vaddr += diff;
        mapping->paddr += diff;
        mapping->size -= diff;
      } else if ((start_vaddr > mapping->vaddr) && (end_vaddr >= m_end)) { /* shrink right */
        mapping->size = start_vaddr - mapping->vaddr;
      } else { /* full overlap */
        list_remove (proc->mappings, &mapping->proc_mappings_link);
        free (mapping);
      }
    }
  }

  if (!used_tail_mapping)
    free (tail_mapping);

  for (uintptr_t vpage = start_vaddr; vpage < end_vaddr; vpage += PAGE_SIZE) {
    mm_unmap_page (&proc->pd, vpage, 0);
  }

  spin_unlock (&proc->pd.lock);

  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);

      uintptr_t p_addr = pmm_alloc (blks);
      if (p_addr == PMM_ALLOC_ERR)
        DEBUG ("pmm oom error while loading ELF segments! (tried to alloc %zu blks)\n", blks);

      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);

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

      proc_map (proc, p_addr, v_addr, blks, pg_flags);
    } break;
    }
  }

  return aux;
}

static struct proc* proc_spawn_rd (char* name) {
  struct rd_file* rd_file = rd_get_file (name);

  bool ok = proc_check_elf (rd_file->content);
  DEBUG ("ELF magic %s\n", (ok ? "OK" : "BAD"));

  if (!ok)
    return NULL;

  return proc_from_elf (rd_file->content);
}

static void proc_register (struct proc* proc, struct cpu* cpu) {
  proc->cpu = cpu;

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

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

  if (cpu->proc_current == NULL)
    cpu->proc_current = proc;

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

static struct proc* proc_find_sched (void) {
  struct rb_node_link* node = NULL;
  struct proc* start = thiscpu->proc_current;
  struct proc* proc = NULL;

  if (start)
    node = &start->cpu_run_q_link;

  if (!node)
    rbtree_first (&thiscpu->proc_run_q, node);

  struct rb_node_link* first = node;
  while (node) {
    proc = rbtree_entry (node, struct proc, cpu_run_q_link);

    if (atomic_load (&proc->state) == PROC_READY)
      return proc;

    rbtree_next (node, node);

    if (!node) {
      rbtree_first (&thiscpu->proc_run_q, node);
    }

    if (node == first)
      break;
  }

  return NULL;
}

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

  spin_lock (&thiscpu->lock);

  if (thiscpu->proc_run_q == NULL) {
    spin_unlock (&thiscpu->lock);
    goto idle;
  }

  next = proc_find_sched ();

  if (next != NULL)
    thiscpu->proc_current = next;

  spin_unlock (&thiscpu->lock);

  if (next != NULL && atomic_load (&next->state) == PROC_READY)
    do_sched (next);

idle:
  spin ();
}

void proc_kill (struct proc* proc) {
  atomic_store (&proc->state, PROC_DEAD);

  spin_lock (&proc_tree_lock);
  rbtree_delete (&proc_tree, &proc->proc_tree_link);
  spin_unlock (&proc_tree_lock);

  struct cpu* cpu = proc->cpu;
  spin_lock (&cpu->lock);
  rbtree_delete (&cpu->proc_run_q, &proc->cpu_run_q_link);
  spin_unlock (&cpu->lock);

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

  proc_cleanup (proc);

  if (cpu == thiscpu)
    proc_sched ();
  else
    cpu_request_sched (cpu);
}

static void proc_irq_sched (void* arg, void* regs) {
  (void)arg, (void)regs;
  proc_sched ();
}

static void proc_irq_cpu_request_sched (void* arg, void* regs) {
  (void)arg, (void)regs;
  proc_sched ();
}

void proc_init (void) {
  struct proc* init = proc_spawn_rd ("init.exe");
  proc_register (init, thiscpu);

#if defined(__x86_64__)
  irq_attach (&proc_irq_sched, NULL, SCHED_PREEMPT_TIMER, IRQ_INTERRUPT_SAFE);
  irq_attach (&proc_irq_cpu_request_sched, NULL, CPU_REQUEST_SCHED, IRQ_INTERRUPT_SAFE);
#endif

  do_sched (init);
}
