#include "ctai_session_widget.h"

ctai_session_widget::ctai_session_widget()
{
    init_layout();
    connect_signals();
}
ctai_session_widget::~ctai_session_widget()
{
    for (auto* s : m_session_map) {
        if (s) {
            delete s->session_info;
            delete s;
        }
    }
    m_session_map.clear();
}
void ctai_session_widget::init_layout()
{
    m_session_layout = new QHBoxLayout();
    m_session_table_btn_layout = new QVBoxLayout();
    splitter = new QSplitter(Qt::Horizontal);
    m_table_widget = new QWidget();
    m_info_widget = new QWidget();
    // 会话列表顶部按钮布局
    m_session_btn_layout = new QHBoxLayout();
    m_btn_new_chat = new QPushButton(tr("新建"));
    m_btn_new_chat->setObjectName("m_new_chat_btn");
    m_btn_new_chat->setIcon(QIcon(":/res/img/btn/btn_new_chat.png"));

    m_btn_set_chat = new QPushButton(tr("设置"));
    m_btn_set_chat->setObjectName("m_btn_set_chat");
    m_btn_set_chat->setIcon(QIcon(":/res/img/btn/btn_setting.png"));

    m_btn_del_chat = new QPushButton("删除");
    m_btn_del_chat->setObjectName("m_btn_del_chat");
    m_btn_del_chat->setIcon(QIcon(tr(":/res/img/btn/btn_delete_chat.png")));

    m_session_btn_layout->addWidget(m_btn_new_chat);
    m_session_btn_layout->addWidget(m_btn_set_chat);
    m_session_btn_layout->addWidget(m_btn_del_chat);
    m_session_btn_layout->setContentsMargins(0, 0, 0, 0);
    // 会话列表布局
    m_session_table_layout = new QHBoxLayout();
    m_session_table = new QTableWidget();
    // 初始化设置界面
    m_session_setting = new ctai_session_setting();
    m_session_setting->hide();  // 初始时隐藏设置界面
    is_setting_visible = false;
    m_session_table->setColumnCount(1);                                        // 设置列数为1
    m_session_table->setSelectionBehavior(QAbstractItemView::SelectRows);      // 整行选中
    m_session_table->setSelectionMode(QAbstractItemView::SingleSelection);     // 单行选择
    m_session_table->verticalHeader()->hide();                                 // 隐藏行号
    m_session_table->horizontalHeader()->hide();                               // 隐藏表头
    m_session_table->horizontalHeader()->setStretchLastSection(true);          // 让列自动填充宽度
    m_session_table->verticalHeader()->setDefaultSectionSize(40);              // 设置默认行高
    m_session_table->setStyleSheet("QTableWidget::item { padding: 8px; }");    // 设置内边距
    m_session_table->setEditTriggers(QAbstractItemView::DoubleClicked | 
                                     QAbstractItemView::EditKeyPressed);       // 允许编辑
    m_session_table_layout->addWidget(m_session_table);
    m_session_table_layout->addWidget(m_session_setting);
    m_session_table_layout->setContentsMargins(0, 0, 0, 0);
    // 会话列表和按钮总布局
    m_session_table_btn_layout->addLayout(m_session_btn_layout);
    m_session_table_btn_layout->addLayout(m_session_table_layout);
    m_session_table_btn_layout->setStretch(1, 10);
    m_session_table_btn_layout->setContentsMargins(0, 0, 0, 0);
    m_table_widget->setLayout(m_session_table_btn_layout);
    // 会话信息布局
    m_session_info_layout = new QHBoxLayout();
    m_session_info_layout->addItem(sparcer_item);
    m_session_info_layout->setContentsMargins(0, 0, 0, 0);
    m_info_widget->setLayout(m_session_info_layout);
    // 窗口主布局
    splitter->addWidget(m_table_widget);
    splitter->addWidget(m_info_widget);
    splitter->setSizes({80, 920});
    m_session_layout->addWidget(splitter);
    m_session_layout->setContentsMargins(0, 0, 0, 0);
    setLayout(m_session_layout);
}
void ctai_session_widget::connect_signals()
{
    // 连接信号槽
    connect(m_btn_new_chat, SIGNAL(clicked()), this, SLOT(on_new_chat_clicked()));
    connect(m_btn_set_chat, SIGNAL(clicked()), this, SLOT(on_set_chat_clicked()));
    connect(m_btn_del_chat, SIGNAL(clicked()), this, SLOT(on_del_chat_clicked()));
    connect(m_session_table, &QTableWidget::currentCellChanged,
        this, [this](int currentRow, int currentCol, int, int) {
            on_session_selected(currentRow, currentCol);
        });
    // 连接表格项目编辑完成信号
    connect(m_session_table, &QTableWidget::itemChanged,
            this, &ctai_session_widget::on_session_name_changed);
}
void ctai_session_widget::on_new_chat_clicked()
{
    // 生成唯一会话ID
    QString session_id = QUuid::createUuid().toString(QUuid::WithoutBraces);

    // 创建新的session对象
    session* new_session = new session();
    new_session->session_id = session_id;
    new_session->session_name = tr("新会话");
    new_session->session_type = "chat";
    new_session->session_info = new ctai_session_info();

    // 添加到vector中
    m_session_map.push_back(new_session);

    // 添加到表格中
    int row = m_session_table->rowCount();
    m_session_table->insertRow(row);
    QTableWidgetItem* nameItem = new QTableWidgetItem(new_session->session_name);
    m_session_table->setItem(row, 0, nameItem);

    // 更新会话信息显示
    m_session_info_layout->addWidget(new_session->session_info);

    // 选中新创建的行
    m_session_table->selectRow(row);
    if (m_session_info_layout->count() > 0) {
        // 移除弹簧
        m_session_info_layout->removeItem(sparcer_item);
    }
}

