#include "ctai_session_info.h"

ctai_session_info::ctai_session_info()
{
    init_layout();
    init_sub_layout();
    init_curl();
}
ctai_session_info::~ctai_session_info()
{

}

void ctai_session_info::init_layout()
{
    mainLayout = new QVBoxLayout();
    splitter = new QSplitter(Qt::Vertical);
    m_sub_systeam_widget = new QWidget();
    m_sub_user_widget = new QWidget();
    splitter->addWidget(m_sub_systeam_widget);
    splitter->addWidget(m_sub_user_widget);
    splitter->setSizes({400, 100});
    mainLayout->addWidget(splitter);
    mainLayout->setContentsMargins(0, 0, 0, 0);
    setLayout(mainLayout);
}
void ctai_session_info::init_sub_layout()
{
    m_list_widget = new QListWidget();
    m_list_widget->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
    m_list_widget->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
    m_list_widget->setStyleSheet(R"(
    QListWidget {
        border: none;
    }
    QScrollBar:vertical {
        width: 0px;
    }
    QScrollBar:horizontal {
        height: 0px;
    }
)");
    m_session_systeam_info = new ctai_history_widget();
    m_session_systeam_info->setObjectName(tr("m_session_systeam_info"));
    m_session_systeam_info_layout = new QVBoxLayout();
    m_session_systeam_info_layout->addWidget(m_session_systeam_info);
    m_session_systeam_info_layout->setContentsMargins(0, 0, 0, 0);
    m_sub_systeam_widget->setLayout(m_session_systeam_info_layout);

    m_session_user_group_layout = new QVBoxLayout();
    m_session_user_edit_info = new QTextEdit(tr("1+1为什么等于2"));
    m_session_user_edit_layout = new QVBoxLayout();
    m_session_user_edit_layout->addWidget(m_session_user_edit_info);

    m_session_user_btn_layout = new QHBoxLayout();
    m_stream_switch = new ctai_switch_control();
    m_stream_mode_tips = new QLabel();
    m_info_error_tips = new QLabel();
    init_user_btn_layout();
    m_session_user_group_layout->addLayout(m_session_user_edit_layout);
    m_session_user_group_layout->addLayout(m_session_user_btn_layout);
    m_session_user_group_layout->setStretch(0, 9);
    m_session_user_group_layout->setStretch(1, 1);
    m_session_user_group_layout->setContentsMargins(0, 0, 0, 0);
    m_sub_user_widget->setLayout(m_session_user_group_layout);
}
void ctai_session_info::init_user_btn_layout()
{
    for (int i = 0; i <= m_user_btn_list.size() - 1; i++)
    {
        QPushButton *btn = new QPushButton();
        btn->setIcon(QIcon(m_user_btn_icon_list[i]));
        btn->setObjectName(m_user_btn_list[i]);
        init_user_btn_size_tips(*btn, QSize(25, 25), m_user_btn_tips_list[i]);
        m_btns.push_back(btn);
        if (i == 4)
        {
            m_session_user_btn_layout->addItem(sparcer_item);
            m_session_user_btn_layout->addWidget(m_stream_mode_tips);
            m_session_user_btn_layout->addWidget(m_stream_switch);
            connect(m_stream_switch, SIGNAL(toggled(bool)), this, SLOT(ctai_session_set_steam_mode(bool)));
        }
        m_session_user_btn_layout->addWidget(btn);
        switch (i)
        {
        case 0:

            break;
        case 1:

            break;
        case 2:

            break;
        case 3:

            break;
        case 4:

            break;
        case 5:
            connect(btn, SIGNAL(clicked(bool)), this, SLOT(ctai_session_user_sending()));
            break;
        default:
            break;
        }
    }
}
void ctai_session_info::init_user_btn_size_tips(QPushButton &btn, QSize size, QString tip_str)
{
    btn.setIconSize(size);
    btn.setToolTip(tip_str);
}
void ctai_session_info::init_curl()
{
    m_curl = new ctai_curl();
    m_curl->curl_init();
    connect(m_curl, SIGNAL(send_post_out_data(model_data)), this, SLOT(ctai_session_postback_info(model_data)));
    connect(m_curl, SIGNAL(send_post_out_error(std::string)), this, SLOT(ctai_session_curl_state_tips(std::string)));
}
void ctai_session_info::btn_disable_enable(bool state)
{
    m_btns[5]->setEnabled(state);
}

void ctai_session_info::ctai_session_postback_info(model_data resp_data)
{  
    if (resp_data.postback_send_id != current_info_id)
    {
        current_info_id = resp_data.postback_send_id;
        m_session_systeam_info->add_message(resp_data);
    }
    else
    {
        m_session_systeam_info->add_message(resp_data);
    }
    if (QSL(resp_data.postback_model_data).contains("[DONE]"))
    {
        current_info_id = {};      
    }
    btn_disable_enable(true);
    ctai_session_curl_state_tips("");
}
void ctai_session_info::ctai_session_curl_state_tips(std::string state_str)
{
    m_info_error_tips->setText(QString::fromStdString(state_str));
}
void ctai_session_info::ctai_session_set_steam_mode(bool state)
{
    qDebug() << "steam mode:" << state;
    m_stream_switch->setToggle(state);
    m_stream_mode=state;
}
void ctai_session_info::ctai_session_user_sending()
{
    if (m_session_user_edit_info->toPlainText().isEmpty())
    {
        QMessageBox::warning(this, tr("警告"), tr("发送内容不能为空"));
        return;
    }
    else
    {
        user_model_data.msg_type_mode = USER;
        user_model_data.send_user_id=QUuid::createUuid().toString(QUuid::WithoutBraces).toStdString();
        user_model_data.send_user_data=m_session_user_edit_info->toPlainText().toStdString();
        user_model_data.send_user_time=(QDateTime::currentDateTime().toString("yyyy-MM-dd hh:mm:ss.zzz")).toStdString();
        m_session_systeam_info->add_message(user_model_data);
        QFuture<void> future = QtConcurrent::run([=]()
                                                 {
            user_model_data.msg_type_mode = SYSTEM;
            user_model_data.postback_stream_mode=m_stream_mode;
            m_curl->send_post_response(user_model_data,curl_opt); 
            });
        btn_disable_enable(false);
        m_session_user_edit_info->clear();
        ctai_session_curl_state_tips("正在获取结果...");
    }
}