#include "gapbuffer.h"
#include "arena_alloc.h"
#include <arena.h>
#include <liballoc.h>
#include <stddef.h>
#include <string.h>

void gapbuffer_init (gb_malloc_func_t mallocfn, void* ctx, struct gapbuffer* gb, size_t capacity) {
  gb->buffer = mallocfn (ctx, capacity);

  if (gb->buffer == NULL)
    return;

  memset (gb->buffer, 0, capacity);
  gb->size = capacity;
  gb->gap_start = 0;
  gb->gap_end = capacity;
}

void gapbuffer_fini (gb_free_func_t freefn, void* ctx, struct gapbuffer* gb) {
  freefn (ctx, gb->buffer);
  memset (gb, 0, sizeof (*gb));
}

void gapbuffer_grow (gb_realloc_func_t reallocfn, void* ctx, struct gapbuffer* gb) {
  size_t old_size = gb->size;
  size_t new_size = gb->size * 2;

  char* new = reallocfn (ctx, gb->buffer, old_size, new_size);

  if (new == NULL)
    return;

  gb->buffer = new;

  size_t post_gap_size = old_size - gb->gap_end;
  size_t new_gap_end = new_size - post_gap_size;

  memmove (gb->buffer + new_gap_end, gb->buffer + gb->gap_end, post_gap_size);

  gb->gap_end = new_gap_end;
  gb->size = new_size;
}

void gapbuffer_move (struct gapbuffer* gb, size_t pos) {
  if (pos > (gb->size - (gb->gap_end - gb->gap_start)))
    pos = (gb->size - (gb->gap_end - gb->gap_start));

  while (gb->gap_start > pos) {
    gb->gap_start--;
    gb->gap_end--;
    gb->buffer[gb->gap_end] = gb->buffer[gb->gap_start];
  }

  while (gb->gap_start < pos) {
    gb->buffer[gb->gap_start] = gb->buffer[gb->gap_end];
    gb->gap_start++;
    gb->gap_end++;
  }
}

void gapbuffer_insert (gb_realloc_func_t reallocfn, void* ctx, struct gapbuffer* gb, char c) {
  if (gb->gap_start == gb->gap_end)
    gapbuffer_grow (reallocfn, ctx, gb);

  gb->buffer[gb->gap_start] = c;
  gb->gap_start++;
}

void gapbuffer_backspace (struct gapbuffer* gb) {
  if (gb->gap_start > 0)
    gb->gap_start--;
}

size_t gapbuffer_length (struct gapbuffer* gb) { return gb->size - (gb->gap_end - gb->gap_start); }

char* gapbuffer_string_at (gb_malloc_func_t mallocfn, void* ctx, struct gapbuffer* gb, size_t pos) {
  size_t total_size = gapbuffer_length (gb);

  if (pos >= total_size)
    return "";

  size_t tail_size = total_size - pos;

  char* res = mallocfn (ctx, tail_size + 1);

  if (res == NULL)
    return NULL;

  size_t written = 0;

  if (pos < gb->gap_start) {
    size_t copy = gb->gap_start - pos;

    if (copy > tail_size)
      copy = tail_size;

    memcpy (res, &gb->buffer[pos], copy);
    written += copy;
    pos += copy;
  }

  if (written < tail_size) {
    size_t gap_size = gb->gap_end - gb->gap_start;
    size_t phys_pos = pos + gap_size;

    size_t copy = tail_size - written;
    memcpy (&res[written], &gb->buffer[phys_pos], copy);
    written += copy;
  }

  res[written] = '\0';
  return res;
}

void gapbuffer_concat (gb_realloc_func_t reallocfn, void* ctx, struct gapbuffer* dest,
                       struct gapbuffer* src) {
  size_t src_len = gapbuffer_length (src);

  if (src_len == 0)
    return;

  while ((dest->gap_end - dest->gap_start) < src_len)
    gapbuffer_grow (reallocfn, ctx, dest);

  gapbuffer_move (dest, gapbuffer_length (dest));

  size_t part1_len = src->gap_start;
  memcpy (dest->buffer + dest->gap_start, src->buffer, part1_len);
  dest->gap_start += part1_len;

  size_t part2_len = src->size - src->gap_end;
  if (part2_len > 0) {
    memcpy (dest->buffer + dest->gap_start, src->buffer + src->gap_end, part2_len);
    dest->gap_start += part2_len;
  }
}
