#include <fstream>
#include "../Common/Common.h"
#define SAMPLE_FILENAME "ProceduralTexture.fbx"
#define TEXTURE_FILENAME "a_texture.jpg"
#define FILENAME_PROP "Filename"
bool CreateSceneAndSaveFile(int argc, char** argv);
bool ReadFileAndDumpProceduralTextureBlobOnDisk(int argc, char** argv);
bool DumpProceduralTextureBlobOnDisk(
FbxScene* pScene);
 
int main(int argc, char** argv)
{
    bool lResult;
    
    lResult = CreateSceneAndSaveFile(argc, argv);
    if(lResult == false)
    {
        return 1;
    }
    lResult = ReadFileAndDumpProceduralTextureBlobOnDisk(argc, argv);
    if(lResult == false)
    {
        return 1;
    }
    return 0;
}
bool CreateSceneAndSaveFile(int argc, char** argv)
{
    bool lResult;
    
    InitializeSdkObjects(lSdkManager, lScene);
    
    lResult = CreateScene(lScene);
    if(lResult == false)
    {
        FBXSDK_printf("\n\nAn error occurred while creating the scene...\n");
        DestroySdkObjects(lSdkManager, lResult);
        return false;
    }
    
    
    for( int i = 1, c = argc; i < c; ++i )
    {
        if( 
FbxString(argv[i]) == 
"-test" ) 
continue;
 
        else if( lFilePath.IsEmpty() ) lFilePath = argv[i];
    }
    if( lFilePath.IsEmpty() ) lFilePath = SAMPLE_FILENAME;
    FBXSDK_printf("Saving the file...\n");
    lResult = SaveScene(lSdkManager, lScene, lFilePath.Buffer());
    if(lResult == false)
    {
        FBXSDK_printf("\n\nAn error occurred while saving the scene...\n");
        DestroySdkObjects(lSdkManager, lResult);
        return false;
    }
    
    DestroySdkObjects(lSdkManager, lResult);
    return true;
}
bool ReadFileAndDumpProceduralTextureBlobOnDisk(int argc, char** argv)
{
    bool lResult;
    
    InitializeSdkObjects(lSdkManager, lScene);
    
    for( int i = 1, c = argc; i < c; ++i )
    {
        if( 
FbxString(argv[i]) == 
"-test" ) 
continue;
 
        else if( lFilePath.IsEmpty() ) lFilePath = argv[i];
    }
    if( lFilePath.IsEmpty() ) lFilePath = SAMPLE_FILENAME;
    FBXSDK_printf("Reading the FBX file...\n");
    lResult = LoadScene(lSdkManager, lScene, lFilePath.Buffer());
    if(lResult == false)
    {
        FBXSDK_printf("\n\nAn error occurred while loading the scene...\n");
        DestroySdkObjects(lSdkManager, lResult);
        return false;
    }
    
    lResult = DumpProceduralTextureBlobOnDisk(lScene);
    if(lResult == false)
    {
        FBXSDK_printf("\n\nAn error occurred while dumping procedural texture blobs on disk...\n");
        DestroySdkObjects(lSdkManager, lResult);
        return false;
    }
    
    DestroySdkObjects(lSdkManager, lResult);
    return true;
}
bool DumpProceduralTextureBlobOnDisk(
FbxScene* pScene)
 
{
    FBXSDK_printf("Writing the blob on disk...\n");
    
    if (!lNbProcTex)
    {
        return true;
    }
    bool lWroteBlob = false; 
    
    for(int lIndex = 0; lIndex < lNbProcTex; lIndex++)
    {
        if(!lProcTex)
        {
            continue;
        }
        {
            continue;
        }
        
        size_t lBlobSize = 0;
        lBlobSize = lBinaryBlob.
Size();
 
        lBlobBegin = 
const_cast<void*
>(lBinaryBlob.
Access());
 
        
        
        if (lIsWritable)
        {
            std::ofstream lDataStreamOut(lFilePath.Buffer(), std::ofstream::binary);
            lDataStreamOut.write((const char *)lBlobBegin, lBlobSize);
            lDataStreamOut.close();
            
            lWroteBlob = true;
            FBXSDK_printf("Blob is written on disk! File: %s\n", lFilePath.Buffer());
        }
    }
    return lWroteBlob;
}
{
    FBXSDK_printf("Creating the scene...\n");
    FbxNode* lPlane = CreatePlane(pScene, 
"Plane");
 
    MapPhong(lPhong, lPlane);
    MapProceduralTexure(lProcTex, lPlane);
    
    return true;
}
{
    
    lControlPoints[0] = lControlPoint0;
    lControlPoints[1] = lControlPoint1;
    lControlPoints[2] = lControlPoint2;
    lControlPoints[3] = lControlPoint3;
    
    
    
    
    
    int lPolygonVertices[] = { 0, 1, 2, 3 };
    
    FBX_ASSERT( lUVDiffuseElement != 
NULL);
 
    
    
    
    for (int j = 0; j < 4; j++)
    {
        
        
    }
    return lNode;
}
{
    
    
    
    
    
    
    if (lIsReadable)
    {
        
        std::ifstream lDataStreamIn(lFilename, std::ifstream::binary);
        char* lBlobBegin = (char*)malloc(4096);
        char* lBlobEnd = lBlobBegin;
        long long lBlobSize = 0;
        long long lBlobPointerSize = 4096;
        std::streamsize lNbRead = 0;
        while(!lDataStreamIn.eof())
        {
            lBlobEnd = lBlobBegin + lBlobSize;
            lDataStreamIn.read(lBlobEnd, 4096);
            lNbRead = lDataStreamIn.gcount();
            lBlobPointerSize += 4096;
            lBlobBegin = (char *)realloc(lBlobBegin, size_t(lBlobPointerSize));
            lBlobSize += lNbRead;
        }
        lDataStreamIn.close();
        lBinaryBlob.
Assign(lBlobBegin, (
int)lBlobSize);
 
        free(lBlobBegin);
        lProceduralTexture->
SetBlob(lBinaryBlob); 
 
    }
    
    {
        lFilenameProp.
Set(lFilename);
 
    }
    return lProceduralTexture;
}
{
    
    return lPhong;
}
{
    
    
    if (lMaterial)
    {
        lMaterial->
Diffuse.ConnectSrcObject(pProceduralTexture);
 
    }
}
{
    
    
    
}