/*
Copyright (c) 2013-2014 Gustav Hartvigsson

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/

#include <assert.h>
#include <stdio.h>
#include "LinkedList.h"



struct
SLinkedListNode {
  SLinkedListNode * prev;
  SLinkedListNode * next;
  spointer data;
};

struct
SLinkedList {
  size_t len; /**< The length of the list, may be wrong.*/
  FreeFunc free_func; /**< The function that frees the data. */
  SLinkedListNode * head; /**< head->prev must be NULL */
  SLinkedListNode * tail; /**< tail->next must be NULL */
  SLinkedListNode * current; /**< Represents the curret object*/
};

// At end of file.
void
_internal_s_liked_list_free_item (SLinkedList * list,
                                  spointer item,
                                  spointer user_data);

SLinkedList *
s_linked_list_new (FreeFunc free_func) {
  SLinkedList * self = s_malloc (sizeof (SLinkedList));
  self->len = 0;
  if (free_func == NULL) {
    self->free_func = FREEFUNC (s_free);
  } else {
    self->free_func = free_func;
  }
  self->head = NULL;
  self->tail = NULL;
  self->current = NULL;
  return self;
}


void
s_linked_list_free (SLinkedList * self, sboolean free_data) {
  //First we free the items if such is specified.
  s_dbg_print ("Freeing SLinkedList");
  if (free_data) {
    s_dbg_print ("Freeing Data in LinkedList");
    s_linked_list_for_each (self,
                            FOREACHFUNC (_internal_s_liked_list_free_item),
                            NULL);
  }
  /*if we have a current item we free the list, if not, we do nothing.
   */
  if (self->current) {
    /* Now the "snake" will "eat" its own tail. :-)
     * Based on this: http://stackoverflow.com/a/7025370
     */
    SLinkedListNode * tmp;
    SLinkedListNode * itm = self->head;
    do{
      tmp = itm;
      itm = itm->next;
      s_free (tmp);
    } while (itm != NULL);
    self->head = NULL;
  }
  s_free (self);
}

void
internal_s_linked_list_initialize_first_item (SLinkedList * self) {
  assert (self->len == 0);
  assert (self->head == NULL);
  assert (self->tail == NULL);
  assert (self->current == NULL);
  self->current = s_malloc (sizeof (SLinkedListNode));
  self->head = self->current;
  self->tail = self->current;
  self->head->prev = NULL;
  self->tail->next = NULL;
}

void
s_linked_list_add (SLinkedList * self, spointer data) {
  if (self->len  == 0) { // special case when the list is empty.
    internal_s_linked_list_initialize_first_item (self);
  } else { // general case.
    SLinkedListNode * old_current = self->current;
    SLinkedListNode * next = old_current->next;
    SLinkedListNode * new_current = s_malloc (sizeof (SLinkedListNode));

    new_current->next = next;
    new_current->prev = old_current;
    old_current->next = new_current;
    next->prev = new_current;

    self->current = new_current;
  }
  self->current->data = data;
  self->len++;
}

void
s_linked_list_append (SLinkedList * self, spointer data) {
  if (self->len  == 0) { // special case when the list is empty.
    internal_s_linked_list_initialize_first_item (self);
  } else { // general case.
    SLinkedListNode * old_tail = self->tail;

    self->tail->next       = s_malloc (sizeof (SLinkedListNode));
    self->tail->next->next = NULL;
    self->tail             = self->tail->next;
    self->tail->prev       = old_tail;
  }
  self->tail->data = data;
  self->len++;

  assert (self->tail->next == NULL);
  assert (self->tail != NULL);
  assert (self->current != NULL);
}

