#include "clOperation.h"
#include "ui_clOperation.h"

#include "CAppInfo.h"
#include "logproc.h"
#include <QTimer>

clOperation::clOperation(const int nRow, QWidget *parent) :
    QWidget(parent),
    ui(new Ui::clOperation),
    m_nRow(nRow)
{
    ui->setupUi(this);

    setStyleSheet(g_appInfoPtr->m_sQssStr);
}

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

void clOperation::setUI(const int nWidth, const int nHeight, CL_OPERATION_TYPE cot)
{
    setGeometry(0, 0, nWidth, nHeight);
    ui->btn1->setGeometry(0, (nHeight - g_appInfoPtr->m_fRate*24)/2,
                          g_appInfoPtr->m_fRate*64, g_appInfoPtr->m_fRate*24);
    ui->btn2->setGeometry(ui->btn1->x() + ui->btn1->width() + 10, ui->btn1->y(),
                          ui->btn1->width(), ui->btn1->height());
    ui->btn3->setGeometry(ui->btn2->x() + ui->btn2->width() + 10, ui->btn1->y(),
                          ui->btn1->width(), ui->btn1->height());
    ui->btn4->setGeometry(ui->btn1->x(), ui->btn1->y(), ui->btn1->width(), ui->btn1->height());
	if (cot == CL_OPERATION_TYPE::cot_online_practice_result)
	{
		ui->btn1->setGeometry(0, (nHeight - g_appInfoPtr->m_fRate * 24) / 2,
			g_appInfoPtr->m_fRate * 88, g_appInfoPtr->m_fRate * 24);
	}

	setOperationType(cot);    
}

