//---------------------------------------------------------
wxString CWKSP_Module_Group::Get_Description(void)
{
	wxString	s;

	s	+= wxString::Format("<h4>%s</h4>", _TL("Tool Libraries"));

	s	+= "<table border=\"0\">";

	s	+= SUMMARY_ADD_STR(_TL("Category" ), Get_Name().c_str());
	s	+= SUMMARY_ADD_INT(_TL("Libraries"), Get_Count());

	s	+= "</table>";

	//-----------------------------------------------------
	s	+= wxString::Format("<hr><h4>%s</h4><table border=\"0\">", _TL("Tool Libraries"));

	s	+= wxString::Format("<tr align=\"left\"><th>%s</th><th>%s</th><th>%s</th></tr>",
			_TL("Library"),
			_TL("Name"   ),
			_TL("Tools"  )
		);

	for(int i=0; i<Get_Count(); i++)
	{
		s	+= wxString::Format("<tr><td valign=\"top\"><i>%s</i></td><td valign=\"top\">%s</td><td valign=\"top\" align=\"right\">%d</td></tr>",
				Get_Library(i)->Get_Library()->Get_Library_Name().c_str(),
				Get_Library(i)->Get_Name().c_str(),
				Get_Library(i)->Get_Count()
			);
	}

	s	+= "</table>";

	return( s );
}
//---------------------------------------------------------
CWKSP_Module_Library * CWKSP_Module_Group::Get_Library(CSG_Module_Library *pLibrary)
{
	for(int i=0; i<Get_Count(); i++)
	{
		if( pLibrary == Get_Library(i)->Get_Library() )
		{
			return( Get_Library(i) );
		}
	}

	return( NULL );
}
//---------------------------------------------------------
bool CWKSP_Module_Manager::_Update(bool bSyncToCtrl)
{
	if( !bSyncToCtrl )
	{
	//	Get_Control()->Freeze();

		for(int i=SG_Get_Module_Library_Manager().Get_Count()-1; i>=0; i--)
		{
			CSG_Module_Library	*pLibrary	= SG_Get_Module_Library_Manager().Get_Library(i);
			CWKSP_Module_Library	*pItem	= Get_Library(pLibrary);

			if( !pItem )
			{
				CWKSP_Module_Group	*pGroup	= Get_Group(pLibrary->Get_Category().c_str());

				if( !pGroup )
				{
					Add_Item(pGroup = new CWKSP_Module_Group(pLibrary->Get_Category().c_str()));
				}

				pGroup->Add_Library(pLibrary);
			}
			else if( pItem->Get_Library()->Get_Type() == MODULE_CHAINS )
			{
				pItem->Update();
			}
		}

	//	Get_Control()->Thaw();
	}

	m_pMenu_Modules->Update();

	return( true );
}
//---------------------------------------------------------
bool CWKSP_Module_Manager::Exists(CWKSP_Module *pModule)
{
    for(int i=0; i<Get_Count(); i++)
    {
        if( Get_Library(i)->Exists(pModule) )
        {
            return( true );
        }
    }

    return( false );
}
//---------------------------------------------------------
void CWKSP_Module_Manager::_Config_Write(void)
{
    CONFIG_Write(wxT("/MODULES")	, wxT("BEEP")		,		m_Parameters("BEEP")		->asBool());
    CONFIG_Write(wxT("/MODULES")	, wxT("START_LOGO")	, (long)m_Parameters("START_LOGO")	->asInt());
    CONFIG_Write(wxT("/MODULES")	, wxT("PROC_FREQ")	, (long)m_Parameters("PROC_FREQ")	->asInt());

    CONFIG_Delete(CFG_LIBS);

    for(int i=0; i<Get_Count(); i++)
    {
        CONFIG_Write(CFG_LIBS, wxString::Format(CFG_LIBF, i), Get_Library(i)->Get_File_Name());
    }
}
//---------------------------------------------------------
CWKSP_Module * CWKSP_Module_Manager::Get_Module_byID(int CMD_ID)
{
    CWKSP_Module	*pModule;

    for(int i=0; i<Get_Count(); i++)
    {
        if( (pModule = Get_Library(i)->Get_Module_byID(CMD_ID)) != NULL )
        {
            return( pModule );
        }
    }

    return( NULL );
}
//---------------------------------------------------------
bool CWKSP_Module_Manager::Open(const wxChar *File_Name)
{
    CWKSP_Module_Library	*pLibrary;

    //-----------------------------------------------------
    if( SG_File_Cmp_Extension(File_Name, wxT("mlb"))
            ||	SG_File_Cmp_Extension(File_Name, wxT("dll"))
            ||	SG_File_Cmp_Extension(File_Name, wxT("so" )) )
    {
        MSG_General_Add(wxString::Format(wxT("%s: %s..."), LNG("[MSG] Load library"), File_Name), true, true);

        //-------------------------------------------------
        for(int i=0; i<Get_Count(); i++)
        {
            if( SG_STR_CMP(File_Name, Get_Library(i)->Get_File_Name()) == 0 )
            {
                MSG_Error_Add(wxString::Format(wxT("%s\n%s"), File_Name, LNG("[ERR] Library has already been loaded")), true);
                MSG_General_Add(LNG("[MSG] has already been loaded"), false);

                return( false );
            }
        }

        //-------------------------------------------------
        pLibrary	= new CWKSP_Module_Library(File_Name);

        if( pLibrary->is_Valid() )
        {
            Add_Item(pLibrary);

            MSG_General_Add(LNG("[MSG] okay"), false, false, SG_UI_MSG_STYLE_SUCCESS);

            return( true );
        }

        delete(pLibrary);

        MSG_General_Add(LNG("[MSG] failed"), false, false, SG_UI_MSG_STYLE_FAILURE);
    }

    return( false );
}
//---------------------------------------------------------
void CWKSP_Module_Manager::_Make_HTML_Docs(void)
{
    CSG_Parameters	Options(NULL, LNG("Create HTML Documentation"), LNG(""));

    Options.Add_FilePath(NULL, "DIR", LNG("Choose Documentation Folder"), LNG(""), NULL, NULL, true, true);

    if( !DLG_Parameters(&Options) )
    {
        return;
    }

    //-----------------------------------------------------
    bool			bDirectory;
    CSG_File		Stream_Module, Stream_Lib, Stream_Libs, Stream_List;
    wxString		LibName, Directory, Main, s;
    wxFileName		FileName;

    MSG_General_Add(wxString::Format(wxT("%s..."), LNG("Creating module documentation files")), true, true);

    bDirectory	= wxDirExists(Options("DIR")->asString());
    Directory	= bDirectory ? Options("DIR")->asString() : SG_File_Get_Path(g_pSAGA->Get_App_Path()).c_str();

    //-----------------------------------------------------
    FileName.AssignDir	(Directory);
    FileName.SetExt		(wxT("html"));
    FileName.SetName	(wxT("index"));

    Stream_Libs.Open(FileName.GetFullPath().c_str(), SG_FILE_W, false);
    Stream_Libs.Printf(SG_T("<html><head><title>SAGA - System for Automated Geoscientific Analyses</title></head><body>"));
    Stream_Libs.Printf(SG_T("<h1><a href=\"http://www.saga-gis.org\">SAGA - System for Automated Geoscientific Analyses</a></h1>"));
    Stream_Libs.Printf(SG_T("<h2>%s</h2>\n<ul>\n"), LNG("Module Library Descriptions"));

    Main		= FileName.GetFullPath();

    //-----------------------------------------------------
    for(int i=0; i<Get_Count() && PROGRESSBAR_Set_Position(i, Get_Count()); i++)
    {
        LibName				= SG_File_Get_Name(Get_Library(i)->Get_File_Name(), false).c_str();
        FileName.AssignDir	(bDirectory ? Directory.c_str() : SG_File_Get_Path(Get_Library(i)->Get_File_Name()).c_str());
        FileName.AppendDir	(LibName);
        FileName.SetExt		(wxT("html"));

        if( wxDirExists(FileName.GetPath()) || wxMkdir(FileName.GetPath()) )
        {
            //---------------------------------------------
            // create a frame

            FileName.SetName(wxT("index"));

            if( Stream_Lib.Open(FileName.GetFullPath().c_str(), SG_FILE_W, false) )
            {
                if( Stream_Libs.is_Open() )
                {
                    s	= Get_FilePath_Relative(Directory.c_str(), FileName.GetFullPath().c_str()).c_str();
                    if( s[0] == '\\' )	s	= s.AfterFirst('\\');
                    if(s[0]=='/') s = s.AfterFirst('/');
                    Stream_Libs.Printf(wxT("<li><a href=\"%s\">%s</a></li>\n"), s.c_str(), Get_Library(i)->Get_Name().c_str());
                }

                Stream_Lib.Printf(SG_T("<html><head><title>SAGA - System for Automated Geoscientific Analyses</title></head>"));
                Stream_Lib.Printf(SG_T("<frameset cols=\"200,*\" frameborder=\"0\" framespacing=\"0\" border=\"0\">"));
                Stream_Lib.Printf(SG_T("  <frame frameborder=\"0\" noresize src=\"modules.html\" name=\"MODULES\">"));
                Stream_Lib.Printf(SG_T("  <frame frameborder=\"0\" noresize src=\"%s.html\" name=\"CONTENT\">")   , LibName.c_str());
                Stream_Lib.Printf(SG_T("</frameset></html>"));
            }

            //---------------------------------------------
            // write the modules

            if( bDirectory )
                s	= wxT("./../index");
            else
                s	= Get_FilePath_Relative(Main.c_str(), FileName.GetFullPath().c_str()).c_str();
            if( s[0] == '\\' )
                s = s.AfterFirst('\\');
            if(s[0]=='/')
                s = s.AfterFirst('/');

            FileName.SetName(wxT("modules"));
            Stream_List.Open(FileName.GetFullPath().c_str(), SG_FILE_W, false);
            Stream_List.Printf(SG_T("<body bgcolor=\"#CCCCCC\">"));
            Stream_List.Printf(SG_T("<b><a target=\"_top\"    href=\"http://www.saga-gis.org\">SAGA</a></b><hr>"));
            Stream_List.Printf(SG_T("<b><a target=\"_top\"    href=\"%s.html\">%s</a></b><hr>"), s.c_str(), LNG("Library Overview"));
            Stream_List.Printf(SG_T("<b><a target=\"CONTENT\" href=\"%s.html\">%s</a></b><hr><ul>"), LibName.c_str(), Get_Library(i)->Get_Name().c_str());

            FileName.SetName(LibName);

            if( Stream_Lib.Open(FileName.GetFullPath().c_str(), SG_FILE_W, false) )
            {
                Stream_Lib.Printf(wxT("%s<hr><ul>"), Get_Library(i)->Get_Description().c_str());

                for(int j=0; j<Get_Library(i)->Get_Count(); j++)
                {
                    FileName.SetName(wxString::Format(wxT("%s_%02d"), LibName.c_str(), Get_Library(i)->Get_Module(j)->Get_Index()));

                    if( Stream_Module.Open(FileName.GetFullPath().c_str(), SG_FILE_W, false) )
                    {
                        Stream_Module.Printf(wxT("%s"), Get_Library(i)->Get_Module(j)->Get_Description().c_str());

                        Stream_Lib .Printf(wxT("<li><a target=\"CONTENT\" href=\"%s\">%s</a></li>"), FileName.GetFullName().c_str(), Get_Library(i)->Get_Module(j)->Get_Name().c_str());
                        Stream_List.Printf(wxT("<li><a target=\"CONTENT\" href=\"%s\">%s</a></li>"), FileName.GetFullName().c_str(), Get_Library(i)->Get_Module(j)->Get_Name().c_str());
                    }
                }
            }
        }
    }

    //-----------------------------------------------------
    if( Stream_Libs.is_Open() )
    {
        Stream_Libs.Printf(wxT("</ul>"));
    }

    PROCESS_Set_Okay(true);

    MSG_General_Add(LNG("okay"), false, false, SG_UI_MSG_STYLE_SUCCESS);
}