#include "mainwindow.h"
#include "ui_mainwindow.h"

#include "fancybar.h"
#include "global.h"
#include "sidepanel.h"
#include "camerapanel.h"
#include "iconhelper.h"
#include "additionalwidget.h"
#include "cmessagebox.h"
#include "chartpanel.h"
#include "cstackedwidget.h"
#include "cameraparam.h"
#include "logininfo.h"
#include "popupmenu.h"
#include "logwidget.h"
#include "loginwidget.h"
#include "setupwaitprogressbar.h"
#include "tablewidget.h"
#include "funcbuttonwidget.h"
#include "treewidget.h"
#include "ctextedit.h"
#include "funcwidget1.h"

#include <QHBoxLayout>
#include <QFrame>
#include <QToolButton>
#include <QPushButton>
#include <QSplitter>
#include <QTextEdit>
#include <QStackedWidget>
#include <QMessageBox>
#include <QPropertyAnimation>


MainWidget::MainWidget(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::MainWidget)
{
    ui->setupUi(this);
    ui->horizontalLayout->setSpacing(0);
    ui->horizontalLayout->setMargin(0);
    ui->horizontalLayout->setContentsMargins(0, 0, 0, 0);
}

MainWidget::~MainWidget()
{
    delete ui;
}

MainWindow::MainWindow(QWidget *parent)
    : FancyWindow(parent)
{
    initUI();
    createConnections();
}

MainWindow::~MainWindow()
{
}

void MainWindow::setCameraInitPanel()
{
    QWidget* widget = widgetFromIndex(17);
    mMainWidget->ui->mStackedWidget->setCurrentWidget(widget);
}

void MainWindow::closeEvent(QCloseEvent *e)
{
    CMessageBox msgBox;
    msgBox.setMessageType(CMessageBox::WarningBox);
    msgBox.setButtonNum(2);
    msgBox.setText("确认关闭程序?");
    msgBox.setWindowIcon(QIcon(Global::Instance()->resourceDirPath() + "/image/logo.ico"));
    int state = msgBox.exec();
    if(state == 0)
    {
        e->ignore();
    }
    else
    {
        closeOtherWidget();
    }
}

void MainWindow::initUI()
{
    mMainWidget = new MainWidget;
    for(int i = 1; i <= 16; i++)
    {
        QWidget* widget = widgetFromIndex(i);
        CameraParam* cameraParam = qobject_cast<CameraParam*>(widget);
        QString strIndex = QString("C") + (i < 10 ? ("0" + QString::number(i)) : QString::number(i));
        cameraParam->setIndex(strIndex);
        cameraParam->setTitle("Unkown");
        cameraParam->setState("Unkown");
    }

    QPixmap pix = IconHelper::Instance()->getPixmap("#FEFEFE", 0xf001, 30, 35, 35);
    mStyleAction = new QAction(QIcon(pix), "style", this);
    fancyBar()->addAdditionalControl(mStyleAction, FancyBar::TitlePosition);
    mStyleMenu = new QMenu;
    mDarkBlueAction = new QAction(tr("dark_blue"));
    mDarkGreenAction = new QAction(tr("dark_green"));
    mDarkYellowGreenAction = new QAction(tr("dark_yellow_green"));
    mLightBlueAction = new QAction(tr("light_blue"));
    mLightGreenAction = new QAction(tr("light_green"));
    mStyleMenu->addAction(mDarkBlueAction);
    mStyleMenu->addAction(mDarkGreenAction);
    mStyleMenu->addAction(mDarkYellowGreenAction);
    mStyleMenu->addAction(mLightBlueAction);
    mStyleMenu->addAction(mLightGreenAction);
    mStyleAction->setMenu(mStyleMenu);

    mAdditionalWidget = new AdditionalWidget;
    mAdditionalWidget->setFixedSize(200, 50);
    fancyBar()->addAdditionalControl(mAdditionalWidget, FancyBar::TitlePosition);
    mMiddleButton = new QPushButton("ClickMe");
    mMiddleButton->setFixedSize(100, 30);
    mMiddleButton->setObjectName("MainWindowMiddleButton");
    fancyBar()->setWidgetInMiddle(mMiddleButton);

    connect(mMiddleButton, &QPushButton::clicked, this, [=](){
        QMessageBox msgBox;
        msgBox.setText("What are you talking about?");
        msgBox.exec();
    });

    createDockWidget();

    mLoginInfo = new LoginInfo();
    mLoginInfo->setFixedSize(200, 250);
    mLoginInfo->hide();
    mLoginInfo->setWindowFlags(mLoginInfo->windowFlags() | Qt::WindowStaysOnTopHint);

    mPopupMenu = new PopupMenu;
    // mPopupMenu->setEnabled(false);
    mPopupMenu->setFixedSize(200, 300);
    mPopupMenu->hide();
    mPopupMenu->setWindowFlags(mPopupMenu->windowFlags() | Qt::WindowStaysOnTopHint);

    mLogWidget = new LogWidget;
    mLogWidget->setWindowIcon(QIcon(Global::Instance()->resourceDirPath() + "/image/logo.ico"));
    mLogWidget->setWindowTitle("日志");
    mLogWidget->hide();

    mLoginWidget = new LoginWidget;
    mLoginWidget->setBackgroundImage(Global::Instance()->resourceDirPath() + "/image/login/1.png");
    mLoginWidget->hide();

    mProgressBar = new SetupWaitProgressBar;

    mTableWidget = new TableWidget;
    mTableWidget->setWindowIcon(QIcon(Global::Instance()->resourceDirPath() + "/image/logo.ico"));
    mTableWidget->setWindowTitle("表格");
    mTableWidget->hide();

    mFuncWidget1 = new FuncWidget1;
    mFuncWidget1->setWindowIcon(QIcon(Global::Instance()->resourceDirPath() + "/image/logo.ico"));
    mFuncWidget1->hide();

    this->setCentralWidget(mMainWidget);
    this->fancyBar()->setFixedHeight(50);
    this->fancyBar()->setTitleBarHeight(50);
    this->fancyBar()->setLogoButtonIconSize(QSize(40, 40));
    this->setWindowTitle(tr("windowName"));
    this->setWindowIcon(QIcon(Global::Instance()->resourceDirPath() + "/image/logo.ico"));
}


