/*
* 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 "pv_resmgr.h"
#include "csutil/cscolor.h"
#include "imesh/particles.h"

#include "paws/pawsmanager.h"
#include "paws/pawsbutton.h"
#include "paws/pawstextbox.h"
#include "paws/pawsmainwidget.h"

#include "paws/pawsfilenavigation.h"

#include "pawsscrollpane.h"

#include "partview.h"

#include "gui_particlesystem.h"
#include "gui_pslist.h"


guiPSList::guiPSList(PawsManager* manager )
            :pawsWidget( manager )
{
    scrollpane_pslist=NULL;
    button_loadsystem=NULL;
    button_newsystem=NULL;
    window_filenav=NULL;
    load_outstanding=false;
    newsystem_count=0;
    awaiting_save=NULL;
}

guiPSList::~guiPSList()
{
}



bool guiPSList::PostSetup()
{
    scrollpane_pslist=(pawsScrollPane *)FindWidget("pslist_scrollpane");
    button_loadsystem=(pawsButton *)FindWidget("pslist_load_system_button");
    button_newsystem=(pawsButton *)FindWidget("pslist_add_system_button");

    // Load file navigation widget
    csArray<pawsWidget *> loadedwidgets;
    windowManager->LoadChildWidgets("data/gui/filenavigation.xml",loadedwidgets);
    if (loadedwidgets.Length() != 1)
    {
        int i;
        for (i=0;i<loadedwidgets.Length();i++)
            delete loadedwidgets[i];
    }
    else
    {
        window_filenav=(pawsFileNavigation *)(loadedwidgets[0]);
        if (window_filenav)
        {
            window_filenav->Hide();

            windowManager->GetMainWidget()->AddChild(window_filenav);

            window_filenav->SetActionButtonText("Load");
            window_filenav->SetName("MenuBarFileNav");
            window_filenav->MoveTo(200,50);
        }
    }

    return true;
}

bool guiPSList::OnButtonPressed(int mouseButton, int keyModifier, pawsWidget* widget)
{
    if (!widget)
        return false;
    if (widget == button_loadsystem)
    {
        // Need a file nav window
        if (!window_filenav)
            return false;
        // Make sure there isnt a load/save prompt already open
        if (load_outstanding || awaiting_save)
            return false;
        load_outstanding=true;
        window_filenav->SetActionButtonText("Load");
        window_filenav->FillFileList();
        window_filenav->Show();
        return true;
    }
    if (widget == button_newsystem)
    {
        char systemname[32];
        sprintf(systemname,"New System%d",newsystem_count);
        newsystem_count++;

        // Create a new particle system 1 unit from the camera with defaults
        PartViewApp::g_pApp->NewParticleSystemAtCamera(systemname);

        // Clear focus widget
        windowManager->SetCurrentFocusedWidget(NULL);

        return true;
    }


    if (parent)
        return parent->OnButtonPressed(mouseButton,keyModifier,widget);
    return false;
}

bool guiPSList::PromptForSave(guiParticleSystem *save_system)
{
    if (!window_filenav)
        return false;
    if (load_outstanding || awaiting_save)
        return false;
    awaiting_save=save_system;
    window_filenav->SetActionButtonText("Save");
    window_filenav->FillFileList();
    window_filenav->Show();
    return true;
}

bool guiPSList::AddParticleSystemInterface(PartSystemTracker *system)
{
    if (!scrollpane_pslist)
        return false;

    csArray<pawsWidget *> loadedwidgets;
    guiParticleSystem *psystem;

    windowManager->LoadChildWidgets("/data/partview/pscollapsable.xml",loadedwidgets);
    if (loadedwidgets.Length() != 1)
    {
        int i;
        for (i=0;i<loadedwidgets.Length();i++)
            delete loadedwidgets[i];
        return false;
    }
    psystem=(guiParticleSystem *)(loadedwidgets[0]);
    
    scrollpane_pslist->AddChild(psystem);
    psystem->InitializeParticleSystem(system,PartViewApp::g_pApp->GetResourceManager(),this);

    particle_systems.Push(psystem);

    // Clear focus widget
    windowManager->SetCurrentFocusedWidget(NULL);

    return true;
}

bool guiPSList::RemoveParticleSystemInterface(PartSystemTracker *system)
{
    int i,l;

    l=particle_systems.Length();
    for (i=0;i<l;i++)
    {
        guiParticleSystem *psystem=particle_systems[i];
        if (psystem->GetParticleSystemTracker() == system)
        {
            particle_systems.DeleteIndex(i);
            delete psystem;
            return true;
        }
    }
    return false;
}

void guiPSList::RefreshMaterialLists()
{
    int i,l;
    l=particle_systems.Length();

    for (i=0;i<l;i++)
        particle_systems[i]->MatchMaterial();
}


void guiPSList::DoTimedChecks(csTicks elapsed)
{
    int i,l;
    l=particle_systems.Length();

    for (i=0;i<l;i++)
    {
        particle_systems[i]->MatchSystemRunningState();
        particle_systems[i]->UpdatePathPosition(elapsed);
    }

    if (load_outstanding || awaiting_save)
    {
        if (window_filenav)
        {
            switch (window_filenav->GetSelectionState())
            {
                case PAWSFILENAV_SELECTION_CANCEL:
                    window_filenav->Hide();
                    load_outstanding=false;
                    awaiting_save=NULL;
                break;
                case PAWSFILENAV_SELECTION_PERFORM:
                    if (awaiting_save)
                    {
                        // Save
                        if (PartViewApp::g_pApp)
                            PartViewApp::g_pApp->SaveParticleSystem(awaiting_save->GetParticleSystemTracker(),window_filenav->GetFullPathFilename());
                    }
                    else
                    {
                        // Load
                        if (PartViewApp::g_pApp)
                            PartViewApp::g_pApp->LoadParticleSystem(window_filenav->GetFullPathFilename());
                    }
                    window_filenav->Hide();
                    load_outstanding=false;
                    awaiting_save=NULL;
                break;
            }
        }
    }

}





