// SPDX-FileCopyrightText: Copyright (c) Kitware Inc.
// SPDX-FileCopyrightText: Copyright (c) Ken Martin, Will Schroeder, Bill Lorensen
// SPDX-License-Identifier: BSD-3-Clause

#include "vtkAdditionalFieldReader.h"

#include "vtkObjectFactory.h"

#include "vtkAbstractArray.h"
#include "vtkCompositeDataIterator.h"
#include "vtkCompositeDataSet.h"
#include "vtkDataObject.h"
#include "vtkDataObjectReader.h"
#include "vtkDataSet.h"
#include "vtkFieldData.h"
#include "vtkInformation.h"
#include "vtkInformationVector.h"
#include "vtkSmartPointer.h"

//----------------------------------------------------------------------------
vtkStandardNewMacro(vtkAdditionalFieldReader);

//----------------------------------------------------------------------------
vtkAdditionalFieldReader::vtkAdditionalFieldReader()
{
  this->FileName = nullptr;
}

//----------------------------------------------------------------------------
vtkAdditionalFieldReader::~vtkAdditionalFieldReader()
{
  this->SetFileName(nullptr);
}

//----------------------------------------------------------------------------
void vtkAdditionalFieldReader::PrintSelf(ostream& os, vtkIndent indent)
{
  os << indent << "FileName: " << (this->FileName ? this->FileName : "nullptr") << endl;
}

//----------------------------------------------------------------------------
int vtkAdditionalFieldReader::RequestData(vtkInformation* vtkNotUsed(request),
  vtkInformationVector** inputVector, vtkInformationVector* outputVector)
{
  vtkInformation* inInfo = inputVector[0]->GetInformationObject(0);
  vtkDataObject* din = inInfo->Get(vtkDataObject::DATA_OBJECT());

  vtkInformation* outInfo = outputVector->GetInformationObject(0);
  vtkDataObject* dout = outInfo->Get(vtkDataObject::DATA_OBJECT());
  vtkCompositeDataSet* cdout = vtkCompositeDataSet::SafeDownCast(dout);
  vtkCompositeDataIterator* cditer = nullptr;
  dout->ShallowCopy(din);
  if (cdout)
  {
    cditer = cdout->NewIterator();
  }

  if (this->FileName && strlen(this->FileName))
  {
    vtkSmartPointer<vtkDataObjectReader> dor = vtkSmartPointer<vtkDataObjectReader>::New();
    dor->SetFileName(this->FileName);
    dor->Update();
    vtkDataObject* d = dor->GetOutput();
    if (!d)
    {
      return VTK_ERROR;
    }
    vtkFieldData* arrs = d->GetFieldData();
    vtkFieldData* ofd = nullptr;
    for (int i = 0; i < arrs->GetNumberOfArrays(); i++)
    {
      if (cditer)
      {
        cditer->InitTraversal();
        while (!cditer->IsDoneWithTraversal())
        {
          vtkDataObject* dsout = cditer->GetCurrentDataObject();
          ofd = dsout->GetFieldData();
          ofd->AddArray(arrs->GetAbstractArray(i));
          cditer->GoToNextItem();
        }
      }
      else
      {
        ofd = dout->GetFieldData();
        ofd->AddArray(arrs->GetAbstractArray(i));
      }
    }
  }
  if (cditer)
  {
    cditer->Delete();
  }

  return VTK_OK;
}
