// Author: Kirill Gavrilov
// Copyright (c) 2016-2019 OPEN CASCADE SAS
//
// This file is part of Open CASCADE Technology software library.
//
// This library is free software; you can redistribute it and/or modify it under
// the terms of the GNU Lesser General Public License version 2.1 as published
// by the Free Software Foundation, with special exception defined in the file
// OCCT_LGPL_EXCEPTION.txt. Consult the file LICENSE_LGPL_21.txt included in OCCT
// distribution for complete text of the license and disclaimer of any warranty.
//
// Alternatively, this file may be used under the terms of Open CASCADE
// commercial license or contractual agreement.

#ifndef _RWGltf_CafReader_HeaderFile
#define _RWGltf_CafReader_HeaderFile

#include <NCollection_DynamicArray.hxx>
#include <RWMesh_CafReader.hxx>
#include <TopoDS_Face.hxx>

class RWMesh_TriangulationReader;

//! The glTF (GL Transmission Format) mesh reader into XDE document.
class RWGltf_CafReader : public RWMesh_CafReader
{
  DEFINE_STANDARD_RTTIEXT(RWGltf_CafReader, RWMesh_CafReader)
public:
  //! Empty constructor.
  Standard_EXPORT RWGltf_CafReader();

  //! Return TRUE if multithreaded optimizations are allowed; FALSE by default.
  bool ToParallel() const { return myToParallel; }

  //! Setup multithreaded execution.
  void SetParallel(bool theToParallel) { myToParallel = theToParallel; }

  //! Return TRUE if Nodes without Geometry should be ignored, TRUE by default.
  bool ToSkipEmptyNodes() { return myToSkipEmptyNodes; }

  //! Set flag to ignore nodes without Geometry.
  void SetSkipEmptyNodes(bool theToSkip) { myToSkipEmptyNodes = theToSkip; }

  //! Return TRUE if all scenes in the document should be loaded, FALSE by default which means only
  //! main (default) scene will be loaded.
  bool ToLoadAllScenes() const { return myToLoadAllScenes; }

  //! Return TRUE if non-uniform scaling should be applied directly to the triangulation.
  //! FALSE if the average scale should be applied to the transformation matrix.
  bool ToApplyScale() const { return myToApplyScale; }

  //! Set flag to flag to load all scenes in the document, FALSE by default which means only main
  //! (default) scene will be loaded.
  void SetLoadAllScenes(bool theToLoadAll) { myToLoadAllScenes = theToLoadAll; }

  //! Set flag to use Mesh name in case if Node name is empty, TRUE by default.
  bool ToUseMeshNameAsFallback() { return myUseMeshNameAsFallback; }

  //! Set flag to use Mesh name in case if Node name is empty.
  void SetMeshNameAsFallback(bool theToFallback) { myUseMeshNameAsFallback = theToFallback; }

  //! Return flag to fill in triangulation using double or single precision; FALSE by default.
  bool IsDoublePrecision() const { return myIsDoublePrecision; }

  //! Set flag to fill in triangulation using double or single precision.
  void SetDoublePrecision(bool theIsDouble) { myIsDoublePrecision = theIsDouble; }

  //! Returns TRUE if data loading should be skipped and can be performed later; FALSE by default.
  bool ToSkipLateDataLoading() { return myToSkipLateDataLoading; }

  //! Sets flag to skip data loading.
  void SetToSkipLateDataLoading(bool theToSkip) { myToSkipLateDataLoading = theToSkip; }

  //! Set flag to apply non-uniform scaling directly to the triangulation (modify nodes).
  //! TRUE by default. In case of FALSE the average scale is applied to the transformation matrix.
  void SetToApplyScale(bool theToApplyScale) { myToApplyScale = theToApplyScale; }

  //! Returns TRUE if data should be loaded into itself without its transferring to new structure.
  //! It allows to keep information about deferred storage to load/unload this data later.
  //! TRUE by default.
  bool ToKeepLateData() { return myToKeepLateData; }

  //! Sets flag to keep information about deferred storage to load/unload data later.
  void SetToKeepLateData(bool theToKeep) { myToKeepLateData = theToKeep; }

  //! Returns TRUE if additional debug information should be print; FALSE by default.
  bool ToPrintDebugMessages() const { return myToPrintDebugMessages; }

  //! Sets flag to print debug information.
  void SetToPrintDebugMessages(const bool theToPrint) { myToPrintDebugMessages = theToPrint; }

protected:
  //! Read the mesh from specified file.
  Standard_EXPORT bool performMesh(std::istream&                  theStream,
                                   const TCollection_AsciiString& theFile,
                                   const Message_ProgressRange&   theProgress,
                                   const bool                     theToProbe) override;

  //! Fill document with new root shapes.
  Standard_EXPORT void fillDocument() override;

  //! Append new shape into the document (recursively).
  Standard_EXPORT bool addShapeIntoDoc(CafDocumentTools&              theTools,
                                       const TopoDS_Shape&            theShape,
                                       const TDF_Label&               theLabel,
                                       const TCollection_AsciiString& theParentName,
                                       const bool                     theHasScale = false,
                                       const gp_XYZ& theScale = gp_XYZ(0., 0., 0.));

  //! Create primitive array reader context.
  //! Can be overridden by sub-class to read triangulation into application-specific data structures
  //! instead of Poly_Triangulation. Default implementation creates RWGltf_TriangulationReader.
  Standard_EXPORT virtual occ::handle<RWMesh_TriangulationReader> createMeshReaderContext() const;

  //! Read late data from RWGltf_GltfLatePrimitiveArray stored as Poly_Triangulation within faces.
  Standard_EXPORT virtual bool readLateData(NCollection_DynamicArray<TopoDS_Face>& theFaces,
                                            const TCollection_AsciiString&         theFile,
                                            const Message_ProgressRange&           theProgress);

  //! Set reader for each late data.
  Standard_EXPORT void updateLateDataReader(
    NCollection_DynamicArray<TopoDS_Face>&         theFaces,
    const occ::handle<RWMesh_TriangulationReader>& theReader) const;

protected:
  class CafReader_GltfBaseLoadingFunctor;
  class CafReader_GltfFullDataLoadingFunctor;
  class CafReader_GltfStreamDataLoadingFunctor;

protected:
  bool myToParallel;           //!< flag to use multithreading; FALSE by default
  bool myToSkipEmptyNodes;     //!< ignore nodes without Geometry; TRUE by default
                               // clang-format off
  bool myToLoadAllScenes;       //!< flag to load all scenes in the document, FALSE by default
  bool myUseMeshNameAsFallback; //!< flag to use Mesh name in case if Node name is empty, TRUE by default
  bool myIsDoublePrecision;     //!< flag to fill in triangulation using single or double precision
  bool myToSkipLateDataLoading; //!< flag to skip triangulation loading
  bool myToKeepLateData;        //!< flag to keep information about deferred storage to load/unload triangulation later
                               // clang-format on
  bool myToPrintDebugMessages; //!< flag to print additional debug information
  bool myToApplyScale;         //!< flag to apply non-uniform scaling
  NCollection_DataMap<TopoDS_Shape, gp_XYZ, TopTools_ShapeMapHasher>*
    myShapeScaleMap; //!< map of shapes with non-uniform scalings
};

#endif // _RWGltf_CafReader_HeaderFile
