/**
 * @file 
 * File containing object module
 */
#include "object.h"

/* Start function definition of object module */


unsigned short int weight(char cellId)
{
    D5(printf("\nOBJECT.weight() - START - input %c (int %i)\n",
                                                cellId,(int)cellId  ));
  
    unsigned short int cellWeight = 1;
    
    if( ! isaccessible( cellId ) )
    {
        if(cellId == FREE) cellWeight=1;
        else
        if(cellId == START) cellWeight=1;
        else    
        if(cellId == DOOR) cellWeight=1;
        else
        if(cellId == CLOSE) cellWeight=0;
    }
  
    D5(printf("\nOBJECT.weight() - 'returns' %u- END\n",cellWeight));
  
    return cellWeight;
}


bool isaccessible(char cellId )
{
    D5(printf("\nOBJECT.isaccessible() - START - input %c (int %i)\n",
                                                      cellId,(int)cellId    ));
  
    bool accessAllow = false;
    if(cellId == FREE) accessAllow=true;
    else
    if(cellId == START) accessAllow=true;
    else
    if(cellId == DOOR) accessAllow=true;
    else
    if(cellId == CLOSE) accessAllow=false;
    
    D5(printf("\nOBJECT.isaccessible() - 'returns' %s- END\n",
                                 (accessAllow?"TRUE":"FALSE")   ));
  
    return accessAllow;
}


char *color( char cellId )
{
    D5(printf("\nOBJECT.color() - START - input %c (int %i)\n",
                                                cellId,(int)cellId  ));
    const char BLACK[] = "#000000";
    const char WHITE[] = "#FFFFFF";
    const char RED[]   = "#FF0000";
    const char GREEN[] = "#00FF00";

    const unsigned short int NUM_CHAR = 8;

    char *colorValue = (char *)malloc( sizeof(char) * NUM_CHAR );
    
    strcpy(colorValue,BLACK); // Default color is black

    if(cellId == FREE) strcpy(colorValue, WHITE); // White
    else
    if(cellId == START) strcpy(colorValue, RED); // Red
    else
    if(cellId == DOOR) strcpy(colorValue, GREEN); // Green
    else
    if(cellId == CLOSE) strcpy(colorValue, BLACK); // Black

    D5(printf("\nOBJECT.color() - 'returns' %s- END\n",colorValue));

    return colorValue;
}

char *colorVisited( unsigned short int cellId )
{
    D5(printf("\nOBJECT.colorVisited() - START - input %c (int %i)\n",
                                                      cellId,(int)cellId ));
    
    const char LIGHT_BLUE[]  = "#8BD0F8";
    const char BLUE[]        = "#4438D8";
    const char OPAQUE_BLUE[] = "#4371A0";

    const unsigned short int NUM_CHAR = 8;

    char *colorValue = (char *)malloc( sizeof(char) * NUM_CHAR );
    
    if( cellId == UNVISITED )   strcpy( colorValue, color(FREE) ); // as FREE
    else
    if( cellId == VISIT1 ) strcpy(colorValue, LIGHT_BLUE); // Light blue
    else
    if( cellId == VISIT2 ) strcpy(colorValue, BLUE); // Blue
    else
        strcpy(colorValue, OPAQUE_BLUE); // Value Unknown as opaque blue

    D5(printf("\nOBJECT.colorVisited() - 'returns' %s- END\n",colorValue));

    return colorValue;
}

void visitCell( unsigned int row,
                unsigned int col
              )
{
    D5(printf("\nOBJECT.visitCell() - START - input row=%u col=%u\n",row,col));

    //Check if the number of rows or columns is out of data. (fail silent error)
    if( (row>=schemaData.numRow) || (col>=schemaData.numCol) )
    {   
        D1(printf("\nError: number of rows or columns out of data\n"));
        D5(printf("\nOBJECT.visitCell() - error & END\n"));
        return;
    }

    if ( schemaData.visitedCellInfo[ row ][ col ] == UNVISITED )
        schemaData.visitedCellInfo[ row ][ col ] = VISIT1;
    else
    if ( schemaData.visitedCellInfo[ row ][ col ] == VISIT1 )
        schemaData.visitedCellInfo[ row ][ col ] = VISIT2;
    else
    if ( schemaData.visitedCellInfo[ row ][ col ] == VISIT2 )
        schemaData.visitedCellInfo[ row ][ col ] = VISIT1;
    else
        // Eventually first set
        schemaData.visitedCellInfo[ row ][ col ] = UNVISITED; 

    D5(printf("\nOBJECT.visitCell() - END\n"));
}

/* End function definition of object module */
