/* -*- Mode: C++; indent-tabs-mode: nil; tab-width: 4 -*-
 * -*- coding: utf-8 -*-
 *
 * Copyright (C) 2025 KylinSoft Co., Ltd.
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published
 * the Free Software Foundation, either version 3 of the License, or
 * any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

#include "Ukcc5DevicePortItem.h"

#include <QHBoxLayout>
#include <QDebug>

Ukcc5DevicePortItem::Ukcc5DevicePortItem(QLabel* label, QLabel* iconLabel, const QString& devName, const QString& portName, QWidget* parent) :
    m_pDisplayLabel(label), m_pIconLabel(iconLabel), IDevicePortItem(devName, portName), QWidget(parent)
{
    initUi();
}

void Ukcc5DevicePortItem::initUi()
{
    setFixedHeight(36);
    this->setContentsMargins(0, 0, 0, 0);

    m_pDisplayLabel->setFixedHeight(28);
    auto hLayout = new QHBoxLayout;
    hLayout->addItem(new QSpacerItem(2, 10, QSizePolicy::Fixed));
    hLayout->addWidget(m_pDisplayLabel);
    hLayout->addStretch();
    hLayout->addWidget(m_pIconLabel);
    hLayout->addItem(new QSpacerItem(16, 90, QSizePolicy::Fixed));
    hLayout->setContentsMargins(0, 4, 0, 4);
    this->setLayout(hLayout);
}

bool Ukcc5DevicePortItem::isChecked()
{
    return m_bIsChecked;
}

void Ukcc5DevicePortItem::setChecked(bool checked)
{
    m_bIsChecked = checked;

    if (checked) {
        setIcon(QIcon::fromTheme("ukui-selected"));
    }
    else {
        setIcon(QIcon::fromTheme(""));
    }
}

void Ukcc5DevicePortItem::setIcon(const QIcon& icon)
{
    m_pIconLabel->setPixmap(icon.pixmap(icon.actualSize(QSize(16, 16))));
}

QWidget* Ukcc5DevicePortItem::getWidget()
{
    return this;
}
