#include "commitlist.h"
#include "stdio.h"

T_CommitList_ptr CL_createlist(){
  return NULL;
};

T_CommitList_ptr CL_empty(T_CommitList_ptr list){
  while (list != NULL)
    list=CL_remove(list);
  return NULL;
};

T_CommitList_ptr CL_insertInOrder(T_CommitList_ptr list, /*T_Tid tid,*/ Commit_M data){
  T_CommitList_ptr newItem;
  T_CommitList_ptr current;
  T_CommitList_ptr previous;

  // create the new list element
  newItem = (T_CommitList_ptr) malloc(sizeof(struct T_CommitList));
  //newItem->item = tid;
  /* Copy Commit_M */
  newItem->data.type = data.type;
  newItem->data.tid = data.tid;
  newItem->data.mclientId = data.mclientId;
  strcpy(newItem->data.endpoint.name, data.endpoint.name);
  memcpy(newItem->data.writeset, data.writeset, data.writeset_length);
  newItem->data.writeset_length = data.writeset_length;

  // find the place to insert the transaction
  previous = NULL;
  current = list;
  while ((current != NULL) && isSmallerId(current->data.tid, data.tid)) {
    previous = current;
    current = current -> next;
  };
  // insert transaction
  newItem -> next = current;
  if (previous != NULL) {
    previous -> next = newItem;

  } else {
    list = newItem;
  }
  CL_num_elements(list);//TODO Quitar
  return list;
};

int CL_isIn(T_CommitList_ptr list, T_Tid tid) {
  T_CommitList_ptr current;
  T_CommitList_ptr previous;

  current = list;
  while ((current != NULL) && ! isEqualId(current->data.tid, tid)) {
    current = current -> next;
  };
  return current != NULL;
} //CL_isIn

T_CommitList_ptr CL_remove(T_CommitList_ptr list){
  T_CommitList_ptr aux;

  if (list == NULL)
    printf("Error trying to remove from empty list\n");
  aux = list;
  list = list -> next;
  free(aux);
  return list;
};

T_CommitList_ptr CL_removeByID(T_CommitList_ptr list, T_Tid tid)
{
  T_CommitList_ptr current;
  T_CommitList_ptr previous;

  previous = NULL;
  current = list;
  while ((current != NULL) && ! isEqualId(current->data.tid, tid)) {
    previous = current;
    current = current -> next;
  };
  if (current == NULL)
    printf("Error trying to remove an id that is not in the list\n");
  if (previous == NULL) {
    list = current -> next;
  }
  else {
    previous -> next = current -> next;
  };
  //free(current);
  return current;
};

T_CommitList_ptr CL_deleteByID(T_CommitList_ptr list, T_Tid tid)
{
  T_CommitList_ptr current;
  T_CommitList_ptr previous;

  previous = NULL;
  current = list;
  while ((current != NULL) && ! isEqualId(current->data.tid, tid)) {
    previous = current;
    current = current -> next;
  };
  if (current == NULL)
    printf("Error trying to remove an id that is not in the list\n");
  if (previous == NULL) {
    list = current -> next;
  }
  else {
    previous -> next = current -> next;
  };
  free(current);
  return list;
};

T_CommitList_ptr CL_getFirst(T_CommitList_ptr list){
  if (list == NULL) {
    printf("Error calling first on an empty list\n");
    exit(1);
  }
  return list;
};

T_Tid CL_getFirstID(T_CommitList_ptr list){
  if (list == NULL) {
    printf("Error calling first on an empty list\n");
    exit(1);
  }
  return list -> data.tid;
};

int CL_num_elements(T_CommitList_ptr list)
{
  T_CommitList_ptr current;
  int elements = 0;

  current = list;
  while (current != NULL) {
    current = current -> next;
    elements++;
  };

printf("Elements in commitlist are <%d>\n", elements);
  return elements;
};

/*************
T_Transaction* CL_getTransaction(T_CommitList_ptr list, T_Tid tid){
  T_CommitList_ptr current;
  current = list;

  while ((current != NULL) && ! isEqualId(current->item->transaction.tid, tid)) {
    current = current -> next;
  };
  if (current == NULL) {
    printf("Error the transaction %d is not in the list\n", tid);
    exit(1);
  } else {
    return current -> item;
  }
};
*************/
