#include "stdafx.h"
#include "ArbitratorUnitData.h"
#include "Commands/Command.h"

//ArbitratorUnitData::ArbitratorUnitData(BWAPI::Unit* unit)
//  : UnitData(unit)
//  , m_highestImportanceAction(&m_actions[0])
//  , m_possibleSuccessorAction(&m_actions[1])
//{
//}
//
//ArbitratorUnitData::~ArbitratorUnitData()
//{
//  for (size_t i = 0; i < sizeof(m_actions) / sizeof(*m_actions); ++i)
//  {
//    m_actions[i].clear();
//  }
//}
//
//void ArbitratorUnitData::addSubmittedAction(const Command &action, ActionImportance importance)
//{
//  
//  if (!m_possibleSuccessorAction->propose(action, importance))
//  {
//    SPAR_LOG(LogTypes.LAYER_1_DECISION, "Arbitration system discarded '%s' action in favor of '%s' action.", 
//                                         toString(action).c_str(),
//                                         toString(m_possibleSuccessorAction->getAction()).c_str());
//  }
//}
//
//const Command* ArbitratorUnitData::arbitrate()
//{
//  if (*m_possibleSuccessorAction > *m_highestImportanceAction)
//  {
//    std::swap(m_possibleSuccessorAction, m_highestImportanceAction);
//  }
//
//  m_possibleSuccessorAction->clear();
//
//  return m_highestImportanceAction->getAction(); // possibly NULL
//}
//
//bool ArbitratorUnitData::ActionData::propose(const Command& action, ActionImportance importance)
//{
//  bool change = false;
//  if (m_action == NULL)
//  {
//    change = true;
//  }
//  else if (importance > m_importance)
//  {
//    m_action->getActionEndedEvent().unsubscribe(m_id);
//    change = true;
//  }
//  if (change)
//  {
//    m_action = &action;
//    m_importance = importance;
//    m_id = m_action->getActionEndedEvent().subscribe(&m_eventHandler, NULL);
//  }
//  return change;
//}
//
//void ArbitratorUnitData::ActionData::onActionTerminate(void* data)
//{
//  m_action->getActionEndedEvent().unsubscribe(m_id);
//  m_action = NULL;
//}
//
//void ArbitratorUnitData::ActionData::clear()
//{
//  if (m_action != NULL)
//  {
//    m_action->getActionEndedEvent().unsubscribe(m_id);
//    m_action = NULL;
//	  m_importance = 0;
//  }
//}