#ifndef QMLOBJECTLISTHELPER_H
#define QMLOBJECTLISTHELPER_H

#include "QmlObjectListModel.h"

#include <type_traits>

template <class PtrType>
inline bool contains(const QmlObjectListModel &in, const PtrType toFind) {

  static_assert(std::is_pointer<PtrType>::value, "PtrType must be a pointer.");

  typedef typename std::remove_pointer<PtrType>::type Type;

  for (int i = 0; i < in.count(); ++i) {
    const auto obj = qobject_cast<const Type *>(in[i]);
    Q_ASSERT(obj != nullptr);
    Q_ASSERT(toFind != nullptr);
    if (*obj == *toFind) {
      return true;
    }
  }
  return false;
}

template <class PtrType>
inline bool equals(const QmlObjectListModel &list,
                   const QmlObjectListModel &otherList) {

  static_assert(std::is_pointer<PtrType>::value, "PtrType must be a pointer.");

  typedef typename std::remove_pointer<PtrType>::type Type;

  if (list.count() == otherList.count()) {

    for (int i = 0; i < list.count(); ++i) {
      const auto obj = qobject_cast<const Type *>(list[i]);
      const auto otherObj = qobject_cast<const Type *>(otherList[i]);
      Q_ASSERT(obj != nullptr);
      Q_ASSERT(otherObj != nullptr);
      if (*obj != *otherObj) {
        return false;
      }
    }

    return true;
  } else {
    return false;
  }
}

#endif // QMLOBJECTLISTHELPER_H
