/**
 * @file task.cpp
 * @brief Task component implementation
 * @author Mahmoud Elmohtady
 * @company Nabd solutions - ASF
 * @copyright Copyright (c) 2025
 */

#include "task.hpp"

Task::Task()
    : m_state(TaskState::CREATED)
    , m_priority(TaskPriority::NORMAL)
    , m_handle(nullptr)
{
}

Task::~Task()
{
    destroy();
}

bool Task::create(const TaskConfig& config, void (*taskFunction)(void*), void* arg)
{
    if (config.name == nullptr || taskFunction == nullptr)
    {
        return false;
    }

    if (config.stackSize < 2048) // Minimum stack size
    {
        return false;
    }

    m_priority = config.priority;
    m_state = TaskState::CREATED;
    // TODO: Implement actual task creation logic
    m_handle = (void*)1; // Placeholder

    return true;
}

bool Task::destroy()
{
    if (m_state == TaskState::DELETED)
    {
        return false;
    }

    // TODO: Implement actual task deletion logic
    m_state = TaskState::DELETED;
    m_handle = nullptr;

    return true;
}

bool Task::suspend()
{
    if (m_state != TaskState::RUNNING && m_state != TaskState::READY)
    {
        return false;
    }

    // TODO: Implement actual task suspension logic
    m_state = TaskState::SUSPENDED;

    return true;
}

bool Task::resume()
{
    if (m_state != TaskState::SUSPENDED)
    {
        return false;
    }

    // TODO: Implement actual task resumption logic
    m_state = TaskState::READY;

    return true;
}

TaskState Task::getState() const
{
    return m_state;
}

TaskPriority Task::getPriority() const
{
    return m_priority;
}

bool Task::setPriority(TaskPriority priority)
{
    m_priority = priority;
    // TODO: Implement actual priority change logic
    return true;
}

void Task::delay(uint32_t delayMs)
{
    // TODO: Implement actual delay logic
    (void)delayMs;
}

Task* Task::getCurrentTask()
{
    // TODO: Implement actual current task retrieval
    return nullptr;
}

