#include "FormChargingProfilesSettings.h"
#include "ui_FormChargingProfilesSettings.h"

#include <QScroller>

#include "Globals.h"
#include "ConfigManager.h"
#include "LanguageManager.h"

FormChargingProfilesSettings::FormChargingProfilesSettings(BaseWidget *parent)
    : BaseWidget(parent)
    , ui(new Ui::FormChargingProfilesSettings)
{
    ui->setupUi(this);

    initWidget();
    initConnect();
}

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

bool FormChargingProfilesSettings::returnParent()
{
    return true;
}

void FormChargingProfilesSettings::initWidget()
{
    initTableWidget();

    m_charging_profile_info.reset(new DialogChargingProfileEdit());
    m_charging_profile_info->setParent(this);
    m_charging_profile_info->setWindowFlags(Qt::Window | Qt::FramelessWindowHint | Qt::WindowStaysOnTopHint);
    m_charging_profile_info->setWindowModality(Qt::WindowModal);

    ui->del->setVisible(false);
    ui->add->setVisible(false);
}

void FormChargingProfilesSettings::initTableWidget()
{
    // ui->chargingProfileTableWidget->horizontalHeader()->hide();
    ui->chargingProfileTableWidget->verticalHeader()->hide();

    ui->chargingProfileTableWidget->horizontalHeader()->setSectionResizeMode(0, QHeaderView::ResizeToContents);
    ui->chargingProfileTableWidget->horizontalHeader()->setSectionResizeMode(1, QHeaderView::Stretch);
    ui->chargingProfileTableWidget->horizontalHeader()->setSectionResizeMode(2, QHeaderView::Stretch);
    ui->chargingProfileTableWidget->horizontalHeader()->setSectionResizeMode(3, QHeaderView::ResizeToContents);
    ui->chargingProfileTableWidget->horizontalHeader()->setSectionResizeMode(4, QHeaderView::ResizeToContents);

    ui->chargingProfileTableWidget->setEditTriggers(QAbstractItemView::NoEditTriggers);
    ui->chargingProfileTableWidget->setSelectionBehavior(QAbstractItemView::SelectRows);
    ui->chargingProfileTableWidget->setSelectionMode(QAbstractItemView::SingleSelection);

    QScroller::grabGesture(ui->chargingProfileTableWidget,              QScroller::TouchGesture);
    QScroller::grabGesture(ui->chargingProfileTableWidget->viewport(),  QScroller::LeftMouseButtonGesture);
}

void FormChargingProfilesSettings::initConnect()
{
    connect(LanguageManager::instance(),    &LanguageManager::languageChanged,  this, [this]() { ui->retranslateUi(this); });
    connect(ui->chargingProfileTableWidget, &QTableWidget::itemDoubleClicked,   this, [this](QTableWidgetItem* item){
        emit currentFormChanged(m_charging_profiles.chargingProfiles[item->row()]);
    });
    connect(GLOBALS, &Globals::chargingProfilesValueChanged,                    this, [this](const CHARGING_PROFILES& info) {
        m_charging_profiles = info;
        refreshData();
    });
    connect(ui->chargingProfileTableWidget, &QTableWidget::itemChanged,         this, [this]() {
        if (ui->chargingProfileTableWidget->rowCount() > 0 && ui->chargingProfileTableWidget->rowCount() < ui->count->value()) {
            ui->del->setVisible(true);
            ui->add->setVisible(true);
        }
        if (ui->chargingProfileTableWidget->rowCount() >= ui->count->value()) {
            ui->add->setVisible(false);
            ui->del->setVisible(true);
        }
        if (ui->chargingProfileTableWidget->rowCount() == 0) {
            ui->add->setVisible(true);
            ui->del->setVisible(false);
        }
    });

    connect(ui->add, &QPushButton::clicked,                                     this, [this]() {
        auto ret = m_charging_profile_info->exec();
        if (ret == QDialog::Accepted) {
            charging_profiles_detail_info_t info = m_charging_profile_info->getChargingProfile();
            m_charging_profiles.chargingProfiles.append(info);

            refreshData();
            emit currentFormChanged(info);
            ConfigManager::instance()->saveConfig(CHARGING_PROFILE, QVariant::fromValue(m_charging_profiles));
        }
    });
    connect(ui->del, &QPushButton::clicked,                                     this, [this]() {
        auto row = ui->chargingProfileTableWidget->currentRow();
        m_charging_profiles.chargingProfiles.removeAt(row);

        refreshData();
        ConfigManager::instance()->saveConfig(CHARGING_PROFILE, QVariant::fromValue(m_charging_profiles));
    });
}

void FormChargingProfilesSettings::refreshData()
{
    ui->count->setValue(m_charging_profiles.count);
    ui->maxLength->setValue(m_charging_profiles.maxLength);
    refreshTableWidget();
}

void FormChargingProfilesSettings::refreshTableWidget()
{
    ui->chargingProfileTableWidget->clearContents();
    ui->chargingProfileTableWidget->setRowCount(0);
    foreach (const auto& data, m_charging_profiles.chargingProfiles) {
        tableWidgetAppendRow(data.connectorId, data.chargingProfilePurpose, data.chargingProfileKind, data.recurrencyKind, data.stackLevel);
    }

    ui->chargingProfileTableWidget->setCurrentItem(nullptr);
}

void FormChargingProfilesSettings::tableWidgetAppendRow(const int &connectorId, const QString &chargingProfilePurpose, const QString &chargingProfileKind, const QString &recurrencyKind, const int &stackLevel)
{
    auto row = ui->chargingProfileTableWidget->rowCount();
    ui->chargingProfileTableWidget->insertRow(row);

    auto connectorIdItem = new QTableWidgetItem(QString::number(connectorId));
    connectorIdItem->setTextAlignment(Qt::AlignCenter);
    ui->chargingProfileTableWidget->setItem(row, 0, connectorIdItem);

    auto purposeItem = new QTableWidgetItem(chargingProfilePurpose);
    purposeItem->setTextAlignment(Qt::AlignCenter);
    ui->chargingProfileTableWidget->setItem(row, 1, purposeItem);

    auto kindItem = new QTableWidgetItem(chargingProfileKind);
    kindItem->setTextAlignment(Qt::AlignCenter);
    ui->chargingProfileTableWidget->setItem(row, 2, kindItem);

    auto recurrencyKindItem = new QTableWidgetItem(recurrencyKind);
    recurrencyKindItem->setTextAlignment(Qt::AlignCenter);
    ui->chargingProfileTableWidget->setItem(row, 3, recurrencyKindItem);

    auto stackLevelItem = new QTableWidgetItem(QString::number(stackLevel));
    stackLevelItem->setTextAlignment(Qt::AlignCenter);
    ui->chargingProfileTableWidget->setItem(row, 4, stackLevelItem);

    ui->chargingProfileTableWidget->setRowHeight(row, 60);
}

void FormChargingProfilesSettings::showEvent(QShowEvent *event)
{
    setIsModify(false);
    m_charging_profiles = ConfigManager::instance()->charging_profiles();
    refreshData();
    BaseWidget::showEvent(event);
}
