#pragma once
#include "../QTreeWidgetItemCustom.h"
#include <QtCore/QString>
#include <PerceptualState/PerceptualState.h>

class UnitRecordWrapper
{
public:
  class QTreeWidgetItemUnit : public QTreeWidgetItemCustom<size_t>
  {
  public:
    QTreeWidgetItemUnit(QTreeWidget *view, const void* key) : QTreeWidgetItemCustom<size_t>(view, key) {}
    QTreeWidgetItemUnit(QTreeWidgetItem *item, const void* key) : QTreeWidgetItemCustom<size_t>(item, key) {}
    virtual void addUnits(UnitsToSelect &units) const
    {
      units.insertUnitRecord(m_data);
    }
  };

  UnitRecordWrapper(const UnitRecord* unit)
    : m_key(unit)
    , m_id(unit->getID())
    , m_name(QString::fromStdString(toString(unit)))
    , m_player(unit->getPlayer())
    , m_accessible(unit->isAccessible())
    , m_position(unit->getPosition())
    , m_unitType(unit->getType())
  {}

  virtual void display(QTreeWidget* parent) const
  {
    QTreeWidgetItemUnit* item = new QTreeWidgetItemUnit(parent, m_key);
    displayIn(item);
  }

  void display(QTreeWidgetItem* parent) const
  {
    QTreeWidgetItemUnit* item = new QTreeWidgetItemUnit(parent, m_key);
    displayIn(item);
  }

  bool operator<(const UnitRecordWrapper& other) const
  {
    return m_id < other.m_id;
  }
protected:
  void displayIn(QTreeWidgetItemUnit* item) const;

  const void* const m_key;
  const size_t m_id;
  const BWAPI::Player* const m_player;
  const QString m_name;
  const bool m_accessible;
  const BWAPI::Position m_position;
  const BWAPI::UnitType m_unitType;
};
