/*
 * Copyright (C) 2023, KylinSoft Co., Ltd.
 *
 * 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 3 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 <https://www.gnu.org/licenses/>.
 */
#include "titlesearchlistdelegate.h"

#include <QPainter>
#include <QPainterPath>
#include <QStyledItemDelegate>
#include <QStyle>
#include <QEvent>
#include <QDebug>
#include <QApplication>
#include "model/titlesearchmodel.h"

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

}

TitleSearchListDelegate::~TitleSearchListDelegate()
{

}

QSize TitleSearchListDelegate::sizeHint(const QStyleOptionViewItem &option, const QModelIndex &index) const
{
    return QSize(292, 48);
}

void TitleSearchListDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const
{
    if(index.isValid())
    {
        QString cityName = index.data(TitleSearchModel::CityName).toString();

        painter->save();
        //画圆角
        QRectF rect;
        rect.setX(option.rect.x());
        rect.setY(option.rect.y());
        rect.setWidth(option.rect.width());
        rect.setHeight(option.rect.height());
        const qreal radius = ViewVar::SAME_ITEMRADIUS;
        QPainterPath path;
        path.moveTo(rect.topRight() - QPointF(radius, 0));
        path.lineTo(rect.topLeft() + QPointF(radius, 0));
        path.quadTo(rect.topLeft(), rect.topLeft() + QPointF(0, radius));
        path.lineTo(rect.bottomLeft() + QPointF(0, -radius));
        path.quadTo(rect.bottomLeft(), rect.bottomLeft() + QPointF(radius, 0));
        path.lineTo(rect.bottomRight() - QPointF(radius, 0));
        path.quadTo(rect.bottomRight(), rect.bottomRight() + QPointF(0, -radius));
        path.lineTo(rect.topRight() + QPointF(0, radius));
        path.quadTo(rect.topRight(), rect.topRight() + QPointF(-radius, -0));

        if ((option.state & QStyle::State_MouseOver) || (option.state & QStyle::State_Selected)) {
            ViewVar::m_collectPen = QPen(Qt::white);
            painter->setPen(Qt::NoPen); //去边框
            //设置背景颜色
            painter->setRenderHint(QPainter::Antialiasing);
            painter->drawRoundRect(ViewVar::SEARCH_ITEMRECT);
            painter->setRenderHint(QPainter::Antialiasing);
            QColor color = qApp->palette().highlight().color();
            QBrush brush = QBrush(color);
            painter->setBrush(brush); //设置画刷形式
            painter->drawPath(path);
        } else {
            if (ViewVar::s_themeStyle) {
                ViewVar::m_collectPen = QPen(Qt::black);
            } else {
                ViewVar::m_collectPen = QPen(Qt::white);
            }
        }
        if (!(cityName == "")) {
            painter->setPen(ViewVar::m_collectPen);
            painter->setOpacity(ViewVar::SAME_OPACITY_2);
            painter->drawText(option.rect.adjusted(ViewVar::SEARCH_NAMEXL, ViewVar::SEARCH_NAMEYL,
                                                   ViewVar::SEARCH_NAMEXR, ViewVar::SEARCH_NAMEYR),
                              Qt::AlignVCenter | Qt::AlignLeft, cityName); //绘制城市名字
        }
        painter->setPen(ViewVar::m_collectPen);
        painter->restore();

    }
}

bool TitleSearchListDelegate::editorEvent(QEvent *event, QAbstractItemModel *model, const QStyleOptionViewItem &option, const QModelIndex &index) 
{
    //判断是自动画出的，没有数据的item
    if (model->data(index, Qt::UserRole).isNull()) {
        return true;
    }
    if (event->type() == QEvent::MouseButtonRelease) {
        emit changeCityFromSearch(index);
    }
    Q_UNUSED(option);
    Q_UNUSED(index);
    return true;

}