void MainWindow::createConnections()
{
    connect(mStyleMenu, &QMenu::triggered, this, [=](QAction* action){
        QFile styleFile(QString("%1/style/%2/style.css")
                        .arg(Global::Instance()->resourceDirPath())
                        .arg(action->text()));
        if(styleFile.open(QIODevice::ReadOnly))
        {
            qApp->setStyleSheet(styleFile.readAll());
            styleFile.close();
        }
    });


    connect(mSidePanel, &SidePanel::signalClicked, this, &MainWindow::clickedTreeWidget);

    connect(mAdditionalWidget, &AdditionalWidget::logInfo, this, [=](){
        if(mLogWidget->isVisible())
            mLogWidget->hide();
        else
            mLogWidget->show();
    });

    connect(mAdditionalWidget, &AdditionalWidget::loginInfo, this, [=](){
        mLoginWidget->show();
    });

    connect(mLoginWidget, &LoginWidget::loggedIn, mLoginWidget, &LoginWidget::hide);

    connect(mAdditionalWidget, &AdditionalWidget::menuInfo, this, [=](){
        QPoint p1 = mapToGlobal(mAdditionalWidget->pos());
        QRect start, stop;
        int x1, y1, x2, y2;
        start.setWidth(mPopupMenu->width());
        start.setHeight(mPopupMenu->height());
        stop.setWidth(mPopupMenu->width());
        stop.setHeight(mPopupMenu->height());
        if(!mPopupMenu->isOut())
        {
            mPopupMenu->setOut(true);
            x1 = p1.x();
            y1 = p1.y();
            x2 = x1;
            y2 = p1.y() + mAdditionalWidget->height();
            mPopupMenu->move(x1, y1);
            mPopupMenu->show();
            start.setX(x1);
            start.setY(y1);
            stop.setX(x2);
            stop.setY(y2);

            QPropertyAnimation *animation = new QPropertyAnimation(mPopupMenu, "geometry");
            animation->setDuration(500);
            animation->setStartValue(start);
            animation->setEndValue(stop);
            animation->start();
        }
        else
        {
            mPopupMenu->setOut(false);
            x1 = p1.x();
            y1 = p1.y();
            x2 = x1;
            y2 = p1.y() + mAdditionalWidget->height();
            mPopupMenu->move(x1, y1);
            mPopupMenu->show();
            start.setX(x1);
            start.setY(y1);
            stop.setX(x2);
            stop.setY(y2);

            QPropertyAnimation *animation = new QPropertyAnimation(mPopupMenu, "geometry");
            animation->setDuration(300);
            animation->setStartValue(stop);
            animation->setEndValue(start);
            animation->start();
            connect(animation, &QPropertyAnimation::finished, mPopupMenu, &LoginInfo::hide);
        }


    });

    connect(mPopupMenu, &PopupMenu::clicked1, this, [=](){
        mPopupMenu->hide();
        mPopupMenu->setOut(false);

        mFuncWidget1->show();
    });

    connect(mPopupMenu, &PopupMenu::clicked2, this, [=](){
        mPopupMenu->hide();
        mPopupMenu->setOut(false);
        mTableWidget->show();
    });

    connect(mPopupMenu, &PopupMenu::clickedReturn, this, [=](){
        QPoint p1 = mapToGlobal(mAdditionalWidget->pos());
        QRect start, stop;
        int x1, y1, x2, y2;
        start.setWidth(mPopupMenu->width());
        start.setHeight(mPopupMenu->height());
        stop.setWidth(mPopupMenu->width());
        stop.setHeight(mPopupMenu->height());
        if(mPopupMenu->isOut())
        {
            mPopupMenu->setOut(false);
            x1 = p1.x();
            y1 = p1.y();
            x2 = x1;
            y2 = p1.y() + mAdditionalWidget->height();
            start.setX(x1);
            start.setY(y1);
            stop.setX(x2);
            stop.setY(y2);

            QPropertyAnimation *animation = new QPropertyAnimation(mPopupMenu, "geometry");
            animation->setDuration(300);
            animation->setStartValue(stop);
            animation->setEndValue(start);
            animation->start();

            connect(animation, &QPropertyAnimation::finished, mPopupMenu, &LoginInfo::hide);
        }

    });

    connect(mAdditionalWidget, &AdditionalWidget::enterHead, this, [=](){
        QPoint p1 = mapToGlobal(mAdditionalWidget->pos());
        QRect start, stop;
        int x1, y1, x2, y2;
        start.setWidth(mLoginInfo->width());
        start.setHeight(mLoginInfo->height());
        stop.setWidth(mLoginInfo->width());
        stop.setHeight(mLoginInfo->height());
        if(mLoginInfo->isOut())
        {
            return;
        }
        else
        {

            mLoginInfo->setOut(true);
            mLoginInfo->setAnimationState(true);

            x1 = p1.x();
            y1 = p1.y();
            x2 = x1;
            y2 = p1.y() + mAdditionalWidget->height();
            mLoginInfo->move(x1, y1);
            mLoginInfo->show();
            start.setX(x1);
            start.setY(y1);
            stop.setX(x2);
            stop.setY(y2);

            QPropertyAnimation *animation = new QPropertyAnimation(mLoginInfo, "geometry");
            animation->setDuration(1000);
            animation->setStartValue(start);
            animation->setEndValue(stop);
            animation->start();

            connect(animation, &QPropertyAnimation::finished, this, [=](){
                mLoginInfo->setAnimationState(false);
                if(!mAdditionalWidget->headHasMouse())
                {
                    mLoginInfo->hide();
                    mLoginInfo->setOut(false);
                }
            });
        }

    });

    connect(mAdditionalWidget, &AdditionalWidget::leaveHead, this, [=](){
        QPoint p1 = mapToGlobal(mAdditionalWidget->pos());
        QRect start, stop;
        int x1, y1, x2, y2;
        start.setWidth(mLoginInfo->width());
        start.setHeight(mLoginInfo->height());
        stop.setWidth(mLoginInfo->width());
        stop.setHeight(mLoginInfo->height());
        if(mLoginInfo->isOut())
        {
            if(!mLoginInfo->isAnimationState())
            {
                x1 = p1.x();
                y1 = p1.y();
                x2 = x1;
                y2 = p1.y() + mAdditionalWidget->height();
                start.setX(x1);
                start.setY(y1);
                stop.setX(x2);
                stop.setY(y2);

                QPropertyAnimation *animation = new QPropertyAnimation(mLoginInfo, "geometry");
                animation->setDuration(500);
                animation->setStartValue(stop);
                animation->setEndValue(start);
                animation->start();

                connect(animation, &QPropertyAnimation::finished, this, [=](){
                    mLoginInfo->hide();
                    mLoginInfo->setOut(false);
                });
            }

        }
    });

}


