#include <id/id_alloc.h>
#include <libk/assert.h>
#include <libk/list.h>
#include <libk/rbtree.h>
#include <libk/std.h>
#include <libk/string.h>
#include <mm/liballoc.h>
#include <mm/pmm.h>
#include <proc/mail.h>
#include <proc/mutex.h>
#include <proc/proc.h>
#include <proc/procgroup.h>
#include <proc/reschedule.h>
#include <proc/resource.h>
#include <sync/spin_lock.h>
#include <sys/debug.h>

struct proc_resource* proc_find_resource (struct procgroup* procgroup, int rid) {
  struct proc_resource* resource = NULL;

  spin_lock (&procgroup->lock);
  rbtree_find (struct proc_resource, &procgroup->resource_tree, rid, resource, resource_tree_link,
               rid);
  spin_unlock (&procgroup->lock);

  return resource;
}

struct proc_resource* proc_create_resource_mutex (struct procgroup* procgroup) {
  struct proc_resource* resource;

  resource = malloc (sizeof (*resource));
  if (resource == NULL)
    return NULL;

  memset (resource, 0, sizeof (*resource));

  spin_lock (&procgroup->lock);

  resource->rid = id_alloc (&procgroup->rid_alloc);

  if (resource->rid < 0) {
    free (resource);
    spin_unlock (&procgroup->lock);
    return NULL;
  }

  resource->lock = SPIN_LOCK_INIT;
  resource->ops.cleanup = &proc_cleanup_resource_mutex;
  resource->u.mutex.resource = resource;
  resource->type = PR_MUTEX;

  rbtree_insert (struct proc_resource, &procgroup->resource_tree, &resource->resource_tree_link,
                 resource_tree_link, rid);

  spin_unlock (&procgroup->lock);

  return resource;
}

struct proc_resource* proc_create_resource_mail (struct procgroup* procgroup) {
  struct proc_resource* resource;

  resource = malloc (sizeof (*resource));
  if (resource == NULL)
    return NULL;

  memset (resource, 0, sizeof (*resource));

  spin_lock (&procgroup->lock);

  resource->rid = id_alloc (&procgroup->rid_alloc);

  if (resource->rid < 0) {
    free (resource);
    spin_unlock (&procgroup->lock);
    return NULL;
  }

  resource->lock = SPIN_LOCK_INIT;
  resource->ops.cleanup = &proc_cleanup_resource_mail;
  resource->u.mail.resource = resource;
  resource->type = PR_MAIL;

  if (!ringbuffer_init (&resource->u.mail.ringbuffer, PROC_MAIL_MAX, sizeof (struct mail_packet))) {
    id_free (&procgroup->rid_alloc, resource->rid);
    free (resource);
    spin_unlock (&procgroup->lock);
    return NULL;
  }

  rbtree_insert (struct proc_resource, &procgroup->resource_tree, &resource->resource_tree_link,
                 resource_tree_link, rid);

  spin_unlock (&procgroup->lock);

  return resource;
}

void proc_delete_resource (struct procgroup* procgroup, struct proc_resource* resource,
                           struct reschedule_ctx* rctx) {
  spin_lock (&procgroup->lock);
  spin_lock (&resource->lock);

  id_free (&procgroup->rid_alloc, resource->rid);
  rbtree_delete (&procgroup->resource_tree, &resource->resource_tree_link);

  spin_unlock (&resource->lock);
  spin_unlock (&procgroup->lock);

  resource->ops.cleanup (resource, rctx);
  free (resource);
}