void clOperation::setOperationType(CL_OPERATION_TYPE cot)
{
    ui->btn1->setVisible(true);
	ui->btn2->setVisible(true);
	ui->btn3->setVisible(true);
    ui->btn4->setVisible(false);
	m_operationType = cot;
    if (cot == CL_OPERATION_TYPE::cot_online_exam ||
            cot == CL_OPERATION_TYPE::cot_online_homework)
	{
		ui->btn3->setVisible(false);        
		ui->btn1->setText(QString::fromLocal8Bit("进入考试"));
		ui->btn2->setText(QString::fromLocal8Bit("客观分"));
		ui->btn2->installEventFilter(this);
	}
	else if (cot == CL_OPERATION_TYPE::cot_online_finish_exam)
	{
		ui->btn2->setVisible(false);
		ui->btn3->setVisible(false);
		ui->btn1->setText(QString::fromLocal8Bit("客观分"));
		ui->btn1->installEventFilter(this);
	}
	else if (cot == CL_OPERATION_TYPE::cot_online_practice)
	{
		ui->btn3->setVisible(false);
		ui->btn1->setText(QString::fromLocal8Bit("进入练习"));
		ui->btn2->setText(QString::fromLocal8Bit("查看详情"));
	}
	else if (cot == CL_OPERATION_TYPE::cot_offline_init_exam)
	{
		ui->btn2->setVisible(false);
		ui->btn3->setVisible(false);
		ui->btn1->setText(QString::fromLocal8Bit("抽取试卷"));
	}
	else if (cot == CL_OPERATION_TYPE::cot_offline_exam)
	{
		ui->btn1->setText(QString::fromLocal8Bit("查看试卷"));
		ui->btn2->setText(QString::fromLocal8Bit("下载试卷"));
		ui->btn3->setText(QString::fromLocal8Bit("上传答案"));
	}
	else if (cot == CL_OPERATION_TYPE::cot_online_practice_result)
	{		
		ui->btn2->setVisible(false);
		ui->btn3->setVisible(false);
		ui->btn1->setText(QString::fromLocal8Bit("查看成绩报告"));
	}
    else if(cot == CL_OPERATION_TYPE::cot_offline_status_null)
    {
        ui->btn1->setVisible(false);
        ui->btn2->setVisible(false);
        ui->btn3->setVisible(false);
        ui->btn4->setVisible(true);
        ui->btn4->setText(QString::fromLocal8Bit("未上传"));
        ui->btn4->setStyleSheet(QString(R"(QPushButton
                                        {
                                            outline:none;
                                            border:none;
                                            background:rgba(255,255,255,1);
                                            font-size:%1px;
                                            font-family:"Microsoft YaHei";
                                            font-weight:400;
                                            color:rgba(51, 51, 51,1);
                                        })").arg((int)(g_appInfoPtr->m_fRate*12)));
    }
    else if(cot == CL_OPERATION_TYPE::cot_offline_status_download)
    {
        ui->btn1->setVisible(false);
        ui->btn2->setVisible(false);
        ui->btn3->setVisible(false);
        ui->btn4->setVisible(true);
        ui->btn4->setText(QString::fromLocal8Bit("下载作答"));

        ui->btn4->setStyleSheet(QString(R"(QPushButton
                                        {
                                            outline:none;
                                            border:1px solid rgba(148,219,197,1);
                                            border-radius:%2px;
                                            background:rgba(255,255,255,1);
                                            font-size:%3px;
                                            font-family:"Microsoft YaHei";
                                            font-weight:500;
                                            color:rgba(19, 186, 140,1);
                                        }

                                        QPushButton:hover
                                        {
                                            outline:none;
                                            border:0px solid rgba(148,219,197,1);
                                            border-radius:%4px;
                                            background:rgba(19, 187, 138,1);
                                            font-size:%5px;
                                            font-family:"Microsoft YaHei";
                                            font-weight:500;
                                            color:rgba(255,255,255,1);
                                        })")//.arg((int)(g_appInfoPtr->m_fRate*1 < 1 ? 1 : g_appInfoPtr->m_fRate*1))
                                            .arg((int)(g_appInfoPtr->m_fRate*6))
                                            .arg((int)(g_appInfoPtr->m_fRate*12))
                                            .arg((int)(g_appInfoPtr->m_fRate*6))
                                            .arg((int)(g_appInfoPtr->m_fRate*12)));
    }
}

bool clOperation::eventFilter(QObject *obj, QEvent *ev)
{
    if(ev->type() == QEvent::Enter)
    {
        if(m_operationType == CL_OPERATION_TYPE::cot_online_exam ||
			m_operationType == CL_OPERATION_TYPE::cot_online_homework)
        {
            if(obj == ui->btn2 && ui->btn2->isEnabled())
            {
                emit showObjectiveScore(m_operationType, m_nRow);
            }
        }
        else if(m_operationType == CL_OPERATION_TYPE::cot_online_finish_exam)
        {
            if(obj == ui->btn1 && ui->btn1->isEnabled())
            {
                emit showObjectiveScore(m_operationType, m_nRow);
            }
        }
    }
    else if(ev->type() == QEvent::Leave)
    {
        if(m_operationType == CL_OPERATION_TYPE::cot_online_exam ||
			m_operationType == CL_OPERATION_TYPE::cot_online_homework)
        {
            if(obj == ui->btn2)
            {
                emit hideObjectiveScore(m_operationType, m_nRow);
            }
        }
        else if(m_operationType == CL_OPERATION_TYPE::cot_online_finish_exam)
        {
            if(obj == ui->btn1)
            {
                emit hideObjectiveScore(m_operationType, m_nRow);
            }
        }
    }

    return QWidget::eventFilter(obj, ev);
}

void clOperation::setBtn1Enable(bool bEnable)
{
//	if (m_operationType == CL_OPERATION_TYPE::cot_online_exam
//		|| m_operationType == CL_OPERATION_TYPE::cot_online_practice
//		|| m_operationType == CL_OPERATION_TYPE::cot_online_homework)
	{
		ui->btn1->setEnabled(bEnable);
	}
}

void clOperation::setBtn2Enable(bool bEnable)
{
    ui->btn2->setEnabled(bEnable);
}


void clOperation::on_btn1_clicked()
{
    if(m_operationType == CL_OPERATION_TYPE::cot_online_exam
            || m_operationType == CL_OPERATION_TYPE::cot_online_practice
            || m_operationType == CL_OPERATION_TYPE::cot_online_homework)
    {
        //进入考试
        ui->btn1->setEnabled(false);

        enterExam(m_operationType, m_nRow);

        QTimer::singleShot(3000, this, [&](){
            ui->btn1->setEnabled(true);
        });
    }
    else if(m_operationType == CL_OPERATION_TYPE::cot_online_practice_result)
    {
        emit viewPracticePaper(m_nRow);
    }
    else if(m_operationType == CL_OPERATION_TYPE::cot_offline_init_exam)
    {
        //抽取试卷
        emit offlineExamStart(m_nRow);
    }
    else if(m_operationType == CL_OPERATION_TYPE::cot_offline_exam)
    {
        //离线考试查看试卷
        emit viewOfflinePaper(m_nRow);
    }
}

void clOperation::on_btn2_clicked()
{
    if(m_operationType == CL_OPERATION_TYPE::cot_online_practice)
    {
        //查看详情
        emit viewPracticeDetail(m_nRow);
    }
    else if(m_operationType == CL_OPERATION_TYPE::cot_offline_exam)
    {
        //下载离线考试试卷
        emit downloadOfflinePaper(m_nRow);
    }
}

void clOperation::on_btn3_clicked()
{
    if(m_operationType == CL_OPERATION_TYPE::cot_offline_exam)
    {
        //上传离线考试答案
        emit uploadOfflineAnswer(m_nRow);
    }
}

void clOperation::on_btn4_clicked()
{
    if(m_operationType == CL_OPERATION_TYPE::cot_offline_status_download)
    {
        //离线考试查看试卷
        emit downloadOfflineAnswer(m_nRow);
    }
}
