#include "mprogrammanagera.h"
#include "ui_mprogrammanagera.h"

mProgramManagerA::mProgramManagerA(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::mProgramManagerA)
{
    ui->setupUi(this);

    LoAppConfig *cfg = LoAppConfig::getInstance();
    QString doc_path = cfg->DocumentsLocation();;
    if(!doc_path.isEmpty()) {
        QString app_path  = doc_path + MACRO_FENGEFU + cfg->ApplicationName();
        QString pRoot = app_path + MACRO_FENGEFU+"APrograms";
        QDir root_dir = QDir(pRoot);
        if(!root_dir.exists()) {
            QDir app_dir(app_path);
            app_dir.mkdir("APrograms");
        }
        m_strProgramItemPath = pRoot;
    }

    QAction *search = new QAction(ui->txtSearch);
    search->setIcon(QIcon(":/res/ProgramManager/bnSearch.png"));
    ui->txtSearch->addAction(search, QLineEdit::LeadingPosition);
    ui->txtSearch->setClearButtonEnabled(true);
    ui->txtSearch->setProperty("ssType", "progManageTool");
    ui->txtSearch->setProperty("ssName", "searchProg");

    ui->bnNew->setProperty("ssName", "newProg");
    ui->bnNew->setProperty("ssType", "progManageTool");
    ui->bnEdit  ->setProperty("ssType", "progManageTool");
    ui->bnDelete->setProperty("ssType", "progManageTool");
    ui->bnImport->setProperty("ssType", "progManageTool");
    ui->bnExport->setProperty("ssType", "progManageTool");
    ui->bnSend  ->setProperty("ssType", "progManageTool");
    ui->bnNew->setText(tr("New"));
    ui->bnEdit->setText(tr("Edit"));
    ui->bnDelete->setText(tr("Delete"));
    ui->bnImport->setText(tr("Import"));
    ui->bnExport->setText(tr("Export"));
    ui->bnSend->setText(tr("Publish"));

    ui->bnEdit->setEnabled(false);
    ui->bnDelete->setEnabled(false);
    ui->bnExport->setEnabled(false);
    ui->bnSend->setEnabled(false);

    connect(ui->bnNew,    SIGNAL(clicked(bool)), this, SLOT(onNewClicked(bool)));
    connect(ui->bnEdit,   SIGNAL(clicked(bool)), this, SLOT(onEditClicked(bool)));
    connect(ui->bnDelete, SIGNAL(clicked(bool)), this, SLOT(onDeleteClicked(bool)));

    m_headerItem = new QTreeWidgetItem();
    m_headerItem->setData(0, 0, "");
    m_headerItem->setData(1, 0, tr("Name"));
    m_headerItem->setData(2, 0, tr("Number"));
    m_headerItem->setData(3, 0, tr("File Size"));
    m_headerItem->setData(4, 0, tr("Last Modify"));
    m_headerItem->setData(5, 0, tr("Export"));
    m_headerItem->setData(6, 0, tr("Publish"));
    m_headerItem->setTextAlignment(1, Qt::AlignLeft | Qt::AlignVCenter);
    for(int i=2; i<7; i++) {
        m_headerItem->setTextAlignment(i, Qt::AlignCenter);
    }

    ui->wProgramList->setHeaderItem(m_headerItem);
    ui->wProgramList->header()->setSectionResizeMode(0, QHeaderView::Fixed);
    ui->wProgramList->header()->setSectionResizeMode(1, QHeaderView::Stretch);
    ui->wProgramList->header()->setSectionResizeMode(2, QHeaderView::Stretch);
    ui->wProgramList->header()->setSectionResizeMode(3, QHeaderView::Stretch);
    ui->wProgramList->header()->setSectionResizeMode(4, QHeaderView::Stretch);
    ui->wProgramList->header()->setSectionResizeMode(5, QHeaderView::Fixed);
    ui->wProgramList->header()->setSectionResizeMode(6, QHeaderView::Fixed);
    ui->wProgramList->header()->setStretchLastSection(false);
    ui->wProgramList->setColumnWidth(0, 72);
    ui->wProgramList->setColumnWidth(5, 72);
    ui->wProgramList->setColumnWidth(6, 72);

    connect(ui->wProgramList, SIGNAL(sigCheckStateChanged(int)), this, SLOT(onCheckStateChanged(int)));

    if(!m_strProgramItemPath.isEmpty()) {
        QDir root_dir(m_strProgramItemPath);
        QStringList pro_list = root_dir.entryList(QDir::Dirs | QDir::NoDotAndDotDot | QDir::NoSymLinks);
        foreach(QString pro_name, pro_list) {
            QDir pro_dir(m_strProgramItemPath + MACRO_FENGEFU + pro_name);
            if(pro_dir.exists("pro.json")) {
                QFile fPro(pro_dir.path() + MACRO_FENGEFU+"pro.json");
                fPro.open(QIODevice::ReadOnly);
                QJsonDocument pro = QJsonDocument::fromJson(fPro.readAll());
                fPro.close();
                onRestoreProgram(pro);
            }
        }
    }
}

mProgramManagerA::~mProgramManagerA()
{
    delete ui;
}