void MainWindow::createDockWidget()
{
    mSidePanel = new SidePanel;
    mSidePanel->setFixedWidth(250);
    QDockWidget *pDockWidget = new QDockWidget(tr("相机"), this);
    pDockWidget->setObjectName("SidePanelDockWidget");
    pDockWidget->setAllowedAreas(Qt::RightDockWidgetArea | Qt::LeftDockWidgetArea);
    pDockWidget->setWidget(mSidePanel);
    this->addDockWidget(Qt::RightDockWidgetArea, pDockWidget);
    pDockWidget->setFeatures(QDockWidget::DockWidgetFloatable | QDockWidget::DockWidgetMovable);
}

void MainWindow::closeOtherWidget()
{
    mPopupMenu->close();
    mLogWidget->close();
    mLoginWidget->close();
    mProgressBar->close();
    mTableWidget->close();
}

QWidget* MainWindow::widgetFromIndex(int index)
{
    if(index >= 1 && index <= 18)
    {
        for(int i = 0; i < 18; i++)
        {
            QWidget* widget = mMainWidget->ui->mStackedWidget->widget(i);
            QString objectName = widget->objectName();
            if(objectName.compare("mCamera1") == 0 && index == 1)
            {
                return widget;
            }

            if(objectName.compare("mCamera2") == 0 && index == 2)
            {
                return widget;
            }

            if(objectName.compare("mCamera3") == 0 && index == 3)
            {
                return widget;
            }

            if(objectName.compare("mCamera4") == 0 && index == 4)
            {
                return widget;
            }

            if(objectName.compare("mCamera5") == 0 && index == 5)
            {
                return widget;
            }

            if(objectName.compare("mCamera6") == 0 && index == 6)
            {
                return widget;
            }

            if(objectName.compare("mCamera7") == 0 && index == 7)
            {
                return widget;
            }

            if(objectName.compare("mCamera8") == 0 && index == 8)
            {
                return widget;
            }

            if(objectName.compare("mCamera9") == 0 && index == 9)
            {
                return widget;
            }

            if(objectName.compare("mCamera10") == 0 && index == 10)
            {
                return widget;
            }

            if(objectName.compare("mCamera11") == 0 && index == 11)
            {
                return widget;
            }

            if(objectName.compare("mCamera12") == 0 && index == 12)
            {
                return widget;
            }

            if(objectName.compare("mCamera13") == 0 && index == 13)
            {
                return widget;
            }

            if(objectName.compare("mCamera14") == 0 && index == 14)
            {
                return widget;
            }

            if(objectName.compare("mCamera15") == 0 && index == 15)
            {
                return widget;
            }

            if(objectName.compare("mCamera16") == 0 && index == 16)
            {
                return widget;
            }

            if(objectName.compare("mCameraPanel") == 0 && index == 17)
            {
                return widget;
            }

            if(objectName.compare("mChartPanel") == 0 && index == 18)
            {
                return widget;
            }

        }

        return widgetFromIndex(17);
    }
    else
    {
        return widgetFromIndex(17);
    }
}

void MainWindow::clickedTreeWidget(const QString &txt)
{
    QString _txt = txt;
    if(_txt.compare("相机") == 0)
    {
        QWidget* widget = widgetFromIndex(17);
        mMainWidget->ui->mStackedWidget->setCurrentWidget(widget);
    }
    else if(_txt.compare("图表") == 0)
    {
        QWidget* widget = widgetFromIndex(18);
        mMainWidget->ui->mStackedWidget->setCurrentWidget(widget);
    }
    else if(_txt.compare("相机") != 0 && _txt.contains("相机"))
    {
        QString tmp = "相机";
        QString indexStr = _txt.remove(tmp, Qt::CaseSensitive);
        int indexInt = indexStr.toInt();
        QWidget* widget = widgetFromIndex(indexInt);
        mMainWidget->ui->mStackedWidget->setCurrentWidget(widget);
    }
}
