/* **********************************************************************************************************************
   Included Header Files
   **********************************************************************************************************************/

#include "VDTile.h"



/* **********************************************************************************************************************
   Constructor
   **********************************************************************************************************************/

VDTile::VDTile() {
    status = 0;
    errno_t returnValue = strcpy_s(imageFileLocation, "");
    if (returnValue != 0) {
        VDError("Failed to set the image file location of a tile.", __FILE__, __LINE__);
    };
    returnValue = strcpy_s(newAreaName, "");
    if (returnValue != 0) {
        VDError("Failed to set the new area name of a tile.", __FILE__, __LINE__);
    };
    newAreaStartingPointX = 0;
    newAreaStartingPointY = 0;
    //spawns.clear();
};



/* **********************************************************************************************************************
   Destructor
   **********************************************************************************************************************/

VDTile::~VDTile() {
};



/* **********************************************************************************************************************
   WriteTile
   ---------
   Saves a tile to hard disk.
   #param  anOpenFile  Handle to a file that's already open.
   **********************************************************************************************************************/

void VDTile::WriteTile(HANDLE anOpenFile) {
    BOOL returnValue = FALSE;
    DWORD numberOfSpawns = 0;
    DWORD numberOfBytesWritten = 0;

    // write the status
    returnValue = WriteFile(anOpenFile, &status, sizeof(status), &numberOfBytesWritten, NULL);
    if (returnValue == FALSE) {
        VDError("Failed to write tile data to file.", __FILE__, __LINE__);
    };

    // write the image file location
    returnValue = WriteFile(anOpenFile, &imageFileLocation, sizeof(imageFileLocation), &numberOfBytesWritten, NULL);
    if (returnValue == FALSE) {
        VDError("Failed to write tile data to file.", __FILE__, __LINE__);
    };

    // write the new area name
    returnValue = WriteFile(anOpenFile, &newAreaName, sizeof(newAreaName), &numberOfBytesWritten, NULL);
    if (returnValue == FALSE) {
        VDError("Failed to write tile data to file.", __FILE__, __LINE__);
    };

    // write the new area starting point X location
    returnValue = WriteFile(anOpenFile, &newAreaStartingPointX, sizeof(newAreaStartingPointX), &numberOfBytesWritten, NULL);
    if (returnValue == FALSE) {
        VDError("Failed to write tile data to file.", __FILE__, __LINE__);
    };

    // write the new area starting point Y location
    returnValue = WriteFile(anOpenFile, &newAreaStartingPointY, sizeof(newAreaStartingPointY), &numberOfBytesWritten, NULL);
    if (returnValue == FALSE) {
        VDError("Failed to write tile data to file.", __FILE__, __LINE__);
    };

    // TODO: write spawn data
};



/* **********************************************************************************************************************
   ReadTile
   ---------
   Loads a tile from hard disk.
   #param  anOpenFile  Handle to a file that's already open.
   **********************************************************************************************************************/

void VDTile::ReadTile(HANDLE anOpenFile) {
    BOOL returnValue = FALSE;
    DWORD numberOfSpawns = 0;
    DWORD numberOfBytesRead = 0;

    // load the status
    returnValue = ReadFile(anOpenFile, &status, sizeof(status), &numberOfBytesRead, NULL);
    if (returnValue == FALSE) {
        VDError("Failed to read the tile data.", __FILE__, __LINE__);
    };

    // write the image file location
    returnValue = ReadFile(anOpenFile, &imageFileLocation, sizeof(imageFileLocation), &numberOfBytesRead, NULL);
    if (returnValue == FALSE) {
        VDError("Failed to read the tile data.", __FILE__, __LINE__);
    };

    // write the new area name
    returnValue = ReadFile(anOpenFile, &newAreaName, sizeof(newAreaName), &numberOfBytesRead, NULL);
    if (returnValue == FALSE) {
        VDError("Failed to read the tile data.", __FILE__, __LINE__);
    };

    // write the new area starting point X location
    returnValue = ReadFile(anOpenFile, &newAreaStartingPointX, sizeof(newAreaStartingPointX), &numberOfBytesRead, NULL);
    if (returnValue == FALSE) {
        VDError("Failed to read the tile data.", __FILE__, __LINE__);
    };

    // write the new area starting point Y location
    returnValue = ReadFile(anOpenFile, &newAreaStartingPointY, sizeof(newAreaStartingPointY), &numberOfBytesRead, NULL);
    if (returnValue == FALSE) {
        VDError("Failed to read the tile data.", __FILE__, __LINE__);
    };

    // TODO: read spawn data
};



/* **********************************************************************************************************************
   Accessors and Modifiers
   **********************************************************************************************************************/

int VDTile::GetStatus() {
    return status;
};

void VDTile::SetStatus(int theStatus) {
    status = theStatus;
};

LPCSTR VDTile::GetImageFileLocation() {
    return imageFileLocation;
};

void VDTile::SetImageFileLocation(LPCSTR theImageFileLocation) {
    if (strcpy_s(imageFileLocation, theImageFileLocation) != 0) {
        VDError("Failed to copy the image file location.", __FILE__, __LINE__);
    };
};

LPCSTR VDTile::GetNewAreaName() {
    return newAreaName;
};

void VDTile::SetNewAreaName(LPCSTR theNewAreaName) {
    if (strcpy_s(newAreaName, theNewAreaName) != 0) {
        VDError("Failed to copy the new area name.", __FILE__, __LINE__);
    };
};

int VDTile::GetNewAreaStartingPointX() {
    return newAreaStartingPointX;
};

int VDTile::GetNewAreaStartingPointY() {
    return newAreaStartingPointY;
};

void VDTile::SetNewAreaStartingPoint(int x, int y) {
    newAreaStartingPointX = x;
    newAreaStartingPointY = y;
};