/***************************************************************************
 *   Project TupiTube Desk                                                 *
 *   Project Contact: info@tupitube.com                                    *
 *   Project Website: http://www.tupitube.com                              * 
 *                                                                         *
 *   Developers:                                                           *
 *   2025:                                                                 *
 *    Utopian Lab Development Team                                         *
 *   2010:                                                                 *
 *    Gustav Gonzalez                                                      *
 *   ---                                                                   *
 *   KTooN's versions:                                                     *
 *   2006:                                                                 *
 *    David Cuadrado                                                       *
 *    Jorge Cuadrado                                                       *
 *   2003:                                                                 *
 *    Fernado Roldan                                                       *
 *    Simena Dinas                                                         *
 *                                                                         *
 *   License:                                                              *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 *   This program is distributed in the hope that it will be useful,       *
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
 *   GNU General Public License for more details.                          *
 *                                                                         *
 *   You should have received a copy of the GNU General Public License     *
 *   along with this program.  If not, see <http://www.gnu.org/licenses/>. *
 ***************************************************************************/

#include "tuplayerindex.h"

// const int LAYER_COLUMN = 0;

// Header

class TUPITUBE_EXPORT TupLayerIndexHeader : public QHeaderView
{
    public:
        TupLayerIndexHeader(QWidget * parent = nullptr);
        ~TupLayerIndexHeader();
        void paintSection(QPainter *painter, const QRect & rect, int logicalIndex) const;

    /*         
    private:
        QPixmap m_lockIcon;
        QPixmap m_viewIcon;
    */
};

TupLayerIndexHeader::TupLayerIndexHeader(QWidget * parent) : QHeaderView(Qt::Horizontal, parent)
{
    // setClickable(true);
    setCascadingSectionResizes(true);
    // setMaximumHeight(20);
    setFixedHeight(26);
}

TupLayerIndexHeader::~TupLayerIndexHeader()
{
}

void TupLayerIndexHeader::paintSection(QPainter * painter, const QRect & rect, int logicalIndex) const
{
    if (!rect.isValid()) 
        return;
    
    QStyleOptionHeader headerOption;
    headerOption.rect = rect;
    headerOption.orientation = Qt::Horizontal;
    headerOption.position = QStyleOptionHeader::Middle;
    
    QStyle::State state = QStyle::State_None;

    if (isEnabled())
        state |= QStyle::State_Enabled;

    if (window()->isActiveWindow())
        state |= QStyle::State_Active;
    
    style()->drawControl(QStyle::CE_HeaderSection, &headerOption, painter);
    
    // painter->drawRect(rect.normalized().adjusted(0, 1, 0, -1));
    
    QString text = model()->headerData(logicalIndex, orientation(), Qt::DisplayRole).toString();;
    
    // QFontMetrics fm(painter->font());
    QFont font = this->font();
    font.setPointSize(9);
    font.setBold(true);
    // QFont label("Arial", 9, QFont::Bold, false); 
    QFontMetrics fm(font);
    
    int x = rect.x() + (sectionSize(logicalIndex) - fm.width( text ))/2;
    // int y = fm.height() + (rect.y() / 2);
    int y = 17;
    painter->setFont(font); 
    painter->drawText(x, y, text);
}

// DELEGATE

class TupLayerIndexItemDelegate : public QItemDelegate
{
    public:
        TupLayerIndexItemDelegate(QObject * parent = nullptr);
        ~TupLayerIndexItemDelegate();
        virtual void paint(QPainter * painter, const QStyleOptionViewItem & option, const QModelIndex & index) const;
};

TupLayerIndexItemDelegate::TupLayerIndexItemDelegate(QObject * parent) :  QItemDelegate(parent)
{
}

TupLayerIndexItemDelegate::~TupLayerIndexItemDelegate()
{
}

void TupLayerIndexItemDelegate::paint(QPainter *painter, const QStyleOptionViewItem & option, const QModelIndex & index) const
{
    QItemDelegate::paint(painter, option, index);

    TupLayerIndex *table = qobject_cast<TupLayerIndex *>(index.model()->parent());
    QTableWidgetItem *item = table->itemFromIndex(index);

    if (item) {
        if (item->isSelected()) {
            painter->setPen(QPen(QColor(255, 190, 31, 255), 1));
            painter->drawRect(option.rect.normalized().adjusted(1,1,-2, -2));
        } 
    }
}

////////////////////////////////

