/////////////////////////////////////////////////////////////////////////////
// Name:        taskpanel.h
// Purpose:     wxPackageTaskQueuePanel definition
// Author:      Francesco Montorsi
// Modified by: 
// Created:     Fri 23 Jun 2006 14:45:00 CEST
// RCS-ID:      $Id: taskpanel.h,v 1.8 2007/02/01 22:00:52 frm Exp $
// Copyright:   (c) 2006 Francesco Montorsi
// Licence:     wxWidgets license
/////////////////////////////////////////////////////////////////////////////

#ifndef _PMBUILDPANEL_H_
#define _PMBUILDPANEL_H_


/*!
 * Includes
 */

#include "wx/htmllbox.h"
#include "guipm/extcmdprogresspanel.h"

/*!
 * Forward declarations
 */

class wxTaskListBox;
class wxTaskListBox;
class wxBitmapButton;

/*!
 * Control identifiers
 */

////@begin control identifiers
#define ID_TASK_QUEUED 10000
#define ID_TASK_START 10110
#define ID_TASK_STOP 10026
#define ID_TASK_DELETEQUEUED 10032
#define ID_TASK_COMPLETED 10002
#define ID_TASK_SHOWLOG 10043
#define ID_TASK_DELETECOMPLETED 10031
#define ID_TASK_CURRENT 10049
#define SYMBOL_WXPACKAGETASKQUEUEPANEL_STYLE wxCAPTION|wxRESIZE_BORDER|wxSYSTEM_MENU|wxCLOSE_BOX
#define SYMBOL_WXPACKAGETASKQUEUEPANEL_TITLE _("Task queue panel")
#define SYMBOL_WXPACKAGETASKQUEUEPANEL_IDNAME wxID_ANY
#define SYMBOL_WXPACKAGETASKQUEUEPANEL_SIZE wxSize(400, 300)
#define SYMBOL_WXPACKAGETASKQUEUEPANEL_POSITION wxDefaultPosition
////@end control identifiers

/// The size of the bitmaps returned by wxBuildPanel::GetBitmapResource
#ifdef __WXGTK20__
    #define ID_TASK_BITMAP_SIZE        wxSize(30,30)
#else
    #define ID_TASK_BITMAP_SIZE        wxSize(17,17)
#endif


// ----------------------------------------------------------------------------
// wxPackageTaskQueuePanel
// ----------------------------------------------------------------------------

class wxPackageTaskQueuePanel: public wxPanel
{    
    DECLARE_EVENT_TABLE()

public:
    /// Constructors
    wxPackageTaskQueuePanel( );
    wxPackageTaskQueuePanel( wxWindow* parent, 
                             wxWindowID id = SYMBOL_WXPACKAGETASKQUEUEPANEL_IDNAME, 
                             const wxString& caption = SYMBOL_WXPACKAGETASKQUEUEPANEL_TITLE, 
                             const wxPoint& pos = SYMBOL_WXPACKAGETASKQUEUEPANEL_POSITION, 
                             const wxSize& size = SYMBOL_WXPACKAGETASKQUEUEPANEL_SIZE, 
                             long style = SYMBOL_WXPACKAGETASKQUEUEPANEL_STYLE );

    /// Creation
    bool Create( wxWindow* parent, 
                 wxWindowID id = SYMBOL_WXPACKAGETASKQUEUEPANEL_IDNAME, 
                 const wxString& caption = SYMBOL_WXPACKAGETASKQUEUEPANEL_TITLE, 
                 const wxPoint& pos = SYMBOL_WXPACKAGETASKQUEUEPANEL_POSITION, 
                 const wxSize& size = SYMBOL_WXPACKAGETASKQUEUEPANEL_SIZE, 
                 long style = SYMBOL_WXPACKAGETASKQUEUEPANEL_STYLE );

    /// Creates the controls and sizers
    void CreateControls();

public:     // abstracts

	virtual bool CreateProgressPanel() = 0;
	virtual int GetCurrentOperationProportion() const = 0;

public:     // event handlers

    void OnIdle(wxIdleEvent &ev);
    void OnUpdateUI(wxUpdateUIEvent &ev);

    void OnProgressPanelEnd(wxCommandEvent &);
    void OnProgressPanelFailed(wxCommandEvent &);

    // buttons
    void OnStart(wxCommandEvent &ev);
    void OnStop(wxCommandEvent &);
    void OnDeleteQueuedBuild(wxCommandEvent &);
    void OnShowLog(wxCommandEvent &);
    void OnDeleteCompletedBuild(wxCommandEvent &);

public:

    void Start();
    void Stop();

    virtual void RemoveFirstQueuedPackage();

    //! Sets the event handler which will receive the wxEVT_COMMAND_PROGRESS_END
    //! events.
    void SetEventHandler(wxEvtHandler *evt)
        { m_pProgressEndHandler=evt; }


protected:      // internal utilities

    // called when a new task is going to be queued.
    virtual void OnQueueTask();

    wxBitmap GetBitmapResource( const wxString& name );
    static bool ShowToolTips();

protected:      // controls

    // our internal panel
    wxProgressPanel *m_pProgressPanel;

    wxTaskListBox *m_pQueuedList;
    wxTaskListBox *m_pCompletedList;

