/*! Visits a SFFieldContainerPtr (or more specific pointer type) during
    preWrite. It creates an element for the pointed to container and
    calls preWrite on it. If the pointed to container is not in the
    root's id set it is added and thus scheduled for writing.

    \param[in] fieldId Id of the field in the container of this element.
 */
void
OSBCommonElement::preWritePtrSingleField(const UInt32 fieldId)
{
    OSG_OSB_LOG(("OSBCommonElement::preWritePtrSingleField: "
            "fieldId: [%u]\n", fieldId));

    OSBRootElement     *root       = editRoot();

    FieldContainerPtrSFieldBase::GetHandlePtr sfPtrField =
        boost::dynamic_pointer_cast<FieldContainerPtrSFieldBase::GetHandle>(
            getContainer()->getField(fieldId));

    if(sfPtrField == NULL || sfPtrField->isValid() == false)
        return;

    FieldContainer *refedFC = (*sfPtrField)->getValue();

    if(refedFC == NULL)
        return;

    UInt32             refedId  = refedFC->getId  ();
    const std::string &typeName = refedFC->getType().getName();

    // only schedule a container once
    if(root->getIdSet().count(refedId) == 0)
    {
        OSBElementBase *elem = OSBElementFactory::the()->acquire(
            typeName, root);

        root->editIdSet      ().insert   (refedId);
        root->editElementList().push_back(elem   );
        elem->setContainer(refedFC);
        elem->preWrite    (refedFC);
    }
}
/*! Callback called for each element in an AttachmentMap (this is used by
    preWriteAttachmentMapField).
 */
void OSBCommonElement::handleAttachmentMapElementPreWrite(
    FieldContainer *refedFC)
{
    OSG_OSB_LOG(("OSBCommonElement::handleAttachmentMapElementPreWrite\n"));

    if(refedFC == NULL)
        return;

    Attachment *refedAtt = dynamic_cast<Attachment *>(refedFC);

    // skip attachments marked as 'internal'
    if(refedAtt                              == NULL ||
       refedAtt->getSFInternal()->getValue() == true   )
    {
        return;
    }

    OSBRootElement    *root     = editRoot();
    UInt32             refedId  = refedAtt->getId  ();
    const std::string &typeName = refedAtt->getType().getName();

    // only schedule a container once
    if(root->getIdSet().count(refedId) > 0)
        return;

    OSBElementBase *elem = OSBElementFactory::the()->acquire(typeName, root);

    root->editIdSet      ().insert   (refedId);
    root->editElementList().push_back(elem   );
    elem->setContainer(refedAtt);
    elem->preWrite    (refedAtt);
}
void OSBChunkBlockElement::postRead(void)
{
    // _mfChunks and _mfSlots have to be kept consistent for ChunkBlock.
    // Also TextureChunk is split into TextureObjChunk and TextureObjChunk
    // on load, so the slot info has to be duplicated.
    // TODO:
    // It would be better if that handling could be confined to
    // TextureChunkElement, but I've not found a good way to do that and keep
    // the information in _mfSlots correct. -- cneumann

    const OSBRootElement *root   = getRoot();
          ChunkBlock     *chkMat = dynamic_cast<ChunkBlock *>(getContainer());
    
    // No chunks loaded
    if(_chunksPtrFieldItValid == false)
        return;

    PtrFieldInfo::PtrIdStoreConstIt idIt  = 
        _chunksPtrFieldIt->getIdStore().begin();
    PtrFieldInfo::PtrIdStoreConstIt idEnd = 
        _chunksPtrFieldIt->getIdStore().end  ();

    for(UInt32 i = 0; idIt != idEnd; ++idIt, ++i)
    {
        UInt32 uiId   =  (*idIt) & 0x00FFFFFF;
        UInt32 uiSlot = ((*idIt) & 0xFF000000) >> 24;

        OSBRootElement::IdElemMapConstIt mapIt = 
            root->getIdElemMap().find(uiId);

        if(mapIt == root->getIdElemMap().end())
        {
            continue;
        }

        OSBElementBase *chunkElem = mapIt->second;

        // other chunk
        StateChunk     *chunk     = 
            dynamic_cast<StateChunk *>(chunkElem->getContainer());
        

        chkMat->addChunk(chunk, uiSlot);
    }
    
    // pointer mapping is already done here for _mfChunks, clear info
    _chunksPtrFieldIt->editIdStore     ().clear();
    _chunksPtrFieldIt->editBindingStore().clear();
}
/*! Collects all containers that are reachable from \a fc and schedules them
    for writing. For each container an approriate element is created and
    its preWrite method is called, thus recursively discovering all reachable
    containers.

    \param[in] fc FieldContainer that is inspected for reachable containers.
 */
