#include "pubcombolinecouplewideget.h"
#include <QDebug>
#include <QList>
#include <tbaseutil/tenumlist.h>
#include <topcore/topenummanager.h>
#include <topcore/topclassabs.h>
#include <tbaseutil/tdataparse.h>
#include <twidget/thboxlayout.h>
#include <twidget/tpushbutton.h>
#include <tbaseutil/tresource.h>
#include <twidget/tlineedit.h>
#include <twidget/ttextedit.h>
#include <twidget/tlabel.h>
#include <twidget/teditablecombobox.h>
#include <twidget/ttitleexpander.h>
#include <twidget/tvboxlayout.h>
#include <twidget/tformlayout.h>
#include <twidget/tcombobox.h>

PubComboLineCoupleWideget::PubComboLineCoupleWideget(TopClassAbs *parent)
    : QWidget(parent), mParent(parent)
{
    initMainView();
    mNewItemButton = new TPushButton();
    mNewItemButton->setText(ttr("New Item"));
    mFramelayout->insertWidget(mItemWgtLst.length(), mNewItemButton, 1, Qt::AlignLeft);
    connect(mNewItemButton, SIGNAL(clicked(bool)), this, SLOT(newCoupleItem()));
    connect(mNewItemButton, SIGNAL(clicked(bool)), this, SIGNAL(dataChanged()));
}

QVariantMap PubComboLineCoupleWideget::getData()
{
    QVariantMap resMap;
    QStringList keyLst;
    QStringList itemValueLst;
    for (PubComboLineCoupleWidegetPrivate *wgt: mItemWgtLst) {
        if (!keyLst.contains(wgt->categoryText())) {
            keyLst.append(wgt->categoryText());
        }
    }

    for (QString keyStr: keyLst) {
        itemValueLst.clear();
        for (PubComboLineCoupleWidegetPrivate* wgt: mItemWgtLst) {
            if (keyStr == wgt->categoryText()) {
                itemValueLst.append(wgt->valueText());
            }
        }
        resMap.insert(keyStr,itemValueLst);
    }
    return resMap;
}

void PubComboLineCoupleWideget::setData(QVariant iData)
{
    QVariantMap valueMap;
    QVariantList resValueLst;

    if (iData.type() == QVariant::String) {
        valueMap = TDataParse::jsonStr2Variant(iData.toString()).toMap();
    } else if (iData.type() == QVariant::Map) {
        valueMap = iData.toMap();
    } else {
        valueMap = QVariantMap();
    }

    QStringList keyLst = valueMap.keys();
    for (QString keyStr: keyLst) {
        QStringList itemValueLst;
        itemValueLst = valueMap.value(keyStr).toStringList();
        QVariantMap resValueMap;
        for (QString oneValue: itemValueLst) {
            resValueMap.insert("name", enumText2Name(keyStr));
            resValueMap.insert("value", oneValue);
            resValueLst.append(resValueMap);
        }
    }

    //清除所有行
    for (PubComboLineCoupleWidegetPrivate *wgt: mItemWgtLst) {
        mItemWgtLst.removeOne(wgt);
        wgt->hide();//deleter来的较慢,避免视觉上重复出现
        wgt->deleteLater();
    }
    //按list的长度初始化控件(行)个数
    for (int count = 0; count < resValueLst.length(); count++) {
       newCoupleItem();
    }
    //对所有行逐一赋值
    for (PubComboLineCoupleWidegetPrivate *wgt: mItemWgtLst) {
        int index = mItemWgtLst.indexOf(wgt);
        QVariantMap itemMap = resValueLst.value(index).toMap();
        wgt->setName(itemMap.value("name").toString());
        wgt->setValue(itemMap.value("value").toString());
    }
}

void PubComboLineCoupleWideget::setEnumName(QString iEnumName)
{
    mEnumName = iEnumName;
}

void PubComboLineCoupleWideget::setComboBoxEditable(bool iIsEditable)
{
    mIsComboEditable = iIsEditable;
}

QString PubComboLineCoupleWideget::ttr(const QString &iStr)
{
    if (mParent) {
        return mParent->ttr(iStr);
    } else {
        return tr(iStr.toStdString().data());
    }
}

QString PubComboLineCoupleWideget::enumText2Name(QString iNameStr)
{
    if (TOPENM->isEnumListExists(mEnumName)){
        QVariantList lst = TOPENM->enumList(mEnumName)->toComboList();
        for (QVariant item: lst) {
            if (iNameStr == item.toMap().value("name")) {
                return item.toMap().value("text").toString();
            }
        }
    }
    return iNameStr;
}

