#include "ephotoattr.h"
#include "ui_ephotoattr.h"
#include "ephoto.h"
#include <LoUIClass/x_uimsgboxok.h>
ePhotoAttr::ePhotoAttr(const ePhoto::Data &data, QWidget *parent) :
    eAttr(parent),
    ui(new Ui::ePhotoAttr)
{
    ui->setupUi(this);

    ui->wFile         ->setText        (data.name);

    ui->wPlayDuration ->setValue       (data.playDuration);
    ui->wPlayTimes    ->setValue       (data.playTimes);
    ui->wEnterStyle   ->setCurrentIndex(data.enterStyle);
    ui->wEnterDuration->setValue       (data.enterDuration);

    connect(ui->wPlayDuration,  SIGNAL(valueChanged(int)),        this, SLOT(onAttrChanged()));
    connect(ui->wPlayTimes,     SIGNAL(valueChanged(int)),        this, SLOT(onAttrChanged()));
    connect(ui->wEnterStyle,    SIGNAL(currentIndexChanged(int)), this, SLOT(onAttrChanged()));
    connect(ui->wEnterDuration, SIGNAL(valueChanged(int)),        this, SLOT(onAttrChanged()));
    connect(ui->bnSelectFile,   SIGNAL(clicked(bool)),            this, SLOT(onSelectFile()));
    //我们的m2m文件结构中没有图片播放次数的参数，所以设置这两个为不可见，以免客户误解
    ui->label_7->setVisible(false);
    ui->wPlayTimes->setVisible(false);
    bool bFileExistFlag=true;
    QFileInfo file1(data.path+"/"+data.yuanshi_name);

        if(!file1.exists())
        {
            QFileInfo file2(data.computer_pic_file);
            if(!file2.exists())
            {
                    bFileExistFlag=false;
            }
            else {

                if(file2.isDir())
                {
                    bFileExistFlag=false;
                }
                else {
                    ui->wFile->setText(QFileInfo(data.computer_pic_file).fileName());
                    emit sResChanged(data.computer_pic_file);

                }
            }


        }
        else {
            if(file1.isDir())
            {
                QFileInfo file2(data.computer_pic_file);
                if(!file2.exists())
                {
                        bFileExistFlag=false;
                }
                else {

                    if(file2.isDir())
                    {
                        bFileExistFlag=false;
                    }
                    else {
                        ui->wFile->setText(QFileInfo(data.computer_pic_file).fileName());
                        emit sResChanged(data.computer_pic_file);

                    }
                }
            }
        }

    if(bFileExistFlag==false)
    {
        X_UIMsgBoxOk *dlg=new X_UIMsgBoxOk(tr("Attention"),tr("file(")+data.path+"/"+data.name+tr(") is not exist"),this,1);
        dlg->exec();
     }

}

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

void ePhotoAttr::onSelectFile()
{
    QString file = LoAppTools::getInstance()->selectFile(ePhoto::filters(), this);
    if(!file.isNull()) {
        ui->wFile->setText(QFileInfo(file).fileName());
        emit sResChanged(file);
    }
}

void ePhotoAttr::onAttrChanged()
{
    ePhoto::Data data;
    data.playDuration  = ui->wPlayDuration->value();
    data.playTimes     = ui->wPlayTimes->value();


    data.enterStyle    = ui->wEnterStyle->currentIndex();
    data.enterDuration = ui->wEnterDuration->value();

    if(data.enterDuration>data.playDuration)
    {
        X_UIMsgBoxOk *dlg=new X_UIMsgBoxOk(tr("Tip Info"),tr("Effect time cannot be longer than duration time"),this,0);
        dlg->exec();
        data.enterDuration=data.playDuration/2;
        ui->wEnterDuration->setValue(data.enterDuration);
    }
    emit sAttrChanged(data);


}
