#include "DialogSelectChargingProfiles.h"
#include "ui_DialogSelectChargingProfiles.h"

#include <QScreen>
#include <QTimer>
#include <QWindow>
#include <QDebug>
#include <QIntValidator>
#include <QPropertyAnimation>

#include "LanguageManager.h"
#include "DataTypeDef.h"

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

    initWidget();
    initConnect();
}

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

QString DialogSelectChargingProfiles::getPurpose()
{
    return ui->chargingProfilePurpose->currentText();
}

void DialogSelectChargingProfiles::initWidget()
{
    setWindowModality(Qt::WindowModal);
    setAttribute(Qt::WA_TranslucentBackground);
    setWindowFlags(Qt::Window | Qt::FramelessWindowHint | Qt::WindowStaysOnTopHint);

    setBackground();

    initComboBox();
}

void DialogSelectChargingProfiles::initConnect()
{
    connect(ui->cancel, &QPushButton::clicked,  this, [this]() {
        reject();
    });
    connect(ui->ok, &QPushButton::clicked,      this, [this]() {
        accept();
    });
    connect(LanguageManager::instance(), &LanguageManager::languageChanged,     this, [this]() { ui->retranslateUi(this); });
}

void DialogSelectChargingProfiles::initComboBox()
{
    QStringList items;
    items << ChargingProfiles::Purpose::CHARGE_POINT_MAX_PROFILE
          << ChargingProfiles::Purpose::TX_DEFAULT_PROFILE
          << ChargingProfiles::Purpose::TX_PROFILE;

    ui->chargingProfilePurpose->addItems(items);
}

void DialogSelectChargingProfiles::setBackground()
{
    m_background = new QLabel();
    ui->background_layout->addWidget(m_background, 0, 0, ui->background_layout->rowCount(), ui->background_layout->columnCount());
    m_background->setPixmap(QPixmap(":/images/price/dialog_edit_background.png"));
    m_background->lower();
    m_background->setScaledContents(true);
}

void DialogSelectChargingProfiles::centerWindow()
{
    // 确保窗口尺寸已确定（关键修复）
    this->ensurePolished();
    // this->adjustSize();

    // 获取有效的屏幕几何尺寸（考虑多显示器）
    QScreen *screen = QGuiApplication::primaryScreen();
    if (QWindow *window = this->windowHandle()) {
        screen = window->screen();
    }
    QRect screenGeometry = screen->availableGeometry();

    // 计算居中位置（考虑Wayland特性）
    int x = (screenGeometry.width() - this->width()) / 2 + screenGeometry.x();
    int y = (screenGeometry.height() - this->height()) / 2 + screenGeometry.y();

    if (QGuiApplication::platformName().contains("wayland")) {
        // Wayland专用处理
        if (QWindow *window = this->windowHandle()) {
            // 重要：延迟设置位置，确保Wayland合成器已准备好
            QTimer::singleShot(100, this, [window, x, y]() {
                window->setPosition(QPoint(x, y));

                // 替代requestActivate的方案
                window->setFlags(window->flags() | Qt::WindowStaysOnTopHint);
                QTimer::singleShot(50, [window]() {
                    window->setFlags(window->flags() & ~Qt::WindowStaysOnTopHint);
                });
            });
        }
    } else {
        // 传统X11处理
        this->move(x, y);
    }
}

void DialogSelectChargingProfiles::showEvent(QShowEvent *event)
{
    centerWindow();
    QDialog::showEvent(event);
}