void
OSBRootElement::preWrite(FieldContainer * const fc)
{
    OSG_OSB_LOG(("OSBRootElement::preWrite\n"));

    if(fc == NULL)
        return;

    const std::string    &typeName = fc->getType().getName();
          OSBElementBase *elem     = OSBElementFactory::the()->acquire(
        typeName, this);

    editElementList().push_back(elem);
    elem->setContainer(fc         );
    elem->setFCIdFile (fc->getId());
    elem->preWrite    (fc         );
}
/*! Visits a MFFieldContainerPtr (or more specific pointer type) during
    preWrite. It creates elements for the pointed to containers and
    calls preWrite on them. If the pointed to containers are not in the
    root's id set they are added and thus scheduled for writing.

    \param[in] fieldId Id of the field in the container of this element.
 */
void
OSBCommonElement::preWritePtrMultiField(const UInt32 fieldId)
{
    OSG_OSB_LOG(("OSBCommonElement::preWritePtrMultiField: "
            "fieldId: [%u]\n", fieldId));

    OSBRootElement           *root       = editRoot();

    FieldContainerPtrMFieldBase::GetHandlePtr mfPtrField =
        boost::dynamic_pointer_cast<FieldContainerPtrMFieldBase::GetHandle>(
            getContainer()->getField(fieldId));

    if(mfPtrField == NULL || mfPtrField->isValid() == false)
        return;

    FieldContainerPtrMFieldBase::const_iterator fieldIt  = 
        (*mfPtrField)->begin();

    FieldContainerPtrMFieldBase::const_iterator fieldEnd = 
        (*mfPtrField)->end  ();

    for(; fieldIt != fieldEnd; ++fieldIt)
    {
        FieldContainer *refedFC = *fieldIt;

        if(refedFC == NULL)
            continue;

        UInt32             refedId  = refedFC->getId  ();
        const std::string &typeName = refedFC->getType().getName();

        // only schedule a container once
        if(root->getIdSet().count(refedId) > 0)
            continue;

        OSBElementBase *elem = OSBElementFactory::the()->acquire(
            typeName, root);

        root->editIdSet      ().insert   (refedId);
        root->editElementList().push_back(elem   );
        elem->setContainer(refedFC);
        elem->preWrite    (refedFC);
    }
}
/*! Callback called for each element in a map field (this is used by
    preWriteMapField).
 */
void OSBCommonElement::handleMapElementPreWrite(FieldContainer *refedFC)
{
    if(refedFC == NULL)
        return;

    OSBRootElement    *root      = editRoot();
    UInt32             refedId  = refedFC->getId  ();
    const std::string &typeName = refedFC->getType().getName();

    // only schedule a container once
    if(root->getIdSet().count(refedId) > 0)
        return;

    OSBElementBase *elem = OSBElementFactory::the()->acquire(typeName, root);

    root->editIdSet      ().insert   (refedId);
    root->editElementList().push_back(elem   );
    elem->setContainer(refedFC);
    elem->preWrite    (refedFC);
}
/*! Reads from the stream set by a preceding call to initialiseRead. Since the
    root element is the first one created it reads the file header and
    creates the elements to read the data following the header.

    \param[in] typeName The argument is ignored.
 */
