/*
 * Copyright (C) 2019 Tianjin KYLIN Information Technology 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 by
 * the Free Software Foundation; either version 3, or (at your option)
 * 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/&gt;.
 *
 */
#include "ukui_list_widget_item.h"

#include <QPainter>
#include <QStyleOption>
#include <QDebug>
#include <QHBoxLayout>
//#include <QSvgRenderer>
#include <QApplication>
#include <QDebug>
#include <QPainterPath>
bool isCheckBluetoothInput;

UkuiListWidgetItem::UkuiListWidgetItem(QWidget *parent) :
    QWidget(parent)
{
    this->setFixedSize(388,48);
    QHBoxLayout *hLayout = new QHBoxLayout;

    deviceButton = new UkuiMediaButton(this);
    portLabel = new FixLabel(this);
    deviceLabel = new QLabel(this);

    deviceButton->setFixedSize(36,36);
    portLabel->setFixedSize(310,24);
    deviceLabel->setFixedSize(306,24);
    deviceLabel->hide();
    hLayout->addWidget(deviceButton);
    hLayout->addSpacing(10);
    hLayout->addWidget(portLabel);
    hLayout->setSpacing(0);

    this->setLayout(hLayout);
    hLayout->setContentsMargins(16,6,16,6);

    this->show();
}

UkuiListWidgetItem::~UkuiListWidgetItem()
{
}

void UkuiListWidgetItem::paintEvent(QPaintEvent *event)
{
    QStyleOption opt;
    opt.init(this);
    QPainter p(this);
    QColor color ;
    switch (state) {
    case WIDGET_NORMAL_STATE:
        color = QColor(0xF0,0xF0,0xF0,0x00);
        break;
    case WIDGET_HOVER_STATE:
        color = QColor(200,200,200,50);
        break;
    }

//    color.setAlpha(transparence);
    QBrush brush = QBrush(color);
    p.setBrush(brush);
    p.setPen(Qt::NoPen);
    QPainterPath path;
    opt.rect.adjust(0,0,0,0);
    path.addRoundedRect(opt.rect,6,6);
    p.setRenderHint(QPainter::Antialiasing);  // 反锯齿;
    p.drawRoundedRect(opt.rect,6,6);
    setProperty("blurRegion",QRegion(path.toFillPolygon().toPolygon()));
    style()->drawPrimitive(QStyle::PE_Widget, &opt, &p, this);
    QWidget::paintEvent(event);
}


void UkuiListWidgetItem::setSelected(bool selected){

}

void UkuiListWidgetItem::leaveEvent(QEvent *e)
{
    state = WIDGET_NORMAL_STATE;
    this->update();
    QWidget::leaveEvent(e);
}
void UkuiListWidgetItem::mouseReleaseEvent(QMouseEvent *e)
{
//    state = WIDGET_NORMAL_STATE;
    QWidget::mouseReleaseEvent(e);
}
void UkuiListWidgetItem::enterEvent(QEvent *e)
{
    state = WIDGET_HOVER_STATE;
    this->update();
    QWidget::enterEvent(e);
}

void UkuiListWidgetItem::setButtonIcon(QString str)
{
    QString iconStr;
    if (str.contains("headphone") || str.contains("Headphone") || str.contains("headset")) {
        iconStr = "audio-headphones-symbolic";
    }
    else {
        iconStr = "audio-volume-high-symbolic";
    }
    deviceButton->setButtonIcon(QIcon::fromTheme(iconStr));
    deviceButton->setProperty("useIconHighlightEffect", 0x8);
}

void UkuiListWidgetItem::setLabelText(QString portLabel, QString deviceLabel){

    this->portLabel->setText(portLabel,true);
    this->deviceLabel->setText(deviceLabel);
}

void UkuiListWidgetItem::mousePressEvent(QMouseEvent *event)
{

    QWidget::mousePressEvent(event);
    qDebug() << "Mouse Press Event" << this->portLabel->fullText << this->deviceLabel->text();
    //蓝牙输入去除勾选
    if (this->deviceLabel->text().contains("bluez_card")) {
        if (isCheckBluetoothInput == false)
            isCheckBluetoothInput = true;
        else  {
            isCheckBluetoothInput = false;
            QString cmd = "pactl set-card-profile "+this->deviceLabel->text()+" a2dp_sink";
            system(cmd.toLocal8Bit().data());
        }
    }
}
