/**
 * @brief Creates a quest files model.
 * @param parent Path of the quest to represent.
 */
QuestFilesModel::QuestFilesModel(Quest& quest):
  QSortFilterProxyModel(nullptr),
  quest(quest),
  source_model(new QFileSystemModel) {

  // Watch changes on the filesystem.
  source_model->setRootPath(quest.get_data_path());  // Only watch changes in the data directory.
  source_model->setReadOnly(false);
  setSourceModel(source_model);

  // Watch changes in resources.
  connect(&quest.get_resources(), SIGNAL(element_added(ResourceType, QString, QString)),
          this, SLOT(resource_element_added(ResourceType, QString, QString)));
  connect(&quest.get_resources(), SIGNAL(element_removed(ResourceType, QString)),
          this, SLOT(resource_element_removed(ResourceType, QString)));
  connect(&quest.get_resources(), SIGNAL(element_renamed(ResourceType, QString, QString)),
          this, SLOT(resource_element_renamed(ResourceType, QString, QString)));
  connect(&quest.get_resources(), SIGNAL(element_description_changed(ResourceType, QString, QString)),
          this, SLOT(resource_element_description_changed(ResourceType, QString, QString)));

  // This model adds extra items for files missing on the filesystem.
  // To ensure we have an extra item if and only if the file is missing,
  // we need to watch files creations and destructions.
  connect(source_model, SIGNAL(rowsInserted(QModelIndex, int, int)),
          SLOT(source_model_rows_inserted(QModelIndex, int, int)));
  connect(source_model, SIGNAL(rowsAboutToBeRemoved(QModelIndex, int, int)),
          SLOT(source_model_rows_about_to_be_removed(QModelIndex, int, int)));
}
/**
 * @brief Changes the id of a resource element from the list.
 * @param resource_ A type of resource.
 * @param old_id Id of the element to change.
 * @param new_id The new id to set.
 * @return @c true in case of success, @c false if the old id does not
 * exist or if the new id already exists.
 */
bool QuestResources::rename(
    ResourceType resource_type,
    const QString& old_id,
    const QString& new_id
) {

  if (!resources.rename(resource_type, old_id.toStdString(),
                        new_id.toStdString())) {
    return false;
  }
  emit element_renamed(resource_type, old_id, new_id);
  return true;
}
/**
 * @brief Creates a resource model.
 * @param quest The quest.
 * @param resource_type Type of resources to show.
 * @param parent The parent object or nullptr.
 */
ResourceModel::ResourceModel(const Quest& quest, ResourceType resource_type, QObject* parent) :
  QStandardItemModel(parent),
  quest(quest),
  resource_type(resource_type),
  items(),
  icons(),
  directory_icon(":/images/icon_folder_open.png"),
  tileset_id() {

  QStringList ids = get_resources().get_elements(this->resource_type);
  for (const QString& id : ids) {
    add_element(id);
  }

  const QuestResources& resources = get_resources();
  connect(&resources, SIGNAL(element_added(ResourceType, QString, QString)),
          this, SLOT(element_added(ResourceType, QString, QString)));
  connect(&resources, SIGNAL(element_removed(ResourceType, QString)),
          this, SLOT(element_removed(ResourceType, QString)));
  connect(&resources, SIGNAL(element_renamed(ResourceType, QString, QString)),
          this, SLOT(element_renamed(ResourceType, QString, QString)));
  connect(&resources, SIGNAL(element_description_changed(ResourceType, QString, QString)),
          this, SLOT(element_description_changed(ResourceType, QString, QString)));
}