#include "companylistitemdelegate.h"
#include "mapplication.h"

CompanyListItemDelegate::CompanyListItemDelegate(QObject *parent) :
    QStyledItemDelegate(parent)
{

}

void CompanyListItemDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const
{
    paintPerson(painter, option, index);
}

QSize CompanyListItemDelegate::sizeHint(const QStyleOptionViewItem &option, const QModelIndex &index) const
{
    return QSize(option.rect.width(), 90);
}

void CompanyListItemDelegate::paintPerson(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const
{
    const bool isSelected = option.state & QStyle::State_Selected;
    const bool isHovered = option.state & QStyle::State_MouseOver;


    painter->save();

    /*
    QLinearGradient backgroundGradient(QPoint(option.rect.x(), option.rect.y()), QPoint(option.rect.x(), option.rect.y()+option.rect.height()));

    if(isSelected)
    {
        //        painter->fillRect(option.rect, QBrush(QColor(49, 49, 49)));
        backgroundGradient.setColorAt(0, QColor(109, 164, 219));
        backgroundGradient.setColorAt(1, QColor(61, 138, 212));
        painter->fillRect(option.rect, QBrush(backgroundGradient));
        //        painter->fillRect(option.rect, QBrush(QColor(225, 225, 225)));

    }
    else
    {
        //        painter->fillRect(option.rect, QBrush(QColor(244, 244, 244)));
        backgroundGradient.setColorAt(0, QColor(245, 245, 245));
        backgroundGradient.setColorAt(1, QColor(240, 240, 240));
        painter->fillRect(option.rect, QBrush(backgroundGradient));
    }

    painter->setPen(QColor(225, 225, 225));
    if(isSelected)
    {
        painter->setPen(QColor(37, 105, 169));
        painter->drawLine(option.rect.bottomLeft(), option.rect.bottomRight());
        painter->setPen(Qt::transparent);
    }
    painter->drawLine(option.rect.topLeft(), option.rect.topRight());

    if(!isSelected)
    {
        painter->setPen(QColor(248, 248, 248));
        painter->drawLine(QPoint(option.rect.x(), option.rect.y()+1), QPoint(option.rect.x()+option.rect.width(), option.rect.y()+1));
    }
    //    QString text = index.model()->data(index, Qt::DisplayRole).toString();
    QRect textRect(option.rect.x()+10, option.rect.y(), option.rect.width()-10, option.rect.height());
    painter->setPen(QColor(69, 69, 69));
    QFont textFont(painter->font());
    textFont.setPixelSize(18);

    if(isSelected)
    {
        painter->setPen(QColor(229, 229, 229));
    }

    painter->setFont(textFont);
    painter->drawText(textRect, Qt::AlignLeft|Qt::AlignVCenter, tp.name());
    */
//-------------------------------------------------------------------------------------------
    /*
    QFont font = QApplication::font();
    QFont SubFont = QApplication::font();
    //font.setPixelSize(font.weight()+);
    font.setBold(true);
    SubFont.setWeight(SubFont.weight()-2);
    QFontMetrics fm(font);

    //item->setData(Name, Qt::DisplayRole);
    //item->setData(CIF,  Qt::UserRole);
    //item->setData(path, Qt::UserRole+1);

    //QIcon icon = qvariant_cast<QIcon>(index.data(Qt::EditRole));
    QString headerText = qvariant_cast<QString>(index.data(Qt::DisplayRole));
    QString subText = qvariant_cast<QString>(index.data(Qt::DisplayRole + 1));
    //QSize iconsize = icon.actualSize(option.decorationSize);


    QRect headerRect = option.rect;
    QRect subheaderRect = option.rect;
    QRect iconRect = subheaderRect;

    iconRect.setRight(iconsize.width()+30);
    iconRect.setTop(iconRect.top()+5);
    headerRect.setLeft(iconRect.right());
    subheaderRect.setLeft(iconRect.right());
    headerRect.setTop(headerRect.top()+5);
    headerRect.setBottom(headerRect.top()+fm.height());

    subheaderRect.setTop(headerRect.bottom()+2);


    //painter->drawPixmap(QPoint(iconRect.right()/2,iconRect.top()/2),icon.pixmap(iconsize.width(),iconsize.height()));
    painter->drawPixmap(QPoint(iconRect.left()+iconsize.width()/2+2,iconRect.top()+iconsize.height()/2+3),icon.pixmap(iconsize.width(),iconsize.height()));

    painter->setFont(font);
    painter->drawText(headerRect,headerText);


    painter->setFont(SubFont);
    painter->drawText(subheaderRect.left(),subheaderRect.top()+17,subText);
    */



    QLinearGradient backgroundGradient(QPoint(option.rect.x(), option.rect.y()), QPoint(option.rect.x(), option.rect.y()+option.rect.height()));

    if(isSelected)
    {
        //        painter->fillRect(option.rect, QBrush(QColor(49, 49, 49)));
        backgroundGradient.setColorAt(0, QColor(109, 164, 219));
        backgroundGradient.setColorAt(1, QColor(61, 138, 212));
        painter->fillRect(option.rect, QBrush(backgroundGradient));
        //        painter->fillRect(option.rect, QBrush(QColor(225, 225, 225)));

    }
    else
    {
        //        painter->fillRect(option.rect, QBrush(QColor(244, 244, 244)));
        backgroundGradient.setColorAt(0, QColor(245, 245, 245));
        backgroundGradient.setColorAt(1, QColor(240, 240, 240));
        painter->fillRect(option.rect, QBrush(backgroundGradient));
    }



    QString name = qvariant_cast<QString>(index.data(Qt::DisplayRole));
    QString cif  = qvariant_cast<QString>(index.data(Qt::UserRole));
    QString path = qvariant_cast<QString>(index.data(Qt::UserRole + 1));

    //painter->setFont(textFont);
    QRect nameRect(option.rect.x() + 100, option.rect.y() + 10,
                   option.rect.width() - 10, option.rect.height() / 2 - 20);

    QRect cifRect(option.rect.x() + 100, option.rect.y() +option.rect.height() / 2 + 10,
                   option.rect.width() - 10, option.rect.height() / 2 - 20);

    painter->drawText(nameRect, Qt::AlignLeft|Qt::AlignVCenter, name);
    painter->drawText(cifRect, Qt::AlignLeft|Qt::AlignVCenter, cif);

    painter->restore();
}