    // buttons in the "queued operations" static box
    wxBitmapButton *m_pStartBtn, *m_pStopBtn, *m_pDeleteQueuedBtn;

    // buttons in the "completed operations" static box
    wxBitmapButton *m_pShowLogBtn, *m_pDeleteCompletedBtn;

protected:      // other variables

    // the queue of commands to run
    wxPackageArray m_arrQueue;

    // the name of the build in progress
    wxString m_currentBuild;

    // was the queue stopped by the user ?
    bool m_bUserStopped;

    // to which item should we forward wxEVT_COMMAND_PROGRESS_END events ?
    wxEvtHandler *m_pProgressEndHandler;
};




// ----------------------------------------------------------------------------
// wxPackageDownloadQueuePanel
// ----------------------------------------------------------------------------

class wxPackageDownloadQueuePanel: public wxPackageTaskQueuePanel
{
public:
    /// Constructors
    wxPackageDownloadQueuePanel( );
    wxPackageDownloadQueuePanel( wxWindow* parent, wxWindowID id = SYMBOL_WXPACKAGETASKQUEUEPANEL_IDNAME, const wxString& caption = SYMBOL_WXPACKAGETASKQUEUEPANEL_TITLE, const wxPoint& pos = SYMBOL_WXPACKAGETASKQUEUEPANEL_POSITION, const wxSize& size = SYMBOL_WXPACKAGETASKQUEUEPANEL_SIZE, long style = SYMBOL_WXPACKAGETASKQUEUEPANEL_STYLE );

    /// Creation
    bool CreateProgressPanel();

public:

	int GetCurrentOperationProportion() const
	    { return 2;}

	void QueueDownloadOf(const wxPackage &pkg);
    void QueueDownloadOf(const wxPackageArray &arr);
};




// ----------------------------------------------------------------------------
// wxPackageCommandQueuePanel
// ----------------------------------------------------------------------------

class wxPackageCommandQueuePanel: public wxPackageTaskQueuePanel
{
public:
    /// Constructors
    wxPackageCommandQueuePanel( );
    wxPackageCommandQueuePanel( wxWindow* parent, wxWindowID id = SYMBOL_WXPACKAGETASKQUEUEPANEL_IDNAME, const wxString& caption = SYMBOL_WXPACKAGETASKQUEUEPANEL_TITLE, const wxPoint& pos = SYMBOL_WXPACKAGETASKQUEUEPANEL_POSITION, const wxSize& size = SYMBOL_WXPACKAGETASKQUEUEPANEL_SIZE, long style = SYMBOL_WXPACKAGETASKQUEUEPANEL_STYLE );

    /// Creation
    bool CreateProgressPanel();

public:
	int GetCurrentOperationProportion() const
	    { return 3;}

    void QueueBuildOf(const wxPackage &pkg);
    void QueueInstallationOf(const wxPackage &pkg);
    void QueueUninstallationOf(const wxPackage &pkg);
};



// ----------------------------------------------------------------------------
// wxTaskListBox
// ----------------------------------------------------------------------------

class wxTaskListBox : public wxHtmlListBox
{
public:
    wxTaskListBox(wxWindow* parent, wxWindowID id = wxID_ANY, 
                  const wxPoint& pos = wxDefaultPosition, 
                  const wxSize& size = wxDefaultSize, 
                  long style = 0, const wxString& name = wxT("tasklistbox"));


public:     // public API

    void Append(const wxString &item, 
                const wxString &logdata = wxEmptyString, 
                const wxString &loglabel = wxEmptyString,
                int success = -1)
    { 
        m_items.Add(item); 
        m_logs.Add(logdata); 
        m_loglabels.Add(loglabel);
        m_success.Add(success); 
        SetItemCount(m_items.GetCount()); 
    }

    void Prepend(const wxString &item, 
                 const wxString &logdata = wxEmptyString, 
                 const wxString &loglabel = wxEmptyString,
                 int success = -1)
    { 
        m_items.Insert(item, 0); 
        m_logs.Insert(logdata, 0); 
        m_loglabels.Insert(loglabel, 0);
        m_success.Insert(success, 0); 
        SetItemCount(m_items.GetCount()); 
    }

    wxString GetString(unsigned int n) const
        { return m_items[n]; }

    wxString GetLog(unsigned int n) const
        { return m_logs[n]; }
    wxString GetLogLabel(unsigned int n) const
        { return m_loglabels[n]; }

    void Delete(unsigned int n);

    // NB: call RefreshAll() after all calls to SetString !!
    void SetString(unsigned int n, const wxString &item)
        { m_items[n] = item; }

    unsigned int GetCount() const
        { return m_items.GetCount(); }

    void SetTaskInProgress(unsigned int n)
        { m_nInProgress=n; RefreshLine(n); }

protected:
    virtual wxString OnGetItem(size_t n) const;
    virtual wxString OnGetItemMarkup(size_t n) const;
    //virtual wxColour GetSelectedTextColour(const wxColour& colFg) const;
    //virtual void OnDrawSeparator(wxDC& dc, wxRect& rect, size_t n) const;

protected:
    wxArrayString m_items, m_logs, m_loglabels;
    wxArrayInt m_success;

    int m_nInProgress;

private:
    DECLARE_NO_COPY_CLASS(wxTaskListBox)
};


#endif
    // _PMBUILDPANEL_H_
