#include "requirementListPanel.h"

#include "requirementTreeModel.h"
#include "resources/resources.h"

using namespace std;
using namespace gui;
//--------------------------------------------------------------
/* Constructor */
RequirementListPanel::RequirementListPanel(wxWindow *parentWindow, dBus::Bus &bus)
  : wxPanel(parentWindow, wxID_ANY, wxDefaultPosition, wxSize(300, -1), wxSIMPLE_BORDER)
  , m_bus(bus)
{
  // Initialization

  // Creating controls
  createControls();

  // Post initialization
}
//--------------------------------------------------------------
/* Creating controls */
void RequirementListPanel::createControls()
{
  // Create the data view control
  m_dataViewCtrl = new wxDataViewCtrl(this, wxID_ANY, wxDefaultPosition, wxDefaultSize, wxSIMPLE_BORDER);
  m_dataViewCtrl->AppendTextColumn(_T("ID"), 0, wxDATAVIEW_CELL_INERT, 100, wxALIGN_LEFT);
  m_dataViewCtrl->AppendTextColumn(_("Title"), 1, wxDATAVIEW_CELL_INERT, 200, wxALIGN_LEFT);

  const auto model = new RequirementTreeModel(m_bus);
  model->loadRootRequirement();
  m_dataViewCtrl->AssociateModel(model);
  model->DecRef();     // The control now owns the model, so we can release our reference

  // Create other controls
  const auto addButton = new wxBitmapButton(this, wxID_ANY, icon_plus_24x24(), wxDefaultPosition, wxDefaultSize, wxBU_AUTODRAW | wxBORDER_NONE);
  addButton->Bind(wxEVT_BUTTON, &RequirementListPanel::on_addButtonClick, this);

  const auto reloadButton = new wxBitmapButton(this, wxID_ANY, icon_rotate_left_24x24(), wxDefaultPosition, wxDefaultSize, wxBU_AUTODRAW | wxBORDER_NONE);
  reloadButton->Bind(wxEVT_BUTTON, &RequirementListPanel::on_reloadButtonClick, this);

  // Controls positioning
  const auto buttonSizer = new wxBoxSizer(wxHORIZONTAL);
  buttonSizer->Add(addButton, wxSizerFlags(0).Border(wxALL, 5));
  buttonSizer->AddStretchSpacer();
  buttonSizer->Add(reloadButton, wxSizerFlags(0).Border(wxALL, 5));

  const auto mainSizer = new wxBoxSizer(wxVERTICAL);
  mainSizer->Add(m_dataViewCtrl, wxSizerFlags(1).Expand().Border(wxALL, 5));
  mainSizer->Add(buttonSizer, wxSizerFlags(0).Expand());

  SetSizer(mainSizer);
}
//--------------------------------------------------------------
/* Process the add requirement click event */
void RequirementListPanel::on_addButtonClick(wxCommandEvent &event)
{
}
//--------------------------------------------------------------
/* Process the reload requirements click event */
void RequirementListPanel::on_reloadButtonClick(wxCommandEvent &event)
{
  // Reload all requirements

  if (auto *model = dynamic_cast<RequirementTreeModel *>(m_dataViewCtrl->GetModel()))
    model->loadRootRequirement();
}
//--------------------------------------------------------------
