/*----------------------------------------------------------------------------*/
/*!
Copyright (c) 2018, 2019 by Christian Nowak a.k.a. Creestee a.k.a. chn
Contact: chnowak@web.de or creestee@creestee.com
All rights reserved.

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:

1. Redistributions of source code must retain the above copyright notice, this
list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.
3. This application may be used and redistributed in source code and binary
form free of charge for personal and for educational purposes.
4. Redistribution and usage of this application in source code or binary form
for commercial purposes requires explicit written permission from the
author.


THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

The views and conclusions contained in the software and documentation are those
of the authors and should not be interpreted as representing official policies,
either expressed or implied, of the RadarVolumeExplorer project.
*/
/*----------------------------------------------------------------------------*/

#include <QPushButton>
#include <QFileInfo>
#include <QCheckBox>
#include <QColorDialog>
#include <QFontDialog>
#include <QVBoxLayout>
#include <QHBoxLayout>
#include <QLabel>

#include "RveLayersGuiOverlayItemHandler.h"
#include "RveLayersGuiHandler.h"
#include "RveShapeFileSettings.h"


RveLayersGuiOverlayItemHandler::RveLayersGuiOverlayItemHandler(
                                                      RveLayersGuiHandler *pLayersGuiHandler,
                                                      QTreeWidget *pTwLayers,
                                                      QTreeWidgetItem *pTwiOverlays,
                                                      RveGisData *pGisData,
                                                      QObject *parent ) :
   QObject( parent ),
   m_pRveLayersGuiHandler( pLayersGuiHandler ),
   m_pTwLayers( pTwLayers ),
   m_pTwiOverlays( pTwiOverlays ),
   m_pGisData( pGisData ),
   m_pPbFont( 0 ),
   m_pPbColor( 0 ),
   m_pCbLabel( 0 ),
   m_pPbSettings( 0 )
{
   connect( this, SIGNAL( changed() ), m_pGisData, SIGNAL( changed() ) );

   m_pTwiOverlayItem = new QTreeWidgetItem();
   QString name = QFileInfo( pGisData->fileName() ).fileName();
   if( name.contains( "." ) )
   {
      name = name.left( name.lastIndexOf( "." ) );
   }
   m_pTwiOverlayItem->setText( 0, name );

   QPushButton *pbOverlayItemRemove = new QPushButton();
   connect( pbOverlayItemRemove, SIGNAL( clicked() ), this, SLOT( onPbRemove() ) );
   pbOverlayItemRemove->setText( "-" );
   pbOverlayItemRemove->setStyleSheet( "border: none;" );
   pbOverlayItemRemove->setSizePolicy( QSizePolicy( QSizePolicy::Minimum, QSizePolicy::Minimum ) );
   m_pTwiOverlays->addChild( m_pTwiOverlayItem );
   m_pTwLayers->setItemWidget( m_pTwiOverlayItem, 1, pbOverlayItemRemove );

   // Enable
   QTreeWidgetItem *pTwiEnable = new QTreeWidgetItem();
   pTwiEnable->setText( 0, "Enable" );
   QCheckBox *pCbEnable = new QCheckBox();
   connect( pCbEnable, SIGNAL( toggled( bool ) ), this, SLOT( onCbEnable( bool ) ) );
   pCbEnable->setChecked( true );
   m_pTwiOverlayItem->addChild( pTwiEnable );
   m_pTwLayers->setItemWidget( pTwiEnable, 1, pCbEnable );

   // Color
   QTreeWidgetItem *pTwiColor = new QTreeWidgetItem();
   pTwiColor->setText( 0, "Color" );
   m_pPbColor = new QPushButton();
   connect( m_pPbColor, SIGNAL( clicked() ), this, SLOT( onPbColor() ) );
   m_pPbColor->setAutoFillBackground( true );
   m_pPbColor->setStyleSheet(
      QString( "background-color: rgb(%1, %2, %3); color: rgb(%1, %2, %3)" )
         .arg( m_pGisData->color().red() )
         .arg( m_pGisData->color().green() )
         .arg( m_pGisData->color().blue() ) );
   m_pPbColor->setSizePolicy( QSizePolicy( QSizePolicy::Minimum, QSizePolicy::Minimum ) );
   m_pTwiOverlayItem->addChild( pTwiColor );
   m_pTwLayers->setItemWidget( pTwiColor, 1, m_pPbColor );

   if( pGisData->shpFile()->type() == RveShp::TYPE_POINT )
   {
      // Font
      QTreeWidgetItem *pTwiFont = new QTreeWidgetItem();
      pTwiFont->setText( 0, "Font" );
      QHBoxLayout *pLayout = new QHBoxLayout();
      m_pLFont = new QLabel();
      m_pLFont->setText( fontText( pGisData->font() ) );
      m_pLFont->setSizePolicy( QSizePolicy( QSizePolicy::Expanding, QSizePolicy::Minimum ) );
      pLayout->addWidget( m_pLFont );

      m_pPbFont = new QPushButton();
      connect( m_pPbFont, SIGNAL( clicked() ), this, SLOT( onPbFont() ) );
      m_pPbFont->setText( ".." );
      m_pPbFont->setSizePolicy( QSizePolicy( QSizePolicy::Minimum, QSizePolicy::Minimum ) );

      pLayout->addWidget( m_pPbFont );
      QWidget *w = new QWidget();
      w->setLayout( pLayout );
      m_pTwiOverlayItem->addChild( pTwiFont );
      m_pTwLayers->setItemWidget( pTwiFont, 1, w );

      // Label
      QTreeWidgetItem *pTwiLabel = new QTreeWidgetItem();
      pTwiLabel->setText( 0, "Label" );
      pLayout = new QHBoxLayout();
      m_pCbLabel = new QComboBox();
      QSizePolicy sp = m_pCbLabel->sizePolicy();
      sp.setHorizontalPolicy( QSizePolicy::Expanding );
      m_pCbLabel->setSizePolicy( sp );
      for( int i = 0; i < m_pGisData->dbfFile()->numFields(); i++ )
      {
         m_pCbLabel->addItem( m_pGisData->dbfFile()->field( i )->name() );
      }
      m_pCbLabel->setCurrentIndex( m_pCbLabel->findText( m_pGisData->label() ) );
      connect( m_pCbLabel, SIGNAL( currentIndexChanged( int ) ), this, SLOT( onCbLabelIndex( int ) ) );
      pLayout->addWidget( m_pCbLabel );

      // Settings
      m_pPbSettings = new QPushButton();
      m_pPbSettings->setText( ".." );
      sp = m_pPbSettings->sizePolicy();
      sp.setHorizontalPolicy( QSizePolicy::Minimum );
      m_pPbSettings->setSizePolicy( sp );
      connect( m_pPbSettings, SIGNAL( clicked() ), this, SLOT( onPbSettings() ) );
      pLayout->addWidget( m_pPbSettings );

      w = new QWidget();
      w->setLayout( pLayout );
      m_pTwiOverlayItem->addChild( pTwiLabel );
      m_pTwLayers->setItemWidget( pTwiLabel, 1, w );


      m_pTwiOverlayItem->addChild( pTwiLabel );
   } else
   {
      // Line Width
      QTreeWidgetItem *pTwiLineWidth = new QTreeWidgetItem();
      pTwiLineWidth->setText( 0, "Line Width" );
      m_pSbLineWidth = new QSpinBox();
      m_pSbLineWidth->setSuffix( "" );
      m_pSbLineWidth->setMinimum( 1 );
      m_pSbLineWidth->setMaximum( 8 );
      m_pSbLineWidth->setValue( m_pGisData->lineWidth() );
      m_pSbLineWidth->setButtonSymbols( QAbstractSpinBox::NoButtons );
      m_pTwiOverlayItem->addChild( pTwiLineWidth );
      m_pTwLayers->setItemWidget( pTwiLineWidth, 1, m_pSbLineWidth );
      connect( m_pSbLineWidth, SIGNAL( editingFinished() ), this, SLOT( onSbLineWidth() ) );
   }
}