void mProgramManagerA::paintEvent(QPaintEvent *event)
{
    Q_UNUSED(event);
    QStyleOption opt;
    opt.init(this);
    QPainter p(this);
    style()->drawPrimitive(QStyle::PE_Widget, &opt, &p, this);
}
//切换语言，实时刷新翻译标签
void mProgramManagerA::refreshLable()
{
    m_headerItem->setData(0, 0, "");
    m_headerItem->setData(1, 0, tr("Name"));
    m_headerItem->setData(2, 0, tr("Number"));
    m_headerItem->setData(3, 0, tr("File Size"));
    m_headerItem->setData(4, 0, tr("Last Modify"));
    m_headerItem->setData(5, 0, tr("Export"));
    m_headerItem->setData(6, 0, tr("Publish"));
    ui->bnNew->setText(tr("New"));
    ui->bnEdit->setText(tr("Edit"));
    ui->bnDelete->setText(tr("Delete"));
    ui->bnImport->setText(tr("Import"));
    ui->bnExport->setText(tr("Export"));
    ui->bnSend->setText(tr("Publish"));

}
void mProgramManagerA::onCheckStateChanged(int f)
{
    switch (f) {
    case LoQTreeWidget::CheckNone:
        ui->bnEdit  ->setEnabled(false);
        ui->bnDelete->setEnabled(false);
        ui->bnExport->setEnabled(false);
        ui->bnSend  ->setEnabled(false);
        break;

    case LoQTreeWidget::CheckOne:
        ui->bnEdit  ->setEnabled(true);
        ui->bnDelete->setEnabled(true);
        ui->bnExport->setEnabled(true);
        ui->bnSend  ->setEnabled(true);
        break;

    case LoQTreeWidget::CheckMulti:
        ui->bnEdit  ->setEnabled(false);
        ui->bnDelete->setEnabled(true);
        ui->bnExport->setEnabled(false);
        ui->bnSend  ->setEnabled(true);
        break;

    default:
        break;
    }
}

void mProgramManagerA::onNewClicked(bool f)
{
   Q_UNUSED(f);
//    wNewProgram *dlg = new wNewProgram(this);
//    connect(dlg, SIGNAL(sigAcceptData(QString,QSize,QString)), this, SLOT(onCreateNewProgram(QString,QSize,QString)));
//    dlg->exec();
}

void mProgramManagerA::onEditClicked(bool f)
{
    Q_UNUSED(f);
//    wProgramItem *item = static_cast<wProgramItem*>(ui->wProgramList->itemChecked());
//    if(nullptr == item) return;
//    QString name    = item->name();
//    QSize   res     = QSize(item->width(), item->height());
//    QString remarks = item->remarks();
//    wNewProgram *dlg = new wNewProgram(name, res, remarks, this);
//    connect(dlg, SIGNAL(sigAcceptData(QString,QSize,QString)), this, SLOT(onEditHead(QString,QSize,QString)));
//    dlg->exec();
}

bool mProgramManagerA::checkIfNameRepeated(const QString &name, QTreeWidgetItem *skip)
{
    Q_UNUSED(name);

    Q_UNUSED(skip);

//    int cnt = ui->wProgramList->topLevelItemCount();
//    for(int i=0; i<cnt; i++) {
//        wProgramItem *item = static_cast<wProgramItem*>(ui->wProgramList->topLevelItem(i));
//        if(item == skip) {
//            continue;
//        }
//        if(name == item->name()) {
//            auto *mb = new X_UIMsgBoxOk(tr("Program name conflicted"), this);
//            mb->exec();
//            return true;
//        }
//    }
    return false;
}

void mProgramManagerA::onEditHead(const QString &name, QSize res, const QString &remarks)
{
    Q_UNUSED(name);
    Q_UNUSED(res);
    Q_UNUSED(remarks);


   // wProgramItem *item = static_cast<wProgramItem*>(ui->wProgramList->itemChecked());
//    if(checkIfNameRepeated(name, item)) {
//        return;
//    }
//    item->onAttrChanged(name, res.width(), res.height(), remarks);
}

void mProgramManagerA::onRestoreProgram(const QJsonDocument &pro)
{
    Q_UNUSED(pro);

//    new wProgramItem(m_strProgramItemPath, pro.object(), ui->wProgramList);
}

void mProgramManagerA::onCreateNewProgram(QString name, QSize res, QString remarks)
{
    Q_UNUSED(name);
    Q_UNUSED(res);
    Q_UNUSED(remarks);

//    if(checkIfNameRepeated(name)) {
//        return;
//    }
//    auto item = new wProgramItem(m_strProgramItemPath, name, res.width(), res.height(), remarks, ui->wProgramList);
//    item->save();
//    ui->wProgramList->adjustCheckState();
}

void mProgramManagerA::onDeleteClicked(bool f)
{
    Q_UNUSED(f);
//    int cnt = ui->wProgramList->topLevelItemCount();
//    QList<wProgramItem*> list;
//    for(int i=0; i<cnt; i++) {
//        if(ui->wProgramList->topLevelItem(i)->checkState(0) == Qt::Checked) {
//            wProgramItem *item = static_cast<wProgramItem*>(ui->wProgramList->topLevelItem(i));
//            list.push_back(item);
//        }
//    }
//    while(!list.isEmpty()) {
//        wProgramItem *item = list.takeFirst();
//        item->del();
//        delete item;
//    }
//    ui->wProgramList->adjustCheckState();
}