void
s_linked_list_prepend (SLinkedList * self, spointer data) {
  if (self->len  == 0) { // special case when the list is empty.
    internal_s_linked_list_initialize_first_item (self);
  } else { // general case.
    SLinkedListNode * old_head = self->head;

    self->head->prev       = s_malloc (sizeof (SLinkedListNode));
    self->head->prev->prev = NULL;
    self->head             = self->head->prev;
    self->head->next       = old_head;
  }
  self->head->data = data;
  self->len++;

  assert (self->head->prev == NULL);
  assert (self->head != NULL);
  assert (self->current != NULL);
}

sboolean
s_linked_list_next (SLinkedList * self) {
  if (self->current == NULL) {
    return FALSE;
  }
  if (self->current->next != NULL) {
    self->current = self->current->next;
    return TRUE;
  } else {
    assert (self->current == self->tail);
    return FALSE;
  }
}


sboolean
s_linked_list_prev (SLinkedList * self) {
  if (self->current == NULL) {
    return FALSE;
  }
  if (self->current->prev != NULL) {
    self->current = self->current->prev;
    return TRUE;
  } else {
    assert (self->current == self->head);
    return FALSE;
  }
}


spointer
s_linked_list_get_current (SLinkedList * self) {
  if (self->current != NULL) {
    return self->current->data;
  }
  return NULL;
}


spointer
s_linked_list_get_next (SLinkedList * self) {
  if (self->current->next) {
    return self->current->next->data;
  } else {
    assert (self->current == self->tail);
    return NULL;
  }
}


spointer
s_linked_list_get_prev (SLinkedList * self) {
    if (self->current->prev) {
    return self->current->next->data;
  } else {
    assert (self->current == self->head);
    return NULL;
  }
}


void
s_linked_list_head (SLinkedList * self) {
  self->current = self->head;
  /*if (self->len != 0) {
    assert (self->current != NULL);
  }*/
}


void
s_linked_list_tail (SLinkedList * self) {
  self->current = self->tail;
  /*if (self->len != 0) {
    assert (self->current != NULL);
  }*/
}


size_t
s_linked_list_len (SLinkedList * self) {
  return self->len;
}

void
s_linked_list_remove_current (SLinkedList * self, sboolean free_data) {
  SLinkedListNode * current = self->current;

  if (current == NULL) {
    assert (self->len == 0);
    s_err_print ("Trying to remove current when there is no items in the list.");
    return;
  }

  SLinkedListNode * prev = self->current->prev;
  SLinkedListNode * next = self->current->next;

  if (free_data) {
    FreeFunc free_func = self->free_func;
    spointer data = current->data;
    if (free_func != NULL) {
      free_func (data);
    } else {
      s_free (data);
    }
  }



  if (prev) {
    prev->next = next;
  }
  if (next) {
    next->prev = prev;
  }

  /*
   * Don't.. Just don't sak.. This is the best I could do...
   * Replace it if you can come up with a better way of doing this.
   */
  if (current == self->tail) {
    self->current = self->tail->prev;
    self->tail = self->current;
  }
  if (current == self->head) {
    self->current = self->head->next;
    self->head = self->current;
  }
  if (!(current == self->head) && !(current == self->tail)) {
    if (prev != NULL) {
      self->current = prev;
    } else if (next != NULL) {
      self->current = next;
    } else {
      self->current = NULL;
    }
  }

  s_free (current);
  current = NULL;


  self->len--;
}

void
s_linked_list_for_each (SLinkedList * self, ForEachFunc func,
                        spointer user_data) {

  SLinkedListNode * current_pos = self->current;

  s_linked_list_head (self);


  if(self->current != NULL) {
    do {
      s_dbg_print ("s_map_linked_list_for_each: doing loop.");
      spointer cur_itm = s_linked_list_get_current (self);
      func (self, cur_itm, user_data);
    } while (s_linked_list_next (self));
  }
  self->current = current_pos;
}

void
_internal_s_liked_list_free_item (SLinkedList * list,
                                  spointer item,
                                  spointer user_data) {
  s_dbg_print ("SLinkedList Free For Each");
  FreeFunc func = list->free_func;
  func (item);
}
