#include "widget.h"
#include "playthread.h"
#include "global.h"

#include <QVBoxLayout>
#include <QHBoxLayout>
#include <QSplitter>
#include <QLabel>
#include <QLineEdit>
#include <QPushButton>
#include <QComboBox>

Widget::Widget(QWidget *parent)
    : QWidget(parent)
{
    initUI();
    createConnections();
}

Widget::~Widget()
{
}

void Widget::openMedia()
{
    QString str = mPathLineEdit->text();
    if(str.isEmpty())
        return;

    HMedia media;
    media.src = str.toStdString();
    int index = mTypeComboBox->currentIndex();
    if(index == 0)
    {
        media.type = MEDIA_TYPE_FILE;
    }
    else
    {
        media.type = MEDIA_TYPE_NETWORK;
    }
    mPlayThread->setMedia(media);
    mPlayThread->start();

    Global::Instance()->setIndex(index);
    Global::Instance()->setPath(str);
}

void Widget::closeMedia()
{
    if(mPlayThread->isRunning())
    {
        mPlayThread->setPlayState(-1);
    }
}

void Widget::pauseMedia()
{
    if(mPlayThread->isRunning())
    {
        mPlayThread->setPlayState(0);
    }
}

void Widget::drawFrame(QImage frame)
{
    QImage img = frame.scaled(mVideoLabel->size());
    mVideoLabel->setPixmap(QPixmap::fromImage(img));

}

void Widget::initUI()
{
    mMainHLayout = new QHBoxLayout;

    mSplitter = new QSplitter;
    mSplitter->setOrientation(Qt::Horizontal);

    mVideoLabel = new QLabel;
    mVideoLabel->setObjectName("VideoLabel");
    mVideoLabel->setAlignment(Qt::AlignCenter);
    mVideoLabel->setText("视频播放");
    mVideoLabel->setSizePolicy(QSizePolicy::Ignored, QSizePolicy::Ignored);
    mVideoLabel->setScaledContents(true);

    mFuncWidget = new QWidget;

    mFuncVLayout = new QVBoxLayout;

    mTypeComboBox = new QComboBox;
    mTypeComboBox->addItem("本地视频");
    mTypeComboBox->addItem("流媒体");

    mPathHLayout = new QHBoxLayout;
    mPathLabel = new QLabel;
    mPathLabel->setText("路径");
    mPathLineEdit = new QLineEdit;
    mPathLineEdit->setText("E:/video/yellow.mp4");
    mSelectPathButton = new QPushButton;
    mSelectPathButton->setText("选择");
    mPathHLayout->addWidget(mPathLabel);
    mPathHLayout->addWidget(mPathLineEdit);
    mPathHLayout->addWidget(mSelectPathButton);

    mButtonHLayout = new QHBoxLayout;
    mPlayButton = new QPushButton("播放");
    mPauseButton = new QPushButton("暂停");
    mStopButton = new QPushButton("停止");
    mButtonHLayout->addWidget(mPlayButton);
    mButtonHLayout->addWidget(mPauseButton);
    mButtonHLayout->addWidget(mStopButton);

    mFuncVLayout->addWidget(mTypeComboBox);
    mFuncVLayout->addLayout(mPathHLayout);
    mFuncVLayout->addLayout(mButtonHLayout);
    mFuncWidget->setLayout(mFuncVLayout);

    mSplitter->addWidget(mVideoLabel);
    mSplitter->addWidget(mFuncWidget);
    mSplitter->setStretchFactor(0, 4);
    mSplitter->setStretchFactor(1, 1);

    mMainHLayout->addWidget(mSplitter);
    this->setLayout(mMainHLayout);
    this->resize(1000, 400);

    mPlayThread = new PlayThread;

    int index = Global::Instance()->index();
    QString path = Global::Instance()->path();
    if(index == 0)
    {
        mTypeComboBox->setCurrentIndex(0);
    }
    else
    {
        mTypeComboBox->setCurrentIndex(1);
    }
    mPathLineEdit->setText(path);
}

void Widget::createConnections()
{
    connect(mPlayThread, &PlayThread::sendFrame, this, &Widget::drawFrame);

    connect(mPlayButton, &QPushButton::clicked, this, &Widget::openMedia);
    connect(mPauseButton, &QPushButton::clicked, this, &Widget::pauseMedia);
    connect(mStopButton, &QPushButton::clicked, this, &Widget::closeMedia);
}




