#include "selectrestorepoint.h"
#include <QSettings>
#include <QTextCodec>
#include "component/mypushbutton.h"
#include "../../common/utils.h"
#include "messageboxutils.h"
#include "../globalbackupinfo.h"

SelectRestorePoint::SelectRestorePoint(QWidget *parent, BackupPointType backupType, bool isOnlyShowLocal) :
    BackupPointListDialog(parent, isOnlyShowLocal),
    m_backupType(backupType)
{
    if (BackupPointType::SYSTEM == backupType) {
        this->setWindowTitle(tr("System Backup Information"));
        m_titleLabel->setDeplayText(tr("System Backup Information"));
    } else {
        this->setWindowTitle(tr("Data Backup Information"));
        m_titleLabel->setDeplayText(tr("Data Backup Information"));
    }

    // 隐藏备份点状态Backup State字段
    m_tableWidget->hideColumn(Column_Index::Backup_State);
    initTableWidget();

    // 刷新
//    MyPushButton * buttonRefresh = new MyPushButton;
//    buttonRefresh->setText(tr("Refresh"));

    // 确定按钮
    MyPushButton * buttonOk = new MyPushButton;
    buttonOk->setText(tr("OK"));
    this->setResult(QDialog::Rejected);
    buttonOk->setProperty("isImportant", true);
    buttonOk->setEnabled(false);

    m_bottomLayout->addStretch();
//    m_bottomLayout->addWidget(buttonRefresh);
    m_bottomLayout->addSpacing(10);
    m_bottomLayout->addWidget(buttonOk);

    connect(m_tableWidget, &QTableWidget::clicked, this, [=]() {
        buttonOk->setEnabled(true);
    });
//    connect(buttonRefresh, &MyPushButton::clicked, this, &SelectRestorePoint::initTableWidget);
    connect(this, &SelectRestorePoint::udiskChange, this, &SelectRestorePoint::initTableWidget);
    connect(&m_dateUtils, &DateTimeUtils::ShortDateSignal, this, &SelectRestorePoint::initTableWidget);

    connect(buttonOk, &MyPushButton::clicked, this, [=](){
        // 判断是否已经选中备份点
        QList<QTableWidgetItem *> selectList = this->m_tableWidget->selectedItems();
        if (selectList.isEmpty()) {
            MessageBoxUtils::QMESSAGE_BOX_WARNING(this, QObject::tr("Warning"), QObject::tr("Please select one backup to continue."), QObject::tr("OK"));
            // this->reject();
            return;
        }

        // 还原相应备份点
        if (!this->m_needConfirm || MessageBoxUtils::QMESSAGE_BOX_WARNING_CANCEL(this, QObject::tr("Information"), QObject::tr("Do you want to continue?"), QObject::tr("Continue"), QObject::tr("Cancel"))) {
            int curRow = this->m_tableWidget->currentRow();
            ParseBackupList::BackupPoint backupPoint;
            backupPoint.m_backupName = this->text(curRow, Column_Index::Backup_Name);
            backupPoint.m_uuid = this->text(curRow, Column_Index::UUID);
            backupPoint.m_time = this->text(curRow, Column_Index::Backup_Time);
            backupPoint.m_path = this->text(curRow, Column_Index::Prefix_Path);
            QString dev = this->text(curRow, Column_Index::Backup_Device);
            if (dev.startsWith(tr("Other machine:")))
                backupPoint.m_iPosition = BackupPosition::OTHER;
            else if (dev.startsWith(tr("Customize:")))
                backupPoint.m_iPosition = BackupPosition::CUSTOMIZE;
            else if (dev.startsWith(tr("Udisk Device:")))
                backupPoint.m_iPosition = BackupPosition::UDISK;
            else
                backupPoint.m_iPosition = BackupPosition::LOCAL;

            emit this->selected(backupPoint);
            this->accept();
        }
    });
}

SelectRestorePoint::~SelectRestorePoint()
{}

void SelectRestorePoint::initTableWidget()
{
    m_tableWidget->clearContents();
    m_tableWidget->setRowCount(0);
    m_tableWidget->setSortingEnabled(false); // 等录入完数据后再排序
    insertLines(getBackupPointList());
}