QString RveLayersGuiOverlayItemHandler::fontText( const QFont &font )
{
   QStringList sum;

   sum.append( font.family() );
   sum.append( QString( "%1 pts" ).arg( font.pointSize() ) );
   if( font.bold() )
      sum.append( "Bold" );
   if( font.italic() )
      sum.append( "Italic" );
   if( font.underline() )
      sum.append( "Underline" );

   return( sum.join( ", " ) );
}


RveLayersGuiOverlayItemHandler::~RveLayersGuiOverlayItemHandler()
{
   delete m_pTwiOverlayItem;
}


void RveLayersGuiOverlayItemHandler::onSbLineWidth()
{
   m_pGisData->setLineWidth( m_pSbLineWidth->value() );
}


bool RveLayersGuiOverlayItemHandler::isEnabled() const
{
   return( m_pGisData->isEnabled() );
}

void RveLayersGuiOverlayItemHandler::onPbRemove()
{
   emit remove( this );
}


RveGisData *RveLayersGuiOverlayItemHandler::gisData() const
{
   return( m_pGisData );
}


void RveLayersGuiOverlayItemHandler::onPbColor()
{
   QColor color = QColorDialog::getColor(
      m_pGisData->color(),
      0,
      QString( "Pick a Color" ) );
   if( !color.isValid() )
      return;

   QString styleSheet =
      QString( "background-color: rgb(%1, %2, %3); color: rgb(%1, %2, %3)" )
         .arg( color.red() )
         .arg( color.green() )
         .arg( color.blue() );
   m_pPbColor->setStyleSheet( styleSheet );
   m_pGisData->setColor( color );
}


void RveLayersGuiOverlayItemHandler::onPbFont()
{
   bool ok;
   QFont font = QFontDialog::getFont(
      &ok,
      m_pGisData->font(),
      0,
      QString( "Pick a Font" ) );

   if( !ok )
      return;

   m_pGisData->setFont( font );
   m_pLFont->setText( fontText( m_pGisData->font() ) );
}


void RveLayersGuiOverlayItemHandler::onCbLabelIndex( int index )
{
   m_pGisData->setLabel( m_pCbLabel->itemText( index ) );
}


void RveLayersGuiOverlayItemHandler::onCbEnable( bool enable )
{
   m_pGisData->setEnabled( enable );
   m_pRveLayersGuiHandler->updateGui();
}


void RveLayersGuiOverlayItemHandler::onPbSettings()
{
   RveShapeFileSettings *pSettings = new RveShapeFileSettings( m_pGisData, m_pTwLayers );
   pSettings->show();
   int result = pSettings->exec();
   delete pSettings;

   if( result == 1 )
      emit changed();
}
