/*
* Author: Andrew Robberts
*
* Copyright (C) 2003 Atomic Blue (info@planeshift.it, http://www.atomicblue.org)
*
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation (version 2 of the License)
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
* GNU General Public License for more details.
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
*
*/

#include <psconfig.h>

#include <csutil/xmltiny.h>
#include <iengine/engine.h>
#include <iengine/material.h>
#include <iengine/mesh.h>
#include <iengine/movable.h>
#include <iengine/sector.h>
#include <imap/loader.h>
#include <imesh/objmodel.h>
#include <csutil/flags.h>

#include "effects/pseffectobjsimpmesh.h"
#include "effects/pseffectanchor.h"
#include "util/pscssetup.h"
#include "util/log.h"

psEffectObjSimpMesh::psEffectObjSimpMesh(iView * parentView)
                   : psEffectObj(parentView)
{
}

psEffectObjSimpMesh::~psEffectObjSimpMesh()
{

}

bool psEffectObjSimpMesh::Load(iDocumentNode *node)
{

    // get the attributes
    name = "";
    materialName = "";
    fileName = "";
    csRef<iDocumentAttributeIterator> attribIter = node->GetAttributes();
    while (attribIter->HasNext())
    {
        csRef<iDocumentAttribute> attr = attribIter->Next();
        csString attrName = attr->GetName();
        attrName.Downcase();
        if (attrName == "name")
            name = attr->GetValue();
        else if (attrName == "material")
            materialName = attr->GetValue();
        else if (attrName == "file")
            fileName = csString("/this/art/effects/") + attr->GetValue();
        else if (attrName == "mesh")
            meshName = attr->GetValue();
    }
    if (name == "")
    {
        csReport(psCSSetup::object_reg, CS_REPORTER_SEVERITY_ERROR, "planeshift_effects", "Attempting to create an effect obj with no name.\n");
        return false;
    }
    
    csRef<iVFS> vfs = CS_QUERY_REGISTRY (psCSSetup::object_reg, iVFS);
    assert(vfs);
    
    if (!vfs->Exists(fileName))
    {
        csReport(psCSSetup::object_reg, CS_REPORTER_SEVERITY_ERROR, "planeshift_effects", "Attempting to create an effect obj without specifying an existing mesh file.\n");
        return false;
    }
    
    if (!psEffectObj::Load(node))
        return false;

    return PostSetup();
}

bool psEffectObjSimpMesh::Render(const csVector3 &up)
{
    static unsigned long nextUniqueID = 0;
    csString effectID = "effect_simpmesh_";
    effectID += nextUniqueID++;

    iMeshWrapper *templateMesh = engine->FindMeshObject(meshName);
    if (!templateMesh)
        return false;
    
//    csRef<iMeshObject> newObj = templateMesh->GetMeshObject()->Clone();
//    if (!newObj)
//        newObj = templateMesh->GetMeshObject();
    
    mesh = engine->CreateMeshWrapper(templateMesh->GetMeshObject(), effectID);

    // mesh has been loaded before hand
    if (!mesh)
        return false;

    // do the up vector
    objUp = up;
    csReversibleTransform rt;
    rt.LookAt(up, csVector3(1,2,0));
    matUp = rt.GetT2O();
    matBase = matUp;

    // common flags
    mesh->GetFlags().Set(CS_ENTITY_NOHITBEAM);
    mesh->SetZBufMode(zFunc);
    mesh->SetRenderPriority(priority);

    // disable culling
    mesh->GetMeshObject()->GetObjectModel()->SetPolygonMeshViscull(0);

    return true;
}

bool psEffectObjSimpMesh::Update(csTicks elapsed)
{
    if (!anchor || !anchor->IsReady()) // wait for anchor to be ready
        return true;

    if (!psEffectObj::Update(elapsed))
        return false;
    
    if (keyFrames->Length() == 0)
        return true;
    
    // do stuff here
    
    return true;
}

psEffectObj *psEffectObjSimpMesh::Clone() const
{
    psEffectObjSimpMesh *newObj = new psEffectObjSimpMesh(view);
    CloneBase(newObj);

    // simp mesh specific
    newObj->fileName = fileName;
    newObj->meshName = meshName;
    
    return newObj;
}

bool psEffectObjSimpMesh::PostSetup()
{
    CS_ASSERT("THIS COULDN'T POSSIBLY WORK!" || true);
    
    csRef<iLoader> loader = CS_QUERY_REGISTRY (psCSSetup::object_reg, iLoader);

    loader->LoadLibraryFile(fileName, region);

    /*
    // setup the material
    if (materialName != "")
    {
        iMaterialWrapper* mat = matUtil->LoadMaterial(materialName, csString("/this/art/effects/") + materialName);
        if (!mat)
        {
            csReport(psCSSetup::object_reg, CS_REPORTER_SEVERITY_ERROR, "planeshift_effects", "Couldn't find effect material: %s\n", materialName.GetData());
            return false;
        }
        facState->SetMaterialWrapper(mat);
    }
    */
    return true;
}
