// -----------------------------------------------------------------------------
//
// -----------------------------------------------------------------------------
int H5FilterParametersWriter::writeValue(const QString name, const DataContainerArrayProxy& v)
{
  int err = 0;
  QList<DataContainerProxy> dcProxies = v.dataContainers.values();
  QListIterator<DataContainerProxy> dcIter(dcProxies);
  hid_t dcaGid = QH5Utilities::createGroup(m_CurrentGroupId, name);

  while (dcIter.hasNext()) // DataContainerLevel
  {
    const DataContainerProxy& dcProxy =  dcIter.next();
    if(dcProxy.flag == Qt::Unchecked)
    {
      continue;    // Skip to the next DataContainer if we are not reading this one.
    }
    hid_t dcGid = QH5Utilities::createGroup(dcaGid, dcProxy.name);

    QStringList flat;
    QMapIterator<QString, AttributeMatrixProxy> amIter(dcProxy.attributeMatricies);
    while(amIter.hasNext()) // AttributeMatrixLevel
    {
      amIter.next();

      const AttributeMatrixProxy& amProxy = amIter.value();
      if(amProxy.flag == Qt::Unchecked)
      {
        continue;    // Skip to the next AttributeMatrix if not reading this one
      }
      hid_t amGid = QH5Utilities::createGroup(dcGid, amProxy.name);

      QMapIterator<QString, DataArrayProxy> dIter(amProxy.dataArrays);
      while(dIter.hasNext()) // DataArray Level
      {
        dIter.next();

        const DataArrayProxy& daProxy = dIter.value();
        if(daProxy.flag == DREAM3D::Unchecked)
        {
          continue;    // Skip to the next DataArray if not reading this one
        }

        flat << dIter.value().name;
      }
      QString data = flat.join(QString('\n'));
      err = QH5Lite::writeStringDataset(amGid, QString::fromLatin1("Arrays"), data);

      H5Gclose(amGid);
    }

    H5Gclose(dcGid);
  }

  H5Gclose(dcaGid);
  return err;
}
// -----------------------------------------------------------------------------
//
// -----------------------------------------------------------------------------
QStringList DataContainerArrayProxy::flattenHeirarchy(Qt::CheckState dcFlag, Qt::CheckState amFlag, Qt::CheckState daFlag)
{
  QStringList strList;
  QList<DataContainerProxy> dcProxies = dataContainers.values();
  QListIterator<DataContainerProxy> dcIter(dcProxies);

  while (dcIter.hasNext()) // DataContainerLevel
  {
    const DataContainerProxy& dcProxy =  dcIter.next();
    if(dcProxy.flag != dcFlag)
    {
      continue;    // Skip to the next DataContainer if we are not reading this one.
    }
    QMapIterator<QString, AttributeMatrixProxy> amIter(dcProxy.attributeMatricies);
    while(amIter.hasNext()) // AttributeMatrixLevel
    {
      amIter.next();

      const AttributeMatrixProxy& amProxy = amIter.value();
      if(amProxy.flag != amFlag)
      {
        continue;    // Skip to the next AttributeMatrix if not reading this one
      }

      QMapIterator<QString, DataArrayProxy> dIter(amProxy.dataArrays);
      while(dIter.hasNext()) // DataArray Level
      {
        dIter.next();

        const DataArrayProxy& daProxy = dIter.value();
        if(daProxy.flag != daFlag)
        {
          continue;    // Skip to the next DataArray if not reading this one
        }

        QString path = QString("%1|%2|%3").arg(dcProxy.name).arg(amProxy.name).arg(daProxy.name);
        strList << path;
      }
    }
  }
  return strList;
}
// -----------------------------------------------------------------------------
//
// -----------------------------------------------------------------------------
void DataContainerArrayProxy::print(const QString header)
{
  QString str;
  QTextStream out(&str);
  QList<DataContainerProxy> dcProxies = dataContainers.values();
  QListIterator<DataContainerProxy> dcIter(dcProxies);

  while (dcIter.hasNext()) // DataContainerLevel
  {
    const DataContainerProxy& dcProxy =  dcIter.next();

    out << dcProxy.name << "|" << "  [flag:" << (int)(dcProxy.flag) << "]\n";
    QMapIterator<QString, AttributeMatrixProxy> amIter(dcProxy.attributeMatricies);
    while(amIter.hasNext()) // AttributeMatrixLevel
    {
      amIter.next();

      const AttributeMatrixProxy& amProxy = amIter.value();
      out << dcProxy.name << "|" << amProxy.name << "|" << "  [flag:" << (int)(amProxy.flag) << "]\n";
      QMapIterator<QString, DataArrayProxy> dIter(amProxy.dataArrays);
      while(dIter.hasNext()) // DataArray Level
      {
        dIter.next();

        const DataArrayProxy& daProxy = dIter.value();

        out << dcProxy.name << "|" << amProxy.name << "|" << daProxy.name << "  [flag:" << (int)(daProxy.flag) << "]"
            << " [Object Type: " << daProxy.objectType << "] [CompDims: ";
        for(int i = 0; i < daProxy.compDims.size(); i++)
        {
          out << daProxy.compDims[i] << " ";
        }
        out << "]\n";
      }
    }
  }
  std::cout << "---------------- " << header.toStdString() << " ----------------------" << std::endl;
  std::cout << str.toStdString() << std::endl;
}
// -----------------------------------------------------------------------------
//
// -----------------------------------------------------------------------------
int QFilterParametersWriter::writeValue(const QString name, const DataContainerArrayProxy& v)
{
  int err = 0;
  QList<DataContainerProxy> dcProxies = v.dataContainers.values();
  QListIterator<DataContainerProxy> dcIter(dcProxies);
  // Reset the iterator to the start of the QList
  dcIter.toFront();
  QStringList flat;
  QStringList daFlags;
  QStringList dcFlags;
  QStringList attrFlags;

  while (dcIter.hasNext()) // DataContainerLevel
  {
    const DataContainerProxy& dcProxy =  dcIter.next();
    // if(dcProxy.flag == Qt::Unchecked) { continue; } // Skip to the next DataContainer if we are not reading this one.

    if (dcProxy.attributeMatricies.size() > 0)
    {
      QMapIterator<QString, AttributeMatrixProxy> amIter(dcProxy.attributeMatricies);
      while (amIter.hasNext()) // AttributeMatrixLevel
      {
        amIter.next();

        const AttributeMatrixProxy& amProxy = amIter.value();
        // if(amProxy.flag == Qt::Unchecked) { continue; } // Skip to the next AttributeMatrix if not reading this one

        QMapIterator<QString, DataArrayProxy> dIter(amProxy.dataArrays);
        while (dIter.hasNext()) // DataArray Level
        {
          dIter.next();

          const DataArrayProxy& daProxy = dIter.value();
          //if(daProxy.flag == DREAM3D::Unchecked) { continue; } // Skip to the next DataArray if not reading this one

          QString path = QString("%1|%2|%3").arg(dcProxy.name).arg(amProxy.name).arg(daProxy.name);
          flat << path;
          dcFlags << QString("%1").arg(dcProxy.flag);
          attrFlags << QString("%1").arg(amProxy.flag);
          daFlags << QString("%1").arg(daProxy.flag);

        }
      }
    }
    else
    {
      QString path = QString("%1").arg(dcProxy.name);
      flat << path;
      dcFlags << QString("%1").arg(dcProxy.flag);
    }
  }

  qint32 count = flat.size();
  m_Prefs->beginWriteArray(name, count);
  QStringListIterator i(flat);
  for(int i = 0; i < count; i++)
  {
    QString data = flat.at(i);
    m_Prefs->setArrayIndex(i);
    m_Prefs->setValue("Path", data);

    if (dcFlags.size() > i)
    {
      data = dcFlags.at(i);
      m_Prefs->setValue("DCFlag", data);
    }

    if (attrFlags.size() > i)
    {
      data = attrFlags.at(i);
      m_Prefs->setValue("ATTRFlag", data);
    }

    if (daFlags.size() > i)
    {
      data = daFlags.at(i);
      m_Prefs->setValue("DAFlag", data);
    }
  }
  m_Prefs->endArray();
  return err;
}