#include "chartpanel.h"
#include "averagechartpanel.h"
#include "instantchartpanel.h"
#include "piechart.h"
#include "ctextedit.h"

#include <QHBoxLayout>
#include <QGridLayout>
#include <QLabel>
#include <QPainter>
#include <QStyleOption>
#include <QTabWidget>

ChartPanel::ChartPanel(QWidget *parent)
    : QWidget(parent)
{
    this->setObjectName("ChartPanel");
    mMainHLayout = new QHBoxLayout;

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

    mChartWidget = new QWidget;

    mChartGLayout = new QGridLayout;
    mChartWidget->setLayout(mChartGLayout);

    mAverageChartPanel = new AverageChartPanel(this);
    mInstantChartPanel = new InstantChartPanel(this);
    mPieChart = new PieChart(this);
    mLabel1 = new QLabel(this);
    mLabel1->setText("I am Active");
    mLabel1->setAlignment(Qt::AlignCenter);
    mChartGLayout->addWidget(mAverageChartPanel, 0, 0);
    mChartGLayout->addWidget(mInstantChartPanel, 0, 1);
    mChartGLayout->addWidget(mPieChart, 1, 0);
    mChartGLayout->addWidget(mLabel1, 1, 1);
    for( int i = 0 ; i < 2; i++ )
    {
        mChartGLayout->setRowStretch( i, 1);
        for ( int j = 0; j < 2; j++)
        {
            if( i == 0 )
            {
                mChartGLayout->setColumnStretch( j, 1 );
            }

        }
    }

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

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

    mLabel2 = new QLabel;
    mLabel2->setObjectName("ChartPanelLabel");
    mLabel2->setText("此处不好添加停靠功能，添加的话，会大改");
    mLabel2->setAlignment(Qt::AlignCenter);

    mTabWidget->addTab(mLogTextEdit, "日志");
    mTabWidget->addTab(mLabel2, "十月的雨");

    mSplitter->addWidget(mChartWidget);
    mSplitter->addWidget(mTabWidget);
    mSplitter->setStretchFactor(0, 3);
    mSplitter->setStretchFactor(1, 1);

    this->setLayout(mMainHLayout);

}

void ChartPanel::paintEvent(QPaintEvent *)
{
    QStyleOption opt;
    opt.init(this);
    QPainter p(this);
    style()->drawPrimitive(QStyle::PE_Widget, &opt, &p, this);
}
