#include "addPic.h"
#include "ui_addPic.h"

#include "CAppInfo.h"

addPic::addPic(int nOrder, QWidget *parent) :
    CBaseWidget(parent),
    ui(new Ui::addPic), m_nOrder(nOrder)
{
    ui->setupUi(this);

    setStyleSheet(g_appInfoPtr->m_sQssStr);

    if(m_picList == nullptr)
    {
        m_picList = std::make_shared<wPictureList>(g_appInfoPtr->m_fRate*100, g_appInfoPtr->m_fRate*10, this);
        connect(m_picList.get(), &wPictureList::picChange, this, [&](){
            QString sFileUrls = m_picList->getFileUrls();
			m_sFileUrls = sFileUrls;
            QStringList fileList;
            if(!sFileUrls.isEmpty())
            {
                fileList = sFileUrls.split(",");
            }
            int nPicWidth = 0;
            if (fileList.count() > 5)
            {
                nPicWidth = g_appInfoPtr->m_fRate * (100+10) * 6;
                ui->btn_addPic->setVisible(false);
            }
            else
            {
                ui->btn_addPic->setVisible(true);
                nPicWidth = g_appInfoPtr->m_fRate * (100+10) * fileList.count();
            }
            int nHeight = m_picList->setUI(0, g_appInfoPtr->m_fRate * 10,
                nPicWidth);
            m_picList->show();

            ui->btn_addPic->setGeometry(m_picList->width(), g_appInfoPtr->m_fRate*10, g_appInfoPtr->m_fRate*100, g_appInfoPtr->m_fRate*100);
            emit answerChanged(fileList.count()>0);
        });
    }


}

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


int addPic::setUI(const int nWidth)
{
    ui->btn_addPic->setGeometry(0, g_appInfoPtr->m_fRate*10, g_appInfoPtr->m_fRate*100, g_appInfoPtr->m_fRate*100);
    ui->label_ap_tips->adjustSize();
    ui->label_ap_tips->setGeometry(ui->btn_addPic->x(), ui->btn_addPic->y() + ui->btn_addPic->height() + g_appInfoPtr->m_fRate*6,
                                   ui->label_ap_tips->width(), ui->label_ap_tips->height());
    setGeometry(0, 0, nWidth, ui->label_ap_tips->y() + ui->label_ap_tips->height() +  g_appInfoPtr->m_fRate*10);
    return height();
}

int addPic::widgetType()
{
    return wt_photoAnswer;
}

void addPic::on_btn_addPic_clicked()
{
    if(m_pWxAnswer == nullptr)
    {
        m_pWxAnswer = std::make_shared<wxAnswer>("MULTIPLE_PICTURES", m_nOrder, g_appInfoPtr->m_pAnsBgWidget);
        m_pWxAnswer->setUI(0);
        m_pWxAnswer->setPics(m_sFileUrls);
		connect(m_pWxAnswer.get(), &wxAnswer::exit, this, [&]() {
			m_pWxAnswer.reset();
		});
        
		connect(m_pWxAnswer.get(), &wxAnswer::picConfirm, this, [&](QString sFileUrls){
            m_sFileUrls = sFileUrls;
            m_pWxAnswer.reset();
            if(m_picList == nullptr)
            {
                m_picList = std::make_shared<wPictureList>(g_appInfoPtr->m_fRate*100, g_appInfoPtr->m_fRate*10, this);
            }

            m_picList->clearPic();
            QStringList fileList;
            if(!m_sFileUrls.isEmpty())
            {
                fileList = m_sFileUrls.split(",");
                for(QString sFile : fileList)
                {
                    m_picList->AddPic(sFile);
                }
            }
			int nPicWidth = 0;
			if (fileList.count() > 5)
			{
                nPicWidth = g_appInfoPtr->m_fRate * (100+10) * 6;
				ui->btn_addPic->setVisible(false);
			}
			else
			{
                nPicWidth = g_appInfoPtr->m_fRate * (100+10) * fileList.count();
			}
			int nHeight = m_picList->setUI(0, g_appInfoPtr->m_fRate * 10,
				nPicWidth);            
            m_picList->show();

            ui->btn_addPic->setGeometry(m_picList->width(), g_appInfoPtr->m_fRate*10, g_appInfoPtr->m_fRate*100, g_appInfoPtr->m_fRate*100);
            emit answerChanged(fileList.count()>0);
        });
    }
    m_pWxAnswer->show();
}

QString addPic::getFileUrl()
{
    return m_picList->getFileUrls();
}

void addPic::setAnswer(QString sAnswer)
{
    m_sFileUrls = sAnswer;

    m_picList->clearPic();
    QStringList fileList;
    if(!m_sFileUrls.isEmpty())
    {
        fileList = m_sFileUrls.split(",");
        for(QString sFile : fileList)
        {
            m_picList->AddPic(sFile);
        }
    }
    int nPicWidth = 0;
    if (fileList.count() > 5)
    {
        nPicWidth = g_appInfoPtr->m_fRate * (100+10)*6;
        ui->btn_addPic->setVisible(false);
    }
    else
    {
        nPicWidth = g_appInfoPtr->m_fRate * (100+10) * fileList.count();
    }
    int nHeight = m_picList->setUI(0, g_appInfoPtr->m_fRate * 10,
        nPicWidth);

    m_picList->show();

    ui->btn_addPic->setGeometry(m_picList->width(), g_appInfoPtr->m_fRate*10, g_appInfoPtr->m_fRate*100, g_appInfoPtr->m_fRate*100);
}
