#include "sidepanel.h"
#include "iconhelper.h"
#include "treewidget.h"
#include "ctextedit.h"

#include <QVBoxLayout>
#include <QPainter>
#include <QToolButton>
#include <QSplitter>
#include <QTabWidget>

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

SidePanel::~SidePanel()
{

}

void SidePanel::paintEvent(QPaintEvent *e)
{
    Q_UNUSED(e);

    QStyleOption opt;
    opt.init(this);
    QPainter p(this);
    style()->drawPrimitive(QStyle::PE_Widget, &opt, &p, this);
}

void SidePanel::initUI()
{
    mMainVLayout = new QVBoxLayout;

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

    mTreeWidget = new TreeWidget;

//    QColor normalTextColor = QColor("#FFFFFF");
    QColor normalTextColor1 = QColor("#000000");
    QColor normalTextColor2 = QColor("#FFFFFF");
    QColor hoverTextColor = QColor("#FFFFFF");
    QColor checkTextColor = QColor("#FFFFFF");

    int pixSize = 30;
    int imgWidth = 40, imgHeight = 40;

    QPixmap start = IconHelper::Instance()->getPixmap(normalTextColor1.name(), 0xf01d, pixSize, imgWidth, imgHeight);
    QPixmap stop = IconHelper::Instance()->getPixmap(normalTextColor1.name(), 0xf011, pixSize, imgWidth, imgHeight);
    QPixmap reset = IconHelper::Instance()->getPixmap(normalTextColor2.name(), 0xf01e, pixSize, imgWidth, imgHeight);

    mSetupButton = new QToolButton;
    mSetupButton->setIcon(QIcon(start));
    mSetupButton->setIconSize(QSize(40, 40));
    mSetupButton->setObjectName("SidePanelSetupButton");
    mSetupButton->setFixedSize(QSize(200, 50));
    mSetupButton->setText("启动");
    mSetupButton->setToolButtonStyle(Qt::ToolButtonTextBesideIcon);
    mPauseButton = new QToolButton;
    mPauseButton->setIcon(QIcon(stop));
    mPauseButton->setIconSize(QSize(40, 40));
    mPauseButton->setObjectName("SidePanelPauseButton");
    mPauseButton->setFixedSize(QSize(200, 50));
    mPauseButton->setText("暂停");
    mPauseButton->setToolButtonStyle(Qt::ToolButtonTextBesideIcon);
    mWaitButton = new QToolButton;
    mWaitButton->setIcon(QIcon(reset));
    mWaitButton->setIconSize(QSize(40, 40));
    mWaitButton->setObjectName("SidePanelWaitButton");
    mWaitButton->setFixedSize(QSize(200, 50));
    mWaitButton->setText("等待");
    mWaitButton->setToolButtonStyle(Qt::ToolButtonTextBesideIcon);

    mTabWidget = new QTabWidget;
    mTabWidget->setMovable(true);
    mTabWidget->setTabPosition(QTabWidget::TabPosition::South);

    mButtonWidget = new QWidget;
    mButtonVLayout = new QVBoxLayout;
    mButtonVLayout->setAlignment(Qt::AlignCenter);
    mButtonVLayout->addWidget(mSetupButton);
    mButtonVLayout->addWidget(mPauseButton);
    mButtonVLayout->addWidget(mWaitButton);
    mButtonWidget->setLayout(mButtonVLayout);

    mTextEdit = new CTextEdit;
    mTextEdit->setObjectName("MainWindowLogTextEdit");
    QString logStr = "十月的雨，总会让人不自觉地悲伤起来，正值秋天，本就是悲伤，\n"
                     "更何况是下着毛毛细雨呢？我敬佩刘禹锡，他豁达乐观，即使在秋天，也能吟上一句“自古逢秋悲寂寥，\n"
                     "我言秋日胜春朝。晴空一鹤排云上，便引诗情到碧霄”而我就只能在秋天独自忧愁，独自忧伤，独自怀念。\n"
                     "人是群居动物，总会感到些许寂寞，即便是习惯了寂寞的人也是。我不怕寂寞，却也怕在人群中明明是笑着的，\n"
                     "内心却还是寂寞，这种寂寞是最可怕的。\n";
    mTextEdit->append(logStr);
    mTabWidget->addTab(mButtonWidget, "功能");
    mTabWidget->addTab(mTextEdit, "日志");

    mSplitter->addWidget(mTreeWidget);
    mSplitter->addWidget(mTabWidget);
    mSplitter->setStretchFactor(0, 10);
    mSplitter->setStretchFactor(1, 2);

    mMainVLayout->addWidget(mSplitter);

    this->setObjectName("LeftPanel");
    this->setLayout(mMainVLayout);
}

void SidePanel::createConnections()
{
    connect(mTreeWidget, &TreeWidget::signalClicked, this, &SidePanel::signalClicked);
}

