#include "MyQUI.h"

static const std::string QSS_GROUP =
"QGroupBox { border: 1px solid gray; margin-top: 8px; border-radius: 5px;}"
"QGroupBox::title { subcontrol-origin: margin; subcontrol-position: top left; left:10px; margin-left: 0px; padding:0 1px; }";

static const std::string QSS_BTN =
"QPushButton {background:rgba(180,180,180,50);border-radius:2px;border:1px solid gray;color:white;}"
"QPushButton:hover {background-color:rgb(32,164,128);}"
"QPushButton:pressed {border-width:2px 0 0 2px;border-style:inset;}";

static const std::string QSS_BTN_ACTIVE =
"QPushButton {background:rgba(180,180,180,50);border-radius:2px;border:1px solid rgb(50,200,50);color:rgb(50,200,50);font:bold 15px;}";

static const std::string QSS_BTN_MENU =
"QPushButton {background:rgba(180,180,180,50);color:white;border-radius:5px;border:1px solid gray;font:bold 18px;}"
"QPushButton:hover {background-color:rgb(32,164,128);}"
"QPushButton:pressed {border-width:3px 0 0 3px;border-style:inset;}"
"QPushButton:disabled {color:rgb(150,150,150);}";

static const std::string QSS_BTN_MENU_ACTIVE =
"QPushButton {background:rgba(32,164,128, 200);color:white;border-radius:5px;border:1px solid gray;font:bold 18px;}"
"QPushButton:hover {background-color:rgb(32,164,128);}"
"QPushButton:pressed {border-width:3px 0 0 3px;border-style:inset;}"
"QPushButton:disabled {color:rgb(150,150,150);}";

static const std::string QSS_BTN_TAB =
"QPushButton {background:rgba(180,180,180,100);color:white;border-radius:3px;border:1px solid rgb(10,120,215);}"
"QPushButton:hover {background-color:rgba(80,80,80,200);}";
//"QPushButton:pressed {border-width:3px 0 0 3px;border-style:inset;}"
//"QPushButton:disabled {color:rgb(150,150,150);}";

static const std::string QSS_LINE =
"QLineEdit { background-color:rgba(180,180,180,10); color:#ffffff; border:1px solid gray; border-radius:3px; font:bold 13px; }"
"QLineEdit:read-only{ background-color:rgba(180,180,180,80); color:rgb(180,180,180); }";


static const std::string QSS_TABLE =
// 表格整体样式
"QTableWidget {"
"   background-color: transparent;"  // 背景色
"   gridline-color: #C0C0C0;"     // 网格线颜色
"   border: 1px solid gray;"      // 边框
"   color: white;"                // 文字颜色
"   outline: 2px solid #409EFF;"  // 选中焦点轮廓
"}"
// 表头样式
"QHeaderView { background-color: rgba(150,150,150,50);}"
"QHeaderView::section {"
"   background-color: rgba(120,120,120,0);"  // 表头背景
"   padding: 4px;"              // 内边距
"   border: 1px solid gray;"    // 边框
"   min-height: 25px;"          // 最小高度
"}"
// 单元格样式
"QTableWidget::item {"
"	padding-left: 5px;"
"   border-bottom: 1px solid gray;"  // 底部边框
"}"
// 选中状态
"QTableWidget::item:selected {"
"   background-color: rgba(184, 214, 255, 50);" // 选中背景色
"   color: rgb(220,220,220);"                   // 选中文字颜色
"}";

static const std::string QSS_BTN_COMBOX =
"QComboBox { background-color: transparent; border: 1px solid rgb(180,180,180); padding-left: 5px; }"
"QComboBox QAbstractItemView { background-color: rgb(60, 80, 120); border: 2px solid #ff0000; padding: 2px; outline: none; }"
"QComboBox QAbstractItemView::item { min-height: 20px; padding: 5px 10px; margin:1px 1px 1px 1px; }"
"QComboBox QAbstractItemView::item:hover { background-color: rgb(220, 220, 220); color: rgb(31, 139, 117);  }";


