#ifndef STYLE_OF_BOX_IN_LIST_H
#define STYLE_OF_BOX_IN_LIST_H
#include <QWidget>
#include <QLayout>
#include <QPushButton>
#include <QLabel>
#include <QLineEdit>
#include <QStyleOption>
#include <QPainter>
#include <QComboBox>
#include "switchbutton.h"
#include "ukui_printer.h"
static const int SYSTEM_DEFAULT_FONT_SIZE_BOX = 11;
static const int SYSTEM_DEFAULT_FONT_SIZE_PIXELS_BOX = 9;

class StyleTextWithLineEdit : public QWidget
{
    Q_OBJECT
public:
    StyleTextWithLineEdit(QString name, bool editable,QWidget *parent = nullptr)
    {
        this->setFixedSize(532,50);
        this->setAutoFillBackground(true);
        this->setBackgroundRole(QPalette::Window);

        m_nameLabel = new QLabel(this);
        m_nameLabel->setText(tr(name.toStdString().c_str()));
        m_nameLabel->setFixedWidth(114);
        m_nameLabel->setMinimumHeight(20);
        m_nameLabel->setBackgroundRole(QPalette::Window);
        m_nameLabel->setAlignment(Qt::AlignLeft|Qt::AlignVCenter);
        setSmartFont(m_nameLabel);

        m_frameLayout = new QHBoxLayout(this);
        m_frameLayout->setSpacing(0);
        m_frameLayout->addSpacing(16);
        m_frameLayout->addWidget(m_nameLabel);
        m_frameLayout->addSpacing(24);
    }
    virtual void setUnFixedPart(bool editable = true)
    {
        m_lineEdit = new QLineEdit(this);
        m_lineEdit->setAcceptDrops(false);
        m_lineEdit->setReadOnly(!editable);
        m_lineEdit->setCursorPosition(0);
        m_lineEdit->setFixedWidth(360);
        m_lineEdit->setMinimumHeight(36);
        m_lineEdit->setBackgroundRole(QPalette::Button);
        m_lineEdit->setAlignment(Qt::AlignLeft|Qt::AlignVCenter);

        m_frameLayout->insertWidget((m_frameLayout->count() + 1), m_lineEdit, 99);
        this->setLayout(m_frameLayout);
    }
    virtual void setFontSizeF(QFont ft)
    {
        m_nameLabel->setFont(ft);
        m_lineEdit->setFont(ft);
    }
    void setSmartFont(QLabel *label)
    {
        QFontMetrics fontWidth(label->font());
        QString labelString = label->text();
        label->setObjectName(labelString.left(labelString.length() - 1));

        int labelWidth = label->width();

        int width = fontWidth.width(labelString);  //计算字符串宽度
        if(width >= labelWidth)
        {
            label->setToolTip(labelString);
            QString temp = fontWidth.elidedText(labelString,Qt::ElideRight,labelWidth);
            label->setText(temp);

        }
    }
    QString getLabelName()
    {
        return m_nameLabel->objectName();
    }
    void setLineEditPropertText(QString text, QString objName)
    {
        m_lineEdit->setText(text);
        m_lineEdit->setToolTip(text);
        m_lineEdit->setCursorPosition(0);
        m_nameLabel->setObjectName(objName);
    }
    void setLineEditStatus(bool isEditable)
    {
        m_lineEdit->setReadOnly(!isEditable);
    }

    virtual void setComboBoxContent(QString objName, QString current,const QList<AttrName>& all) {}
    virtual void setSwitchBtnContent(bool editable, bool defaultValue) {}
    virtual QString getComboBoxContent() {return QString();}
    virtual bool getSwitchBtnEditable() {return false;}
    virtual void setConflictWarning() {}
    virtual void setConflictDefault() {}

protected:
    void paintEvent(QPaintEvent *e) override
    {
        QStyleOption opt;
        opt.init(this);
        QPainter p(this);
        style()->drawPrimitive(QStyle::PE_Widget, &opt, &p, this);
        QWidget::paintEvent(e);
    }
    QHBoxLayout *m_frameLayout;
    QLabel *m_nameLabel;
    QLineEdit *m_lineEdit;
    friend class PropertyListWindow;
};

class StyleTextWithLineEditAndButton : public StyleTextWithLineEdit
{
    Q_OBJECT
public:
    explicit StyleTextWithLineEditAndButton(QString name, bool editable,QWidget *parent = nullptr)
        :StyleTextWithLineEdit(name, editable,parent)
    {
    }
    void setUnFixedPart(bool editable = true) override
    {
        Q_UNUSED(editable)
        m_lineEdit = new QLineEdit(this);
        m_lineEdit->setAcceptDrops(false);
        m_lineEdit->setFixedSize(270,36);
        m_lineEdit->setCursorPosition(0);
        m_lineEdit->setBackgroundRole(QPalette::Button);
        m_lineEdit->setAlignment(Qt::AlignLeft|Qt::AlignVCenter);

        m_modify = new QPushButton(this);
        m_modify->setEnabled(true);
        m_modify->setFixedSize(80,36);

        m_frameLayout->insertWidget((m_frameLayout->count() + 1), m_lineEdit);
        m_frameLayout->addSpacing(80);
        m_frameLayout->insertWidget((m_frameLayout->count() + 1), m_modify, 99);

        this->setLayout(m_frameLayout);
    }
    void setBtnStatus(bool isClickable, QString text)
    {
        m_modify->setEnabled(isClickable);
        m_modify->setText(text);
    }
    void setFontSizeF(QFont ft)
    {
        m_nameLabel->setFont(ft);
        m_lineEdit->setFont(ft);
        m_modify->setFont(ft);
    }
signals:
    void btnIsClicked();
protected:
    QPushButton *m_modify;

