#include "stdafx.h"
#include "Process.h"

ProcessWrapper* ProcessWrapper::createWrapper(const Process* process)
{
  const Behavior* behavior;
  const TacticalAction* tacticalAction;
  if ((behavior = dynamic_cast<const Behavior*>(process)) != NULL)
    return new BehaviorWrapper(behavior);
  else if ((tacticalAction = dynamic_cast<const TacticalAction*>(process)) != NULL)
    return new TacticalActionWrapper(tacticalAction);
  else
    return new ProcessWrapper(process);
}

void ProcessWrapper::displayIn(QTreeWidgetItemProcess* item, bool actionsOnly, bool displayUnits) const
{
  item->setCustomData(m_id);
  item->setText(0, m_name);
  if (displayUnits && !m_unitsInvolved.empty())
  {
    QTreeWidgetItemProcess* unitsItem = new QTreeWidgetItemProcess(item, reinterpret_cast<const void*>(reinterpret_cast<unsigned int>(m_key) + 1));
    unitsItem->setCustomData(m_id);
    unitsItem->setText(0, "Units");
    for (std::set<UnitRecordWrapper>::const_iterator it = m_unitsInvolved.begin(); it != m_unitsInvolved.end(); ++it)
    {
      const UnitRecordWrapper& wrapper = *it;
      wrapper.display(unitsItem);
    }
  }

  displayInImpl(item, actionsOnly, displayUnits);

  item->setExpanded(true);
};

void ProcessWrapper::displayInImpl(QTreeWidgetItemProcess* item, bool actionsOnly, bool displayUnits) const
{
  for (std::set<ProcessWrapper*, CompareProcessWrapper>::const_iterator it = m_childProcesses.begin(); it != m_childProcesses.end(); ++it)
  {
    const ProcessWrapper* childProcess = *it;
    childProcess->display(item, actionsOnly, displayUnits);
  }
}

void BehaviorWrapper::displayInImpl(QTreeWidgetItemProcess* item, bool actionsOnly, bool displayUnits) const
{
  QBrush brush = item->foreground(0);
  brush.setColor(QColor(0, 0, 160));
  item->setForeground(0, brush);

  QFont font(item->font(0));
  font.setBold(true);
  item->setFont(0, font);

  for (std::list<BehaviorWrapper::BehaviorStateWrapper>::const_iterator itState = m_states.begin(); itState != m_states.end(); ++itState)
  {
    const BehaviorWrapper::BehaviorStateWrapper& s = *itState;
    QTreeWidgetItemProcess* state = new QTreeWidgetItemProcess(item, m_key);
    state->setCustomData(m_id);
    if (s.isCurrentState())
      ProcessWrapper::displayInImpl(state, actionsOnly, displayUnits);
    s.displayIn(state);
  }
}

void BehaviorWrapper::BehaviorStateWrapper::displayIn(QTreeWidgetItemKey* item) const
{
  item->setText(0, m_name);
  if (m_isCurrentState)
  {
    QFont font(item->font(0));
    font.setItalic(true);
    item->setFont(0, font);
  }
  item->setExpanded(true);
}

void TacticalActionWrapper::displayInImpl(QTreeWidgetItemProcess* item, bool actionsOnly, bool displayUnits) const
{
  QBrush brush = item->foreground(0);
  brush.setColor(QColor(200, 150, 0));
  item->setForeground(0, brush);

  QFont font(item->font(0));
  font.setBold(true);
  item->setFont(0, font);

  ProcessWrapper::displayInImpl(item, actionsOnly, displayUnits);
}