void SelectRestorePoint::insertLines(const QList<ParseBackupList::BackupPoint> &backupPoints)
{
    QString settingPath = Utils::getSysRootPath() + UDISK_UNIQUE_SETTINGS;
    settingPath.replace("//", "/");
    QSettings udisk_setting(settingPath, QSettings::IniFormat);
    udisk_setting.setIniCodec(QTextCodec::codecForName("utf-8"));

    int indexOfRow = 0;
    for (const ParseBackupList::BackupPoint& backupPoint : backupPoints) {
        // 系统备份排除非系统备份且非增量系统备份
        if (BackupPointType::SYSTEM == m_backupType &&
            int(BackupType::BACKUP_SYSTEM) != backupPoint.m_type &&
            int(BackupType::INC_BACKUP_SYSTEM) != backupPoint.m_type)
            continue;
        else if (BackupPointType::DATA == m_backupType &&
                 int(BackupType::BACKUP_DATA) != backupPoint.m_type &&
                 int(BackupType::INC_BACKUP_DATA) != backupPoint.m_type)
            continue;

        // 数据备份需要用户隔离
        if (BackupPointType::DATA == m_backupType) {
            // 管理员可看自己用户进行的数据备份和无用户信息的数据备份（旧备份数据）
            if (GlobelBackupInfo::instance().isManager() && !backupPoint.m_userId.isEmpty() && backupPoint.m_userId != GlobelBackupInfo::instance().curUserId())
                continue ;

            // 标准用户只能看自己进行的操作
            if (!GlobelBackupInfo::instance().isManager() && backupPoint.m_userId != GlobelBackupInfo::instance().curUserId())
                continue ;
        }

        // 不展示未完成的备份点
        if (BACKUP_PARSE_STATE_SUCCESS_STRTING != backupPoint.m_state)
            continue;

        //hide factory backup
        // 华为机型向主线SP1看齐，出厂备份相关的保持和主线一致
//        if (backupPoint.m_uuid == FACTORY_BACKUP_UUID && Utils::isHuawei990())
//            continue;

        //udisk unique
        bool isOther = false;
        QString preDevPath(Utils::getSysRootPath());
        preDevPath += "/media";
        preDevPath.replace("//", "/");
        if (backupPoint.m_path.startsWith(preDevPath)) {
            udisk_setting.beginGroup(backupPoint.m_backupName);
            QString localuuid = udisk_setting.value("uuid").toString();
            udisk_setting.endGroup();
            if (localuuid != backupPoint.m_uuid) {
                if (BackupPointType::SYSTEM != m_backupType)
                    continue;

                if (
                        backupPoint.m_os.left(backupPoint.m_os.length() - 1) != SystemInfo::m_os.left(SystemInfo::m_os.length() - 1)
                     || backupPoint.m_arch != SystemInfo::m_arch
                     || backupPoint.m_archdetect != SystemInfo::m_archDetect
                   ) {
                    qDebug() << "localuuid is " << localuuid << ", backupUUID is " << backupPoint.m_uuid;
                    continue;
                } else {
                    isOther = true;
                }

            }
        }

        m_tableWidget->insertRow(indexOfRow);
        setItem(indexOfRow, Column_Index::Backup_Name, backupPoint.m_backupName);
        setItem(indexOfRow, Column_Index::UUID, backupPoint.m_uuid);
        // setItem(indexOfRow, Column_Index::Backup_Time, backupPoint.m_time);
        setItem(indexOfRow, Column_Index::Backup_Time, m_dateUtils.TranslateDateFormat(QString("20") + backupPoint.m_time));
        setItem(indexOfRow, Column_Index::Backup_Size, backupPoint.m_size);

        QString prefixPath_to_device;
        if (backupPoint.m_iPosition == BackupPosition::CUSTOMIZE) {
            prefixPath_to_device = QObject::tr("Customize:") + backupPoint.m_path + BACKUP_SNAPSHOTS_PATH;
        } else if (backupPoint.m_path.startsWith(preDevPath)) {
            QStorageInfo storage(backupPoint.m_path);
            QString udiskName = storage.rootPath();
            if (isOther)
                prefixPath_to_device = QObject::tr("Other machine:") + " " + udiskName + BACKUP_SNAPSHOTS_PATH;
            else
                prefixPath_to_device = QObject::tr("Udisk Device:") + " " + udiskName + BACKUP_SNAPSHOTS_PATH;
        } else {
            prefixPath_to_device = QObject::tr("Local Disk:") + " " + BACKUP_SNAPSHOTS_PATH;
        }
        prefixPath_to_device.replace("//", "/");
        setItem(indexOfRow, Column_Index::Backup_Device, prefixPath_to_device);
        setItem(indexOfRow, Column_Index::Backup_State, backupPoint.m_state);
        setItem(indexOfRow, Column_Index::Prefix_Path, backupPoint.m_path);

        ++indexOfRow;
    }

    if (0 < indexOfRow) {
        m_tableWidget->setSortingEnabled(true); // 等录入完数据后再排序
        m_tableWidget->sortItems(Column_Index::Backup_Time, Qt::DescendingOrder);
        m_tableWidget->horizontalHeader()->setVisible(true);
    } else {
        m_labelEmpty->setVisible(true);
        m_labelEmptyText->setVisible(true);
        m_tableWidget->horizontalHeader()->setVisible(false);
    }
}