void
OSBRootElement::read(const std::string &/*typeName*/)
{
    OSG_OSB_LOG(("OSBRootElement::read\n"));

    BinaryReadHandler *rh           = getReadHandler();
    std::string        headerMarker;

    rh->getValue(headerMarker);

    if(headerMarker == OSGOSB_HEADER_ID_1)
    {
        OSG_OSB_LOG(("OSBRootElement::read: Header version: [%u]\n",
                OSGOSBHeaderVersion100));
        setHeaderVersion(OSGOSBHeaderVersion100);
    }
    else if(headerMarker == OSGOSB_HEADER_ID_2)
    {
        OSG_OSB_LOG(("OSBRootElement::read: Header version: [%u]\n",
                OSGOSBHeaderVersion200));
        setHeaderVersion(OSGOSBHeaderVersion200);
    }
//     else if(headerMarker == OSGOSB_HEADER_ID_201)
//     {
//         OSG_OSB_LOG(("OSBRootElement::read: Header version: [%u]\n",
//                 OSGOSBHeaderVersion201));
//         setHeaderVersion(OSGOSBHeaderVersion201);
//     }
    else
    {
        FWARNING(("OSBRootElement::read: Unrecognized file header, could not "
                  "load file.\n"));
        return;
    }

    std::string headerName;
    rh->getValue(headerName);
    std::string headerOptions;
    rh->getValue(headerOptions);
    UInt64 fileSize;
    rh->getValue(fileSize);

    OSG_OSB_LOG(("OSBRootElement::read: headerName: [%s]\n",
            headerName.c_str()));
    OSG_OSB_LOG(("OSBRootElement::read: headerOptions: [%s]\n",
            headerOptions.c_str()));
    OSG_OSB_LOG(("OSBRootElement::read: fileSize: [%" PRISize "]\n",
            fileSize));

    std::string     fcTypeName;
    UInt32          fcIdFile;    // id used in the file
    UInt32          fcIdSystem;  // id used in the system
    OSBElementBase *elem;

    while(true)
    {
        if(!readFieldContainerHeader(fcTypeName, fcIdFile))
            break;

        OSG_OSB_LOG(("OSBRootElement::read: fcTypeName [%s] fcIdFile: [%u]\n",
                fcTypeName.c_str(), fcIdFile));

        elem = OSBElementFactory::the()->acquire(fcTypeName, this);
        elem->setFCIdFile(fcIdFile  );
        elem->read       (fcTypeName);

        if(elem->getContainer() != NULL)
        {
            fcIdSystem = elem->getContainer()->getId();

            OSG_OSB_LOG(("OSBRootElement::read: fcIdFile: [%u] fcIdSystem: [%u]\n",
                    fcIdFile, fcIdSystem));

            editIdMap().insert(
                FieldContainerIdMap::value_type(fcIdFile, fcIdSystem));

            if(getContainer() == NULL)
            {
                setContainer(elem->getContainer());
            }

            editElementList().push_back(elem                          );
            editIdElemMap  ().insert   (std::make_pair(fcIdFile, elem));
        }
    }
}
void
OSBGeometryElement::preWrite(FieldContainer * const fc)
{
    OSG_OSB_LOG(("OSBGeometryElement::preWrite\n"));

    OSBRootElement *root       = editRoot();
    UInt32          fieldCount = fc->getType().getNumFieldDescs();

    UInt8 quanResPositions = root->getOptions().quantizePositions();
    UInt8 quanResNormals   = root->getOptions().quantizeNormals  ();
    UInt8 quanResTexCoords = root->getOptions().quantizeTexCoords();
    bool  packIndices      = root->getOptions().packIndices      ();

    // go through all fields and find those refering to other FCs
    for(UInt32 fieldId = 1; fieldId <= fieldCount; ++fieldId)
    {
        const FieldDescriptionBase *fieldDesc = fc->getFieldDescription(fieldId);
        const FieldType            &fieldType = fieldDesc->getFieldType();
        const std::string          &fieldName = fieldDesc->getName     ();

        // skip internal fields
        if(fieldDesc->isInternal())
        {
            OSG_OSB_LOG(("OSBGeometryElement::preWrite: "
                    "Skipping internal field: [%s]\n", fieldName.c_str()));
            continue;
        }

        if(fieldName == "properties")
        {
            // "properties" might be quantized
            FieldContainerPtrMFieldBase::GetHandlePtr fP =
                boost::dynamic_pointer_cast<
                    FieldContainerPtrMFieldBase::GetHandle>(
                        getContainer()->getField(fieldId));

            if(fP == NULL || fP->isValid() == false)
                continue;

            FieldContainerPtrMFieldBase::const_iterator fieldIt  = 
                (*fP)->begin();

            FieldContainerPtrMFieldBase::const_iterator fieldEnd = 
                (*fP)->end  ();

            for(UInt32 i = 0; fieldIt != fieldEnd; ++fieldIt, ++i)
            {
                FieldContainer *refedFC = *fieldIt;

                if(refedFC == NULL)
                    continue;

                      UInt32      refedId  = refedFC->getId  ();
                const std::string typeName = refedFC->getType().getName();

                // only schedule a container once
                if(root->getIdSet().count(refedId) > 0)
                    continue;

                OSBElementBase              *elem     =
                    OSBElementFactory::the()->acquire(typeName, root);
                OSBGeoVectorPropertyElement *propElem =
                    dynamic_cast<OSBGeoVectorPropertyElement *>(elem);

                if((propElem != 0) && (i == Geometry::PositionsIndex))
                {
                    propElem->setQuantizeResolution(quanResPositions);
                }
                else if((propElem != 0) && (i == Geometry::NormalsIndex))
                {
                    propElem->setQuantizeResolution(quanResNormals);
                }
                else if((propElem != 0) && (i >= Geometry::TexCoordsIndex) &&
                        (i <= Geometry::TexCoords7Index)                     )
                {
                    propElem->setQuantizeResolution(quanResTexCoords);
                }

                root->editIdSet      ().insert   (refedId);
                root->editElementList().push_back(elem   );
                elem->setContainer(refedFC);
                elem->preWrite    (refedFC);
            }
        }
        else if(fieldName == "propIndices")
        {
            // "propIndices" might be packed
            FieldContainerPtrMFieldBase::GetHandlePtr fP =
                boost::dynamic_pointer_cast<
                    FieldContainerPtrMFieldBase::GetHandle>(
                        getContainer()->getField(fieldId));

            if(fP == NULL || fP->isValid() == false)
                continue;

            FieldContainerPtrMFieldBase::const_iterator fieldIt  = 
                (*fP)->begin();
            FieldContainerPtrMFieldBase::const_iterator fieldEnd = 
                (*fP)->end  ();

            for(UInt32 i = 0; fieldIt != fieldEnd; ++fieldIt, ++i)
            {
                FieldContainer *refedFC = *fieldIt;

                if(refedFC == NULL)
                    continue;

                      UInt32      refedId  = refedFC->getId  ();
                const std::string typeName = refedFC->getType().getName();

                // only schedule a container once
                if(root->getIdSet().count(refedId) > 0)
                    continue;

                OSBElementBase                *elem     =
                    OSBElementFactory::the()->acquire(typeName, root);
                OSBGeoIntegralPropertyElement *propElem =
                    dynamic_cast<OSBGeoIntegralPropertyElement *>(elem);

                if((propElem != 0) && (packIndices == true))
                {
                    propElem->setPackData(true);
                }

                root->editIdSet      ().insert   (refedId);
                root->editElementList().push_back(elem   );
                elem->setContainer(refedFC);
                elem->preWrite    (refedFC);
            }
        }
        else if(fieldName == "attachments")
        {
            preWriteAttachmentMapField(fieldId);
        }
        else
        {
            // check if field refers to another FC, i.e. its a field holding
            // FieldContainerPtr or an FieldContainerAttachmentMap
            if(fieldType.getContentType().isDerivedFrom(
                FieldTraits<FieldContainer *>::getType()) == true)
            {
                if(fieldType.getCardinality() == FieldType::SingleField)
                {
                    preWritePtrSingleField(fieldId);
                }
                else if(fieldType.getCardinality() == FieldType::MultiField)
                {
                    preWritePtrMultiField(fieldId);
                }
            }
        }
    }
}