void ctai_session_widget::on_del_chat_clicked()
{
    int currentRow = m_session_table->currentRow();
    if (currentRow < 0 || currentRow >= m_session_map.size()) {
        QMessageBox::warning(this, tr("警告"), tr("请先选择要删除的会话"));
        return;
    }

    // 删除session对象
    session* current_session = m_session_map[currentRow];
    m_session_map.erase(m_session_map.begin() + currentRow);
    delete current_session->session_info;
    delete current_session;

    // 从表格中删除
    m_session_table->removeRow(currentRow);

}
void ctai_session_widget::on_session_selected(int currentRow, int currentColumn)
{
    if (currentRow < 0 || currentRow >= m_session_map.size()) return;

    // 获取选中的会话
    session* current_session = m_session_map[currentRow];
    
    // 清理当前布局
    while (QLayoutItem* item = m_session_info_layout->takeAt(0)) {
        if (QWidget* widget = item->widget()) {
            widget->hide();
        }
        delete item;
    }

    // 显示新的会话信息
    if (current_session && current_session->session_info) {
        m_session_info_layout->addWidget(current_session->session_info);
        current_session->session_info->show();
    }
}

void ctai_session_widget::on_session_name_changed(QTableWidgetItem* item)
{
    if (!item) return;
    
    int row = item->row();
    if (row < 0 || row >= m_session_map.size()) return;
    
    // 更新 session 对象中的名称
    session* current_session = m_session_map[row];
    if (current_session) {
        current_session->session_name = item->text();
    }
}

void ctai_session_widget::on_set_chat_clicked()
{
    is_setting_visible = !is_setting_visible;
    
    if (is_setting_visible) {
        m_session_table->hide();
        m_session_setting->show();
        m_btn_set_chat->setText(tr("返回"));
    } else {
        m_session_setting->hide();
        m_session_table->show();
        m_btn_set_chat->setText(tr("设置"));
    }
}