#include "FileSystemHandler.h"

#include "fsfw/tasks/TaskFactory.h"
#include "fsfw/memory/GenericFileSystemMessage.h"
#include "fsfw/ipc/QueueFactory.h"

FileSystemHandler::FileSystemHandler(object_id_t fileSystemHandler):
        SystemObject(fileSystemHandler) {
    mq = QueueFactory::instance()->createMessageQueue(50);
}

FileSystemHandler::~FileSystemHandler() {
    QueueFactory::instance()->deleteMessageQueue(mq);
}

ReturnValue_t FileSystemHandler::performOperation(uint8_t unsignedChar) {
    while(true) {
        CommandMessage filemsg;
        ReturnValue_t result = HasReturnvaluesIF::RETURN_OK;
        while(true) {
            result = mq->receiveMessage(&filemsg);
            if(result == MessageQueueIF::EMPTY) {
                break;
            }
            else if(result != HasReturnvaluesIF::RETURN_FAILED) {
                sif::warning << "FileSystemHandler::performOperation: Message reception failed!"
                        << std::endl;
                break;
            }
            Command_t command = filemsg.getCommand();
            switch(command) {
            case(GenericFileSystemMessage::CMD_CREATE_DIRECTORY): {
                break;
            }
            case(GenericFileSystemMessage::CMD_CREATE_FILE): {
                break;
            }
            }
        }

        // This task will have a low priority and will run permanently in the background
        // so we will just run in a permanent loop here and check file system
        // messages permanently
        TaskFactory::instance()->delayTask(1000);
    }
}

MessageQueueId_t FileSystemHandler::getCommandQueue() const {
    return mq->getId();
}

ReturnValue_t FileSystemHandler::appendToFile(const char *repositoryPath, const char *filename,
        const uint8_t *data, size_t size, uint16_t packetNumber, void *args) {
    return HasReturnvaluesIF::RETURN_OK;
}

ReturnValue_t FileSystemHandler::createFile(const char *repositoryPath, const char *filename,
        const uint8_t *data, size_t size, void *args) {
    return HasReturnvaluesIF::RETURN_OK;
}

ReturnValue_t FileSystemHandler::deleteFile(const char *repositoryPath, const char *filename,
        void *args) {
    return HasReturnvaluesIF::RETURN_OK;
}