/*
 * Author: Andrew Mann
 *
 * Copyright (C) 2004 PlaneShift Team (info@planeshift.it,
 * http://www.planeshift.it)
 *
 * 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 "iutil/plugin.h"
#include "iutil/objreg.h"
#include "iutil/vfs.h"
#include "ivaria/reporter.h"
#include "ivaria/stdrep.h"


#include "imap/parser.h"
#include "iengine/camera.h"
#include "iengine/sector.h"
#include "iengine/material.h"
#include "iengine/texture.h"
#include "ivideo/material.h"
#include "cstool/csview.h"
#include "imesh/thing.h"
#include "iengine/mesh.h"
#include "imesh/object.h"
#include "imesh/particles.h"
#include "igraphic/imageio.h"


#include "pv_resmgr.h"


PVResourceManager::PVResourceManager(iObjectRegistry* obj_reg)
{
    object_reg=obj_reg;
    engine=CS_QUERY_REGISTRY(object_reg,iEngine);
}


PVResourceManager::~PVResourceManager()
{

}

int PVResourceManager::Material_AddAllMaterialsInEngine()
{
    int engine_matcount,i,added_count;
    iMaterialList *engine_matlist;
    iMaterialWrapper *engine_matwrapper;
    if (!engine)
        return 0;
    engine_matlist=engine->GetMaterialList();

    if (!engine_matlist)
        return 0;
    engine_matcount=engine_matlist->GetCount();

    added_count=0;
    for (i=0;i<engine_matcount;i++)
    {
        iObject *engine_matwrap_object;
        const char *mat_name;
        PVMaterialTracker *new_tracker;

        engine_matwrapper=engine_matlist->Get(i);
        if (!engine_matwrapper)
            continue;
        engine_matwrap_object=engine_matwrapper->QueryObject();
        if (!engine_matwrap_object)
            continue;
        mat_name=engine_matwrap_object->GetName();

        if (!mat_name)
            continue;

        if (Material_FindByName(mat_name)>=0)
            continue; // Material already exists in list

        new_tracker=new PVMaterialTracker(engine_matwrapper);
        if (!new_tracker)
            continue;

        material_list.Push(new_tracker);
        added_count++;
    }
    return added_count;
}


int PVResourceManager::Material_GetMaterialCount()
{
    return material_list.Length();
}


PVMaterialTracker *PVResourceManager::Material_GetMaterialTracker(int idx)
{
    size_t id = (size_t)idx;
    if (id<0 || id >= material_list.Length())
        return NULL;
    return material_list[id];
}

int PVResourceManager::Material_FindByName(const char *name)
{
    int i,l;
    PVMaterialTracker *tracker;

    if (!name)
        return -1;

    l=material_list.Length();
    for (i=0;i<l;i++)
    {
        iObject *mat_wrap_obj;
        tracker=material_list[i];
        if (!tracker->material_wrapper)
            continue;
        
        mat_wrap_obj=tracker->material_wrapper->QueryObject();
        if (!mat_wrap_obj || !mat_wrap_obj->GetName())
            continue;
        if (!strcmp(name,mat_wrap_obj->GetName()))
            return i;
    }
    return -1;
}


bool PVResourceManager::LoadLibraryFile(const char *working_dir,const char *filename)
{
    csString oldcwd;
    csRef<iVFS> vfs = CS_QUERY_REGISTRY (object_reg, iVFS);
    if (!vfs)
    {
        csReport(object_reg,CS_REPORTER_SEVERITY_WARNING, "PVResourceManager::LoadLibraryFile()","Could not find an iVFS interface in the available plugins!");
        return false;
    }
    csRef<iLoader> loader = CS_QUERY_REGISTRY (object_reg, iLoader);
    if (!loader)
    {
        csReport(object_reg,CS_REPORTER_SEVERITY_WARNING, "PVResourceManager::LoadLibraryFile()","Could not find an iLoader interface in the available plugins!");
        return false;
    }

    oldcwd=vfs->GetCwd();
    if (working_dir)
    {
        if (!vfs->ChDir(working_dir))
        {
            csReport(object_reg,CS_REPORTER_SEVERITY_WARNING, "PVResourceManager::LoadLibraryFile()","Could not change working directory to '%s'!",working_dir);
        }
    }


    if (!loader->LoadLibraryFile(filename))
    {
        csReport(object_reg,CS_REPORTER_SEVERITY_WARNING, "PVResourceManager::LoadLibraryFile()","Failed to load library from file '%s'!",filename);
        return false;
    }
    if (working_dir)
        vfs->ChDir(oldcwd);

    engine->PrepareTextures();


    int mat_count=Material_AddAllMaterialsInEngine();

    csReport(object_reg,CS_REPORTER_SEVERITY_NOTIFY, "PVResourceManager::LoadLibraryFile()","Library file '%s' loaded.  %d new materials found.",filename,mat_count);
    
    return true;

}

PVMaterialTracker::PVMaterialTracker(csRef<iMaterialWrapper> mat_wrapper)
{
    material_wrapper = mat_wrapper;
}
    
