#include "RenderLayerCommands.h"

namespace UsdLayerManager {

// ─── RenderLayerCreateCommand ─────────────────────────────────────────────

RenderLayerCreateCommand::RenderLayerCreateCommand(RenderLayerManager* mgr, std::string name)
    : m_mgr(mgr)
    , m_name(std::move(name))
    , m_description("Create Render Layer " + m_name)
{
}

void RenderLayerCreateCommand::Execute() {
    if (!m_mgr) return;
    m_layerId = m_mgr->CreateRenderLayer(m_name);
}

void RenderLayerCreateCommand::Undo() {
    if (!m_mgr || m_layerId.empty()) return;
    m_mgr->DeleteRenderLayer(m_layerId);
}

// ─── RenderLayerDeleteCommand ─────────────────────────────────────────────

RenderLayerDeleteCommand::RenderLayerDeleteCommand(RenderLayerManager* mgr, std::string layerId)
    : m_mgr(mgr)
    , m_layerId(std::move(layerId))
    , m_description("Delete Render Layer")
{
    if (!mgr) return;
    for (const auto& info : mgr->GetRenderLayers()) {
        if (!info.isDefault && info.layerIdentifier == m_layerId) {
            m_description = "Delete Render Layer " + info.name;
            break;
        }
    }
}

void RenderLayerDeleteCommand::Execute() {
    if (!m_mgr) return;
    m_mgr->DeleteRenderLayer(m_layerId);
}

void RenderLayerDeleteCommand::Undo() {
    if (!m_mgr || m_layerId.empty()) return;
    m_mgr->RestoreDeletedRenderLayer(m_layerId);
}

// ─── RenderLayerRenameCommand ─────────────────────────────────────────────

RenderLayerRenameCommand::RenderLayerRenameCommand(RenderLayerManager* mgr, std::string layerId,
                                                   std::string newName)
    : m_mgr(mgr)
    , m_layerId(std::move(layerId))
    , m_newName(std::move(newName))
    , m_description("Rename Render Layer")
{
    if (!mgr) return;
    for (const auto& info : mgr->GetRenderLayers()) {
        if (!info.isDefault && info.layerIdentifier == m_layerId) {
            m_oldName     = info.name;
            m_description = "Rename Render Layer " + info.name + " -> " + m_newName;
            break;
        }
    }
}

void RenderLayerRenameCommand::Execute() {
    if (!m_mgr) return;
    m_mgr->RenameRenderLayer(m_layerId, m_newName);
}

void RenderLayerRenameCommand::Undo() {
    if (!m_mgr || m_oldName.empty()) return;
    m_mgr->RenameRenderLayer(m_layerId, m_oldName);
}

// ─── RenderLayerActivateCommand ───────────────────────────────────────────

RenderLayerActivateCommand::RenderLayerActivateCommand(RenderLayerManager* mgr, std::string targetLayerId)
    : m_mgr(mgr)
    , m_targetLayerId(std::move(targetLayerId))
    , m_description(m_targetLayerId.empty() ? "Activate Render Layer: Default"
                                            : "Activate Render Layer")
{
    if (mgr) m_priorLayerId = mgr->GetActiveRenderLayerId();
}

void RenderLayerActivateCommand::Execute() {
    if (!m_mgr) return;
    if (m_targetLayerId.empty()) m_mgr->ActivateDefaultLayer();
    else                          m_mgr->ActivateRenderLayer(m_targetLayerId);
}

void RenderLayerActivateCommand::Undo() {
    if (!m_mgr) return;
    if (m_priorLayerId.empty()) m_mgr->ActivateDefaultLayer();
    else                         m_mgr->ActivateRenderLayer(m_priorLayerId);
}

} // namespace UsdLayerManager