MyWidget::MyWidget(QWidget* parent) : QWidget(parent)
{
    // 可以在这里设置样式表，也可以在其他地方设置
    setObjectName("MyWidget");
    setStyleSheet("#MyWidget {background-color:rgba(120,120,120,80); border-radius:5px;}");
    // 确保自动填充背景
    setAutoFillBackground(true);
}

shared_ptr<QWidget> MyQUI::Widget(QWidget* parent, string objectName, string qss)
{
    auto widget = std::make_shared<QWidget>(parent);
    widget->setObjectName(objectName.c_str());
    widget->setStyleSheet(("#" + objectName + " {"+ qss + "}").c_str());
    return widget;
}


shared_ptr<QPushButton> MyQUI::Button(QWidget* parent, int x, int y, int w, int h, std::string title)
{
    auto btn = std::make_shared<QPushButton>(title.c_str(), parent);
    btn->setGeometry(x, y, w, h);
    btn->setStyleSheet(QSS_BTN.c_str());
    return btn;
}

std::shared_ptr<QGroupBox> MyQUI::GroupBox(QWidget* parent, int x, int y, int w, int h, std::string title)
{
	auto groupBox = std::make_shared<QGroupBox>(title.c_str(), parent);
	groupBox->setGeometry(x, y, w, h);
	groupBox->setStyleSheet(QSS_GROUP.c_str());
	return groupBox;
}

MyPairLabelLine MyQUI::PairLine(QWidget* parent, int x, int y, string k, string v, bool readonly/* = true*/)
{
    shared_ptr<QLabel> key = make_shared<QLabel>(parent);
    key->setText(k.c_str());
    key->setGeometry(x, y, 80, 26);
    shared_ptr<QLineEdit> value = make_shared<QLineEdit>(parent);
    value->setText(v.c_str());
    value->setGeometry(x+80, y, 260, 26);
    value->setStyleSheet(QSS_LINE.c_str());
    value->setReadOnly(readonly);
    return {key, value};
}

std::shared_ptr<QTableWidget> MyQUI::TableWidget(QWidget* parent, int x, int y, int w, int h)
{
    auto table = std::make_shared<QTableWidget>(parent);
    table->setGeometry(x, y, w, h);
    table->setStyleSheet(QSS_TABLE.c_str());
    table->horizontalHeader()->setStretchLastSection(true);		// 最后一列占满
    table->verticalHeader()->setVisible(false);					// 不显示垂直表头
    table->setEditTriggers(QAbstractItemView::NoEditTriggers);	// 单元格不可编辑
    table->setSelectionMode(QAbstractItemView::SingleSelection);	// 设置为单选模式
    table->setSelectionBehavior(QAbstractItemView::SelectRows);		// 设置为整行选中
    table->horizontalHeader()->setFixedHeight(50);
    table->horizontalHeader()->setDefaultSectionSize(50);  // 设置默认行宽
    return table;
}

void MyQUI::setTableCellText(std::shared_ptr<QTableWidget> table, int row, int col, std::string text, std::string style /*= ""*/)
{
    if (row >= table->rowCount())
    {
        table->insertRow(row);
    }
    auto item = table->item(row, col);
    if (!item)
    {
        item = new QTableWidgetItem();
        table->setItem(row, col, item);
    }
    item->setText(text.c_str());
    if (style == "OK") { item->setForeground(QBrush(Qt::green)); }
    else if (style == "ERR") { item->setForeground(QBrush(Qt::red)); }
    else if (style == "WARN") { item->setForeground(QBrush(Qt::cyan)); }
}

void MyQUI::setTableCellButton(std::shared_ptr<QTableWidget> table, int row, int col, const vector<string>& items, QObject* obj, std::function<void(string name)> cb)
{
    if (row >= table->rowCount()) { table->insertRow(row); }

    QWidget* cellWidget = new QWidget();
    for (int i=0; i<items.size(); ++i)
    {
        string itemName = items[i];
        QPushButton* btn = new QPushButton(itemName.c_str(), cellWidget);
        btn->setGeometry(i*60, 3, 50, 24);
        btn->setStyleSheet(QSS_BTN_TAB.c_str());
        QObject::connect(btn, &QPushButton::clicked, obj, [=]() { if (cb) cb(itemName); });
    }
   table->setCellWidget(row, col, cellWidget);
}