/*=========================================================================

  Program: 3D Slicer

  Copyright (c) Laboratory for Percutaneous Surgery (PerkLab)
  Queen's University, Kingston, ON, Canada. All Rights Reserved.

  See COPYRIGHT.txt
  or http://www.slicer.org/copyright/copyright.txt for details.

  Unless required by applicable law or agreed to in writing, software
  distributed under the License is distributed on an "AS IS" BASIS,
  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  See the License for the specific language governing permissions and
  limitations under the License.

  This file was originally developed by Csaba Pinter, PerkLab, Queen's University
  and was supported through the Applied Cancer Research Unit program of Cancer Care
  Ontario with funds provided by the Ontario Ministry of Health and Long-Term Care

=========================================================================*/

// Terminology includes
#include "qSlicerTerminologySelectorButton.h"
#include "qSlicerTerminologySelectorDialog.h"
#include "vtkSlicerTerminologiesModuleLogic.h"
#include "vtkSlicerTerminologyEntry.h"

#include "qSlicerCoreApplication.h"

// Qt includes
#include <QApplication>
#include <QDebug>
#include <QPainter>
#include <QPixmap>
#include <QStyle>
#include <QStyleOptionButton>
#include <QStylePainter>

class qSlicerTerminologySelectorButtonPrivate
{
  Q_DECLARE_PUBLIC(qSlicerTerminologySelectorButton);

protected:
  qSlicerTerminologySelectorButton* const q_ptr;

public:
  qSlicerTerminologySelectorButtonPrivate(qSlicerTerminologySelectorButton& object);
  void init();
  QString text() const;

  qSlicerTerminologyNavigatorWidget::TerminologyInfoBundle TerminologyInfo;
  mutable QSize CachedSizeHint;
};

//-----------------------------------------------------------------------------
qSlicerTerminologySelectorButtonPrivate::qSlicerTerminologySelectorButtonPrivate(qSlicerTerminologySelectorButton& object)
  : q_ptr(&object)
{
}

//-----------------------------------------------------------------------------
void qSlicerTerminologySelectorButtonPrivate::init()
{
  Q_Q(qSlicerTerminologySelectorButton);
  q->setCheckable(true);
  QObject::connect(q, SIGNAL(toggled(bool)), q, SLOT(onToggled(bool)));
}

//-----------------------------------------------------------------------------
qSlicerTerminologySelectorButton::qSlicerTerminologySelectorButton(QWidget* _parent)
  : QPushButton(_parent)
  , d_ptr(new qSlicerTerminologySelectorButtonPrivate(*this))
{
  Q_D(qSlicerTerminologySelectorButton);
  d->init();
}

//-----------------------------------------------------------------------------
qSlicerTerminologySelectorButton::~qSlicerTerminologySelectorButton() = default;

//-----------------------------------------------------------------------------
void qSlicerTerminologySelectorButton::changeTerminology()
{
  Q_D(qSlicerTerminologySelectorButton);
  bool usedCustomNameOrColor = (d->TerminologyInfo.NameAutoGenerated == false || d->TerminologyInfo.ColorAutoGenerated == false);
  if (qSlicerTerminologySelectorDialog::getTerminology(d->TerminologyInfo, this))
  {
    this->update();
    emit terminologyChanged();

    bool nowUsingCustomNameOrColor = (d->TerminologyInfo.NameAutoGenerated == false || d->TerminologyInfo.ColorAutoGenerated == false);
    if (nowUsingCustomNameOrColor && !usedCustomNameOrColor)
    {
      // When terminology is selected from color table, the name and color are always set as custom (not auto-generated).
      // Do not report these as the user selected custom name or color.
      bool isTerminology = true;
      vtkSlicerTerminologiesModuleLogic* terminologiesLogic =
        qSlicerCoreApplication::application() ? vtkSlicerTerminologiesModuleLogic::SafeDownCast(qSlicerCoreApplication::application()->moduleLogic("Terminologies")) : nullptr;
      if (terminologiesLogic && d->TerminologyInfo.GetTerminologyEntry())
      {
        const char* contextName = d->TerminologyInfo.GetTerminologyEntry()->GetTerminologyContextName();
        if (contextName)
        {
          isTerminology = terminologiesLogic->IsTerminologyContextLoaded(contextName);
        }
      }
      if (isTerminology)
      {
        // not selected from color table, therefore the user manually set name or color
        emit userSetCustomNameOrColor();
      }
    }
  }
  else
  {
    emit canceled();
  }
}

//-----------------------------------------------------------------------------
void qSlicerTerminologySelectorButton::onToggled(bool change)
{
  if (change)
  {
    this->changeTerminology();
    this->setChecked(false);
  }
}

//-----------------------------------------------------------------------------
void qSlicerTerminologySelectorButton::terminologyInfo(qSlicerTerminologyNavigatorWidget::TerminologyInfoBundle& terminologyInfo)
{
  Q_D(qSlicerTerminologySelectorButton);
  terminologyInfo = d->TerminologyInfo;
}

//-----------------------------------------------------------------------------
void qSlicerTerminologySelectorButton::setTerminologyInfo(qSlicerTerminologyNavigatorWidget::TerminologyInfoBundle& terminologyInfo)
{
  Q_D(qSlicerTerminologySelectorButton);

  d->TerminologyInfo = terminologyInfo;

  this->update();
}

//-----------------------------------------------------------------------------
void qSlicerTerminologySelectorButton::paintEvent(QPaintEvent*)
{
  Q_D(qSlicerTerminologySelectorButton);
  QStylePainter p(this);
  QStyleOptionButton option;
  this->initStyleOption(&option);
  option.text = d->TerminologyInfo.Name;
  p.drawControl(QStyle::CE_PushButton, option);
}
