#include "stdafx.h"
#include "../UnitRecord.h"
#include "../UnitRecordManager.h"
#include "TransportUnitRecordType.h"

void TransportUnitRecordType::_outputUnitInfo(std::ostream& out) const
{
  UnitRecordType::_outputUnitInfo(out);
  out << " (known loaded units excluding history: ";
  std::pair<LoadedUnitIterator, LoadedUnitIterator> loadedUnits = getLoadedUnits();
  for (; loadedUnits.first != loadedUnits.second; ++loadedUnits.first)
  {
    const UnitRecord* loadedUnit = *loadedUnits.first;
    out << "[" << *loadedUnit << "]";
  }
  out << ")";
}

TransportUnitRecordType::SavedState::SavedState(TransportUnitRecordType* unitRecordType)
  : UnitRecordType::SavedState(unitRecordType)
{
  std::pair<TransportUnitRecordType::LoadedUnitIterator, TransportUnitRecordType::LoadedUnitIterator> loadedUnits =
    unitRecordType->getLoadedUnits();
  m_loadedUnits.clear();
  m_loadedUnits.insert(loadedUnits.first, loadedUnits.second);
}

void TransportUnitRecordType::onDestroyed()
{
  {
    std::set<AutoPtr<UnitRecord>>::const_iterator it = m_loadedUnits.begin();
    while (it != m_loadedUnits.end())
    {
      std::set<AutoPtr<UnitRecord>>::const_iterator next = ++(std::set<AutoPtr<UnitRecord>>::const_iterator(it));
      (*it)->onTransportDestroyed(m_unitRecord);
      it = next;
    }
  }
  {
    std::set<AutoPtr<UnitRecord>>::const_iterator it = m_loadedUnitsHistory.begin();
    while (it != m_loadedUnitsHistory.end())
    {
      std::set<AutoPtr<UnitRecord>>::const_iterator next = ++(std::set<AutoPtr<UnitRecord>>::const_iterator(it));
      (*it)->onTransportDestroyed(m_unitRecord);
      it = next;
    }
  }
}

void TransportUnitRecordType::onUnitDiscover()
{
  UnitRecordType::onUnitDiscover();
}

void TransportUnitRecordType::onUnitEvade()
{
  UnitRecordType::onUnitEvade();
  m_loadedUnits.clear();
}