// SPDX-FileCopyrightText: 2024 Manuel Alcaraz Zambrano <manuel@alcarazzam.dev>
// SPDX-License-Identifier: GPL-3.0-or-later

#pragma once

#include <QAbstractTableModel>

#include "tournament/player.h"

class Tournament;

class PlayersModel : public QAbstractTableModel
{
    Q_OBJECT
    QML_ELEMENT
    QML_UNCREATABLE("")

public:
    enum Columns {
        StartingRank,
        Title,
        Name,
        Rating,
        NationalRating,
        PlayerId,
        BirthDate,
        Federation,
        Origin,
        Gender,
    };
    Q_ENUM(Columns)

    enum Roles {
        PlayerRole = Qt::UserRole,
        IconRole,
    };
    Q_ENUM(Roles)

    explicit PlayersModel(QObject *parent = nullptr);

    [[nodiscard]] int rowCount(const QModelIndex &parent = QModelIndex()) const override;
    [[nodiscard]] int columnCount(const QModelIndex &parent = QModelIndex()) const override;
    [[nodiscard]] QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const override;
    bool setData(const QModelIndex &index, const QVariant &value, int role = Qt::EditRole) override;
    [[nodiscard]] QVariant headerData(int section, Qt::Orientation orientation, int role = Qt::DisplayRole) const override;
    [[nodiscard]] QHash<int, QByteArray> roleNames() const override;
    [[nodiscard]] Qt::ItemFlags flags(const QModelIndex &index) const override;

    void setColumns(const QList<int> &columns);
    void setTournament(Tournament *tournament);
    void setPlayers(const QList<Player *> &players);
    Q_INVOKABLE void addPlayer(const QString &title,
                               const QString &name,
                               int rating,
                               int nationalRating,
                               const QString &playerId,
                               const QString &birthDate,
                               const QString &federation,
                               const QString &origin,
                               const QString &sex);
    void updatePlayer(Player *player);
    void reloadPlayers();

Q_SIGNALS:
    void playerChanged(Player *player, Columns field);

    void errorOcurred(const QString &error);

private:
    Tournament *m_tournament;
    QList<Player *> m_players;

    QList<int> m_columns = {StartingRank, Title, Name, Rating, NationalRating, PlayerId, BirthDate, Federation, Origin, Gender};
};