void PubComboLineCoupleWideget::newCoupleItem()
{
    PubComboLineCoupleWidegetPrivate* wgt = new PubComboLineCoupleWidegetPrivate(this);
    wgt->setComboBoxEditable(mIsComboEditable);
    mItemWgtLst.append(wgt);
    mFramelayout->insertWidget(mItemWgtLst.length() - 1 , wgt);
    wgt->setCategoryEnum(mEnumName);
    connect(wgt, SIGNAL(dataChanged()), this, SIGNAL(dataChanged()));
    connect(wgt, SIGNAL(deleteClicked()), this, SLOT(onDeleteClicked()));
}

void PubComboLineCoupleWideget::initMainView()
{
    QHBoxLayout* hMainLayout = new QHBoxLayout(this);
    hMainLayout->setMargin(0);
    hMainLayout->setSpacing(0);
    this->setLayout(hMainLayout);
    mFrame = new QFrame(this);
    hMainLayout->addWidget(mFrame);
    mFramelayout = new TVBoxLayout(mFrame);
    mFramelayout->setMargin(8);
    mFramelayout->setSpacing(8);
}

void PubComboLineCoupleWideget::onDeleteClicked()
{
    PubComboLineCoupleWidegetPrivate* wgt = qobject_cast<PubComboLineCoupleWidegetPrivate *>(sender());
    mItemWgtLst.removeOne(wgt);
    mFramelayout->removeWidget(wgt);
    wgt->deleteLater();
    emit(dataChanged());
}

PubComboLineCoupleWidegetPrivate::PubComboLineCoupleWidegetPrivate(QWidget *parent)
{
    Q_UNUSED(parent);

    THBoxLayout* layout = new THBoxLayout(this);
    layout->setMargin(0);
    layout->setSpacing(0);
    mCategoryComboBox = new TEditableComboBox(this);
    mCategoryComboBox->setFixedWidth(100);
    mCategoryComboBox->setStyleSheet("QComboBox{border-top:0px;border-bottom:0px;border-left:0px;border-right:4px;border-right-style: solid;border-right-color:#F3F7FB;}");
    mValueLineEdit = new TLineEdit(this);
    mValueLineEdit->prependWidget(mCategoryComboBox);

    mDeleteButton = new TPushButton(this);
    mDeleteButton->setIcon(TRES->icon("minus-circle"));
    mDeleteButton->setFlat(true);
    mValueLineEdit->appendWidget(mDeleteButton);
    layout->addWidget(mValueLineEdit);
    connect(mCategoryComboBox, SIGNAL(currentTextChanged(QString)), this, SIGNAL(dataChanged()));
    connect(mValueLineEdit, SIGNAL(textChanged(QString)), this, SIGNAL(dataChanged()));
    connect(mDeleteButton, SIGNAL(clicked()), this, SIGNAL(deleteClicked()));
}

void PubComboLineCoupleWidegetPrivate::setCategoryEnum(QString iEnumName)
{
    if (TOPENM->isEnumListExists(iEnumName)) {
        mEnumName = iEnumName;
        QVariantList lst = TOPENM->enumList(iEnumName)->toComboList();
        QStringList strLst;
        for (QVariant item: lst) {
            strLst.append(item.toMap().value("text").toString());
        }
        mCategoryComboBox->setItemList(strLst);
    }
}

void PubComboLineCoupleWidegetPrivate::setComboBoxEditable(bool iIsEditable)
{
    mCategoryComboBox->setEditable(iIsEditable);
}

QString PubComboLineCoupleWidegetPrivate::categoryText()
{
    if (mCategoryComboBox) {
        //查找当前选中项对应的枚举
        if (TOPENM->isEnumListExists(mEnumName)) {
            QVariantList lst = TOPENM->enumList(mEnumName)->toComboList();
            for (QVariant item: lst) {
                if (mCategoryComboBox->currentText() == item.toMap().value("text")) {
                    return item.toMap().value("name").toString();
                }
            }
        }
    }
    return mCategoryComboBox->currentText();
}

QString PubComboLineCoupleWidegetPrivate::valueText()
{
    if (mValueLineEdit) {
        return mValueLineEdit->text();
    }
    return "";
}

void PubComboLineCoupleWidegetPrivate::clearData()
{
    mCategoryComboBox->clear();
    mValueLineEdit->clear();
}

void PubComboLineCoupleWidegetPrivate::setName(const QString &iNameStr)
{
    if (iNameStr.isEmpty() || iNameStr.isNull()) {
        mCategoryComboBox->setCurrentText("");
        return ;
    }
    mCategoryComboBox->setCurrentText(iNameStr);
}

void PubComboLineCoupleWidegetPrivate::setValue(const QString &iValueStr)
{
    if (iValueStr.isEmpty()|| iValueStr.isNull()) {
        mValueLineEdit->setText("");
        return ;
    }
    mValueLineEdit->setText(iValueStr);
}