    friend class PropertyListWindow;
};

class StyleTextWithComboBox : public StyleTextWithLineEdit
{
    Q_OBJECT
public:
    explicit StyleTextWithComboBox(QString name, bool editable,QWidget *parent = nullptr)
        :StyleTextWithLineEdit(name, editable,parent)
    {
    }
    void setUnFixedPart(bool editable = true) override
    {
        Q_UNUSED(editable);

        m_comboBox = new QComboBox(this);
        m_comboBox->setEnabled(true);
        m_comboBox->setFixedSize(360,36);
        m_comboBox->setObjectName("MyComboBox");
        m_comboBox->setBackgroundRole(QPalette::Button);

        m_frameLayout->insertWidget((m_frameLayout->count() + 1), m_comboBox, 99);

        this->setLayout(m_frameLayout);
    }
    void setComboBoxContent(QString objName, QString current,const QList<AttrName>& all) override
    {
        m_nameLabel->setObjectName(objName);
        for(auto it : all)
        {
            m_supportPairList.append({it.name,it.displayName});
        }
        for (int i = 0; i < m_comboBox->count();++i)
        {
            m_comboBox->removeItem(i);
        }
        if(all.size() == 0 || current.isEmpty())
        {
            m_comboBox->setEnabled(false);
            return;
        }
        m_comboBox->setEnabled(true);
        for(auto strStruct:all)
        {
            m_comboBox->addItem(strStruct.displayName);
        }
        for(auto it : all)
        {
            if (it.name == current)
            {
                m_comboBox->setCurrentIndex(m_comboBox->findText(it.displayName));
            }
        }

        connect(m_comboBox,SIGNAL(currentIndexChanged(int)),this,SLOT(sendOutComboBoxText()));
    }
    QString getComboBoxContent() override
    {
        QString realStr;
        for(auto it : m_supportPairList)
        {
            if (it.second == m_comboBox->currentText())
            {
                realStr = it.first;
            }
        }
        return realStr;
    }
    void setConflictWarning()
    {
        qDebug() << m_comboBox->currentText() << "为冲突项！";
        m_comboBox->setStyleSheet("QComboBox#MyComboBox{color:rgba(255, 0, 0, 1);}");
    }
    void setConflictDefault()
    {
        qDebug() << "无冲突项！";

        QPalette pal = m_comboBox->palette();
        pal.setColor(QPalette::HighlightedText,QColor(QPalette::HighlightedText));
        m_comboBox->setPalette(pal);
        m_comboBox->setStyleSheet("QComboBox#MyComboBox{color:QColor(QPalette::HighlightedText);}");
    }
    void setFontSizeF(QFont ft)
    {
        m_nameLabel->setFont(ft);
        m_comboBox->setFont(ft);
    }

signals:
    void checkModifiedValue(QString option,QString value);
public slots:
    void sendOutComboBoxText()
    {
        if(this->getLabelName().isEmpty())
        {
            return;
        }
        qDebug() << "Property" << this->getLabelName() << "changed to " << m_comboBox->currentText();
        emit checkModifiedValue(this->getLabelName(),getComboBoxContent());
    }
private:
    QComboBox *m_comboBox;
    friend class PropertyListWindow;

    QList<QPair<QString,QString>> m_supportPairList;
};

class StyleTextWithSwitch : public StyleTextWithLineEdit
{
    Q_OBJECT
public:
    explicit StyleTextWithSwitch(QString name, bool editable,QWidget *parent = nullptr)
        :StyleTextWithLineEdit(name, editable,parent)
    {
    }
    void setUnFixedPart(bool editable = true) override
    {
        Q_UNUSED(editable);

        m_switchBtn = new SwitchButton(this);
        m_switchBtn->setEnabled(true);

        m_frameLayout->insertSpacing((m_frameLayout->count() + 1), 312);
        m_frameLayout->insertWidget((m_frameLayout->count() + 1), m_switchBtn, 99);

        this->setLayout(m_frameLayout);
    }
    void setSwitchBtnContent(bool editable, bool defaultValue) override
    {
        if(editable == false)
        {
            m_switchBtn->setEnabled(false);
        }
        else
        {

            m_switchBtn->setChecked(defaultValue);
        }
    }
    bool getSwitchBtnEditable() override
    {
        return m_switchBtn->isEnabled();
    }
private:
    SwitchButton *m_switchBtn;
    friend class PropertyListWindow;
};

#endif // STYLE_OF_BOX_IN_LIST_H