TupLayerIndex::TupLayerIndex(int index, QWidget *parent) : QTableWidget(0, 1, parent)
{
    #ifdef TUP_DEBUG
        #ifdef Q_OS_WIN
            qDebug() << "[TupLayerIndex()]";
        #else
            TINIT;
        #endif
    #endif

    sceneIndex = index;
    setSelectionMode(QAbstractItemView::SingleSelection);
    setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
    setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff); 
    
    QTableWidgetItem *prototype = new QTableWidgetItem;
    prototype->setTextAlignment(Qt::AlignCenter);
    prototype->setBackgroundColor(palette().text().color());
    prototype->setTextColor(palette().background().color());
    
    setItemPrototype(prototype);
    
    setHorizontalHeaderLabels(QStringList() << tr("Layers"));
    verticalHeader()->hide();
    
    setHorizontalHeader(new TupLayerIndexHeader(this));
    setItemDelegate(new TupLayerIndexItemDelegate(this));

    connect(this, SIGNAL(cellClicked(int, int)), this, SLOT(setLocalRequest(int, int)));
}

TupLayerIndex::~TupLayerIndex()
{
    #ifdef TUP_DEBUG
        #ifdef Q_OS_WIN
            qDebug() << "[~TupLayerIndex()]";
        #else
            TEND;
        #endif
    #endif
}

void TupLayerIndex::insertLayer(int position, const QString &name)
{
    if (position >= 0 && position <= rowCount()) {
        QTableWidgetItem *newLayer = new QTableWidgetItem(name);

        QFont font = this->font();
        font.setPointSize(8);
        newLayer->setFont(font);
        // newLayer->setFont(QFont("Arial", 8, QFont::Normal, false));
        newLayer->setTextAlignment(Qt::AlignCenter);
        
        newLayer->setBackgroundColor(palette().background().color());
        newLayer->setTextColor(palette().foreground().color());
        
        insertRow(position);

        setItem(position, 0, newLayer);
        fixSize();
    }
}

void TupLayerIndex::insertSoundLayer(int position, const QString &name)
{
    if (position >= 0 && position <= rowCount()) {
        QTableWidgetItem *newLayer = new QTableWidgetItem(name);
        newLayer->setTextAlignment(Qt::AlignCenter);
        
        newLayer->setBackgroundColor(palette().background().color());
        newLayer->setTextColor(palette().foreground().color());
        
        insertRow(position);
        setItem(position, 0, newLayer);
        fixSize();
    }
}

void TupLayerIndex::removeLayer(int position)
{
    removeRow(verticalHeader()->logicalIndex(position));
}

void TupLayerIndex::renameLayer(int position, const QString &name)
{
    QTableWidgetItem *item = this->item(0, verticalHeader()->logicalIndex(position));
    
    if (item)
        item->setText(name);
}

void TupLayerIndex::resizeEvent(QResizeEvent *)
{
    fixSize();
}

void TupLayerIndex::fixSize()
{
    horizontalHeader()->resizeSection(0, 196);

    for (int row = 0; row < rowCount(); row++)
         verticalHeader()->resizeSection(row, rowHeight);
}

void TupLayerIndex::setRowHeight(int rowHeight)
{
    rowHeight = rowHeight;
}

void TupLayerIndex::commitData(QWidget *editor)
{
    QLineEdit *lineEdit = qobject_cast<QLineEdit *>(editor);
    
    QTableWidget::commitData(0); // Don't rename
    
    if (lineEdit)
        emit requestRenameEvent(verticalHeader()->visualIndex(currentRow()), lineEdit->text());
}

void TupLayerIndex::moveLayer(int position, int newPosition)
{
    if (position < 0 || position >= rowCount() || newPosition < 0 || newPosition >= rowCount()) 
        return;
    
    verticalHeader()->moveSection((position), (newPosition));
}

void TupLayerIndex::lockLayer(int position, bool locked)
{
    Q_UNUSED(locked);

    if (position < 0 || position >= rowCount()) 
        return;
}

void TupLayerIndex::setLocalRequest(int layer, int column)
{
    Q_UNUSED(layer);
    Q_UNUSED(column);

    emit localRequest();
}

void TupLayerIndex::keyPressEvent(QKeyEvent *event)
{
    int row = currentRow();
    int total = rowCount();

    if (event->key() == Qt::Key_Up) {
        if (row > 0) {
            setCurrentCell(row - 1, 0);
            emit localRequest();
        }
    }

    if (event->key() == Qt::Key_Down) {
        if (row < total-1) {
            setCurrentCell(row + 1, 0);
            emit localRequest();
        }
    }
}

void TupLayerIndex::refresh()
{
    // int w = width();
    // int h = height();
    // tFatal() << "Updating TupLayerIndex interface - Size: " << w << " : " << h;
    // repaint(0, 0, width(), height());
}
