#include "player.h"
#include <math.h>
#include <stdlib.h>

void Player_construct(Player * self, int i, Color color)
{
    self->ID = i;
    self->nodes = malloc(sizeof(Node*));
    self->nodeCount = 0;
    self->color = color;
}

Player * Player_new()
{
    Player * player= malloc( sizeof(Player) );
    Player_construct(player, 0, BLACK);
    return player;
}

Player * Player_newArray(int size, Color color[])
{
    size= fmaxf(1, size);
    Player * p= malloc( sizeof(Player)*size );
    for(int i=0 ; i < size ; ++i )
    {
        Player_construct( &(p[i]), i, color[i]);
    }
    return p;
}

void Player_delete( Player * player )
{
    free( player );
}

void Player_add_Node( Player * self, Node * node )
{
    //Node * newNodes= Node_newArray(self->nodeCount + 1);
    Node ** newNodes =  malloc(sizeof(Node*)*(self->nodeCount + 1));
    for( int i = 0 ; i < self->nodeCount ; ++i )
    {
        newNodes[i] = self->nodes[i];
    }
    node->color = self->color;
    node->playerID = self->ID;
    newNodes[self->nodeCount] = node;
    self->nodes= newNodes;
    self->nodeCount += 1;
}

void Player_remove_Node( Player * self, Node * node )
{
    //Node * newNodes = Node_newArray(self->nodeCount - 1);
    Node ** newNodes =  malloc(sizeof(Node*)*(self->nodeCount - 1));
    for( int i = 0 ; i < self->nodeCount - 1 ; ++i )
    {
        if(self->nodes[i] != node){
            newNodes[i] = self->nodes[i];
        }
    }
    node->playerID = -1;
    self->nodes= newNodes;
    self->nodeCount -= 1;
}

void Player_start_turn(Player * self){
}

void Player_end_turn(Player * self){
}