/*
 * 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 "ukmedia_application_volume_widget.h"
#include <QDebug>
#include <QScrollBar>
#include <QHBoxLayout>
#include <QSpacerItem>
#include <QStyleOption>
#include <QPainter>
ApplicationVolumeWidget::ApplicationVolumeWidget(QWidget *parent) : QWidget (parent)
{
    connect(qApp, &QApplication::paletteChanged, this, &ApplicationVolumeWidget::onPaletteChanged);
    applicationLabel = new QLabel(tr("Application Volume"),this);
    app_volume_list = new QStringList;
    systemVolumeLabel = new QLabel(tr("System Volume"),this);
    systemVolumeBtn = new UkuiMediaMuteButton(this);
    systemVolumeSlider = new UkmediaVolumeSlider(this);
    systemVolumeDisplayLabel = new QLabel(this);
    systemVolumeSliderWidget = new QWidget(this);

    QPalette palette = applicationLabel->palette();
    QColor color = palette.color(QPalette::PlaceholderText);
    palette.setColor(QPalette::WindowText,color);
    applicationLabel->setPalette(palette);
    systemVolumeLabel->setPalette(palette);  

    //系统音量label
    QWidget *systemVolumeLabelWidget = new QWidget(this);
    systemVolumeLabelWidget->setFixedSize(404,20);
    systemVolumeLabel->setFixedSize(404,20);
    QHBoxLayout *systemVolumeLabelLayout = new QHBoxLayout;
    systemVolumeLabelLayout->addWidget(systemVolumeLabel);
    systemVolumeLabelWidget->setLayout(systemVolumeLabelLayout);
    systemVolumeLabelLayout->setContentsMargins(16,0,0,0);

    //应用label
    QWidget *applicationLabelWidget = new QWidget(this);
    applicationLabel->setFixedSize(404,20);
    applicationLabelWidget->setFixedSize(404,20);
    QHBoxLayout *applicationLabelLayout = new QHBoxLayout;
    applicationLabelLayout->addWidget(applicationLabel);
    applicationLabelWidget->setLayout(applicationLabelLayout);
    applicationLabelLayout->setContentsMargins(16,0,0,0);

    //音量滑动条
    QHBoxLayout *systemVolumeSliderLayout = new QHBoxLayout();
    systemVolumeSliderWidget->setFixedSize(412,48);
    systemVolumeBtn->setFixedSize(36,36);
    systemVolumeBtn->setActive(true);
    systemVolumeSlider->setOrientation(Qt::Horizontal);
    systemVolumeSlider->setRange(0,100);
    systemVolumeSlider->setFixedSize(273,48);
    systemVolumeDisplayLabel->setFixedSize(55,48);
    systemVolumeDisplayLabel->setAlignment(Qt::AlignCenter);
    systemVolumeSliderLayout->addWidget(systemVolumeBtn);
    systemVolumeSliderLayout->addSpacing(8);
    systemVolumeSliderLayout->addWidget(systemVolumeSlider);
    systemVolumeSliderLayout->addSpacing(16);
    systemVolumeSliderLayout->addWidget(systemVolumeDisplayLabel);
    systemVolumeSliderLayout->setSpacing(0);
    systemVolumeSliderWidget->setLayout(systemVolumeSliderLayout);
    systemVolumeSliderLayout->setContentsMargins(16,0,24,0);
    systemVolumeSlider->setObjectName("systemVolumeSlider");
    this->setFixedSize(420,436);

    volumeSettingButton = new UkuiSettingButton(volumeSettingFrame);
    volumeSettingButton->setText(tr("Sound Settings"));
    volumeSettingButton->setCursor(Qt::PointingHandCursor);
    volumeSettingButton->setScaledContents(true);

    volumeSettingFrame = new QFrame(this);
    volumeSettingFrame->setFixedHeight(52);
    QHBoxLayout *volumeSettingLayout = new QHBoxLayout(volumeSettingFrame);
    volumeSettingLayout->setContentsMargins(16,0,24,8);
    volumeSettingLayout->addWidget(volumeSettingButton);
    volumeSettingLayout->addStretch();
    volumeSettingFrame->setLayout(volumeSettingLayout);

    appArea = new QScrollArea(this);
    displayAppVolumeWidget = new UkuiApplicationWidget(appArea);
    appArea->setWidget(displayAppVolumeWidget);
    QPalette palette1 = appArea->palette();
    palette1.setColor(QPalette::Window, QColor(0x00,0xff,0x00,0x00));  //改变appArea背景色透明
    appArea->setPalette(palette1);
    appArea->verticalScrollBar()->setProperty("drawScrollBarGroove",false);
//    appWidget->appArea->verticalScrollBar()->setFixedSize(2,70);       //设置垂直滑动条大小和背景色，因为设置完不美观，暂时注释掉
//    appWidget->appArea->verticalScrollBar()->setBackgroundRole(QPalette::Light);
    displayAppVolumeWidget->setFixedWidth(404);
    m_pVlayout = new QVBoxLayout(displayAppVolumeWidget);

    displayAppVolumeWidget->setAttribute(Qt::WA_TranslucentBackground);
    appArea->setFixedSize(404,245);
    displayAppVolumeWidget->setLayout(m_pVlayout);
    this->setObjectName("mainWidget");
    setObjectName("appWidget");
    appArea->setFrameShape(QFrame::NoFrame);//bjc去掉appArea的边框
    appArea->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);

    //整体布局
    QVBoxLayout *mainLayout = new QVBoxLayout;
    mainLayout->addWidget(systemVolumeLabelWidget);
    mainLayout->addSpacing(8);
    mainLayout->addWidget(systemVolumeSliderWidget);
    mainLayout->addSpacing(17);
    mainLayout->addWidget(applicationLabelWidget);
    mainLayout->addSpacing(8);
    mainLayout->addWidget(appArea);
    mainLayout->addSpacing(8);
    mainLayout->addWidget(volumeSettingFrame);
    mainLayout->setSpacing(0);
    this->setLayout(mainLayout);
    mainLayout->setContentsMargins(8,16,0,0);
}

void ApplicationVolumeWidget::fullushBlueRecordStream()
{
    isRecording = true;
    outputFile.setFileName("/tmp/test.raw");
    outputFile.open( QIODevice::WriteOnly | QIODevice::Truncate );
    QAudioFormat format;

    format.setSampleRate(8000);
    format.setChannelCount(1);
    format.setSampleSize(8);
    format.setCodec("audio/pcm");
    format.setByteOrder(QAudioFormat::LittleEndian);

    format.setSampleType(QAudioFormat::UnSignedInt);
    QAudioDeviceInfo info = QAudioDeviceInfo::defaultInputDevice();
    qDebug() << "input device" << info.deviceName();
    if (!info.isFormatSupported(format))
    {
       qWarning()<<"default format not supported try to use nearest";
       format = info.nearestFormat(format);
    }
    qDebug() << "准备蓝牙录音-------";
    audio = new QAudioInput(info, format, this);
    audio->start(&outputFile);
}

void ApplicationVolumeWidget::deleteBlueRecordStream()
{
    isRecording = false;
    qDebug() << "停止录制-------";
    audio->stop();
    outputFile.close();
    system("rm /tmp/test.raw");
    delete audio;
}

void ApplicationVolumeWidget::onPaletteChanged(){
    QPalette palette = applicationLabel->palette();
    QColor color = palette.color(QPalette::PlaceholderText);
    palette.setColor(QPalette::WindowText,color);
    applicationLabel->setPalette(palette);
    systemVolumeLabel->setPalette(palette);
}

ApplicationVolumeWidget::~ApplicationVolumeWidget()
{
//    delete  app_volume_list;
//    delete  appIconBtn;
}

