/*
 * Fooyin
 * Copyright © 2023, Luke Taylor <luket@pm.me>
 *
 * Fooyin 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.
 *
 * Fooyin 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 Fooyin.  If not, see <http://www.gnu.org/licenses/>.
 *
 */

#pragma once

#include "filteritem.h"

#include <QStyledItemDelegate>

namespace Fooyin::Filters {
struct IconItemLayoutMetrics;

class FilterDelegate : public QStyledItemDelegate
{
    Q_OBJECT

public:
    using QStyledItemDelegate::QStyledItemDelegate;

    void paint(QPainter* painter, const QStyleOptionViewItem& option, const QModelIndex& index) const override;
    [[nodiscard]] QSize sizeHint(const QStyleOptionViewItem& option, const QModelIndex& index) const override;

private:
    static RichText recolourRichText(RichText richText, const QColor& colour);

    static QRect iconTextRect(const QStyleOptionViewItem& option);
    static int iconTextWidth(const QStyleOptionViewItem& option);

    [[nodiscard]] static IconCaptionLineList iconRichLines(const QModelIndex& index,
                                                           const QStyleOptionViewItem& option);
    [[nodiscard]] QSize richLineSize(const QStyleOptionViewItem& option, int maxWidth, const RichText& richText) const;
    [[nodiscard]] static RichText fallbackRichText(const QStyleOptionViewItem& option, const QModelIndex& index);
    [[nodiscard]] QSize richTextSize(const QStyleOptionViewItem& option, const QModelIndex& index) const;
    [[nodiscard]] QSize iconItemSize(const QStyleOptionViewItem& option, const QModelIndex& index) const;
    [[nodiscard]] static QSize iconItemLayoutSize(const QStyleOptionViewItem& option);
    [[nodiscard]] static QSize iconItemSizeForText(const QStyleOptionViewItem& option,
                                                   const IconItemLayoutMetrics& metrics, const QSize& textSize);

    void initLayoutOnlyOption(QStyleOptionViewItem* option, const QModelIndex& index) const;
    static void setupFilterOption(QStyleOptionViewItem* option, const QModelIndex& index);
    static void drawRichTextLines(QPainter* painter, const QStyleOptionViewItem& option, QRect rect,
                                  const IconCaptionLineList& lines);
    static void drawTextBlocks(QPainter* painter, const QStyleOptionViewItem& option, QRect rect,
                               const std::vector<RichTextBlock>& blocks);
};
} // namespace Fooyin::Filters
