/*
 * Copyright (C) 2020, 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 "commandtemplatelist.h"

#include <QVariantList>
#include <QDebug>

//CommandTemplateList* CommandTemplateList::m_commandTemplateListModel = nullptr;
CommandTemplateList* CommandTemplateList::originalCommandListModel = nullptr;

CommandTemplateList::CommandTemplateList(QObject *parent)
    : QAbstractListModel{parent}
{

}

int CommandTemplateList::rowCount(const QModelIndex &parent) const
{
    Q_UNUSED(parent)
    return m_commandTemplateList.count();
}

QVariant CommandTemplateList::data(const QModelIndex &index, int role) const
{
    if(index.row() < 0 || m_commandTemplateList.size() <= index.row()) {
        return QVariant();
    }

    CommandTemplateItem item = m_commandTemplateList.at(index.row());
    switch (role) {
        case CommandTemplateRoleType::NameRole:
            return item.name;
        case CommandTemplateRoleType::ActorRole:
            return item.actorId;
        case CommandTemplateRoleType::ContentRole:
            return item.content;
        case CommandTemplateRoleType::CommandTypeRole:
            return item.commandType;
        case CommandTemplateRoleType::IconRole:
            return item.iconPath;
        default:
            break;
    }

    return QVariant();
}

QHash<int, QByteArray> CommandTemplateList::roleNames() const
{
    QHash<int, QByteArray> roles;

    roles[CommandTemplateRoleType::NameRole]="name";
    roles[CommandTemplateRoleType::ActorRole]="actorId";
    roles[CommandTemplateRoleType::ContentRole]="content";
    roles[CommandTemplateRoleType::CommandTypeRole]="commandType";
    roles[CommandTemplateRoleType::IconRole] = "iconPath";

    return roles;
}

//CommandTemplateList *CommandTemplateList::getInstance()
//{
//    if(m_commandTemplateListModel == nullptr) {
//        m_commandTemplateListModel = new CommandTemplateList;
//    }

//    return m_commandTemplateListModel;
//}

QList<CommandTemplateItem> CommandTemplateList::getCommandTemplateList()
{
    return m_commandTemplateList;
}

QVariant CommandTemplateList::getData(const int row, int role) const
{

     CommandTemplateItem item = m_commandTemplateList.at(row);
    switch (role) {
        case 0:
            return item.name;
        case 1:
            return item.actorId;
        case 2:
            return row;
        case 3:
            return item.content;
        case 4:
            return item.commandType;
        case 5:
            return item.iconPath;
        default:
            break;
    }
}

int CommandTemplateList::getPromptId(QString name)
{
    int promptId = -2; // -2返回为一个无意义的值，为了和promptId默认为-1区分开，防止模型没有整理周报功能去生成周报
    for (int i = 0; i < m_commandTemplateList.size(); ++i) {
        CommandTemplateItem &item = m_commandTemplateList[i];
        if (item.name == name) {

            promptId = item.actorId;
            break;
        }
    }
    if (promptId == -2) {
        KyInfo() << "获取promptId失败";
    } else {
        KyInfo() << "获取promptId成功" << promptId;
    }
    return promptId;
}

int CommandTemplateList::count()
{
    return m_commandTemplateList.count();
}

void CommandTemplateList::initData(std::vector<Prompt> promptList)
{
    // actorId 到图标路径的映射
    QMap<int, QString> iconMap = {
        {1, "qrc:/icon/icon/setting/text-expend.svg"},
        {2, "qrc:/icon/icon/setting/text-polish.svg"},
        {3, "qrc:/icon/icon/setting/translate.svg"},
        {4, "qrc:/icon/icon/setting/system-QA.svg"},
        {5, "qrc:/icon/icon/setting/todo-generate.svg"},
        {6, "qrc:/icon/icon/setting/weekly-report.svg"},
        {7, "qrc:/icon/icon/setting/new-schedule.svg"},
        {8, "qrc:/icon/icon/setting/text-proofread.svg"},
        {9, "qrc:/icon/icon/setting/summary.svg"},
        {13, "qrc:/icon/icon/setting/system-control.svg"}
    };

    for (const auto& prompt : promptList) {
        CommandTemplateItem item;
        item.actorId = prompt.id;
        item.name = QString::fromStdString(prompt.name);
        item.content = QString::fromStdString(prompt.content);
        if(prompt.type == PromptContentAccessType::ReadOnly || prompt.type == PromptContentAccessType::NoAccess)
        {
            continue;
        }
        item.commandType = prompt.type;
        item.iconPath = iconMap.value(item.actorId, "qrc:/icon/icon/setting/prompt.svg");

        beginInsertRows(QModelIndex(), m_commandTemplateList.count(), m_commandTemplateList.count());
        m_commandTemplateList << item;
        endInsertRows();
    }

}

void CommandTemplateList::loadData(QList<CommandTemplateItem> commandTemplateList)
{
    // 清空现有数据
    beginResetModel();
    m_commandTemplateList.clear();
    endResetModel();

    for (const auto& item : commandTemplateList) {
        beginInsertRows(QModelIndex(), m_commandTemplateList.count(), m_commandTemplateList.count());
        m_commandTemplateList.append(item);
        endInsertRows();
    }
}

void CommandTemplateList::resetAllData()
{
    QList<CommandTemplateItem> list = m_commandTemplateList;
    beginResetModel();
    m_commandTemplateList.clear();
    m_commandTemplateList = list;
    endResetModel();

}

void CommandTemplateList::createCommand(const QString &name, const QString &content)
{
    emit sigCreateCommand(name, content);
}

void CommandTemplateList::updateCommand(int actorId, const QString &name, const QString &content)
{
    for (int i = 0; i < m_commandTemplateList.size(); ++i) {
        CommandTemplateItem &item = m_commandTemplateList[i];
        if (item.actorId == actorId) {

            emit sigUpdateCommand(actorId, name, content);
        }
    }
}

void CommandTemplateList::deleteCommand(int actorId, const QString &name)
{
    KyInfo() << "deletecommand";
    emit sigDeleteActiveCommand(actorId, name);
}

void CommandTemplateList::moveCommand(int fromIndex, int toIndex)
{
    if (fromIndex < 0 || fromIndex >= m_commandTemplateList.count() ||
        toIndex < 0 || toIndex >= m_commandTemplateList.count() || fromIndex == toIndex) {
        return;
    }

    beginMoveRows(QModelIndex(), fromIndex, fromIndex, QModelIndex(), toIndex > fromIndex ? toIndex + 1 : toIndex);
    m_commandTemplateList.move(fromIndex, toIndex);
    endMoveRows();

    emit dataChanged(index(0, 0), index(m_commandTemplateList.count() - 1, 0)); // 更新整个列表

}

void CommandTemplateList::addData(PromptContentAccessType commandType, int actorId, const QString &name, const QString &content)
{
    CommandTemplateItem item;
    item.commandType = commandType;
    item.actorId = actorId;
    item.name = name;
    item.content = content;
    item.iconPath = "qrc:/icon/icon/setting/prompt.svg";

    beginInsertRows(QModelIndex(), m_commandTemplateList.count(), m_commandTemplateList.count());
    m_commandTemplateList << item;
    endInsertRows();

    resetAllData();
    // emit sigUpdateUI();
}

void CommandTemplateList::updateData(int actorId, const QString &name, const QString &content)
{
    for (int i = 0; i < m_commandTemplateList.size(); ++i) {
        CommandTemplateItem &item = m_commandTemplateList[i];
        if (item.actorId == actorId) {

            item.name = name;
            item.content = content;

            QModelIndex modelIndex = this->index(i);
            emit dataChanged(modelIndex, modelIndex);

            // emit sigUpdateUI();
            emit sigUpdateActiveCommand(actorId, name);

            KyInfo() << "提示词编辑完成" << actorId << name;
            break;
        }
    }
}

void CommandTemplateList::removeData(int actorId, const QString &name)
{
    for (int i = 0; i < m_commandTemplateList.size(); ++i) {
        CommandTemplateItem &item = m_commandTemplateList[i];
        if (item.actorId == actorId && item.name == name) {

            beginRemoveRows(QModelIndex(), i, i);
            m_commandTemplateList.removeAt(i);
            endRemoveRows();

            // emit sigUpdateUI();
            KyInfo() << "提示词删除完成" << actorId << name;
            break;
        }
    }
}

// operator<< 格式化输出
QDebug operator<<(QDebug debug, const CommandTemplateItem &item)
{
    QDebugStateSaver saver(debug); // 保存 debug 的状态
    debug.nospace() << "CommandTemplateItem: "
                    << ", commandType: " << item.commandType
                    << ", actorId: " << item.actorId
                    << ", name: " << item.name
                    << ", content: " << item.content
                    << ", iconPath: " << item.iconPath << ")";
    return debug;
}
