#include "select_file_dialog.h"
#include "ui_select_file_dialog.h"

Select_File_Dialog::Select_File_Dialog(QWidget *parent, camera::frame *Input_Frame1, camera::frame *Input_Frame2) :
    QDialog(parent),
    ui(new Ui::Select_File_Dialog),
    frame1(Input_Frame1),
    frame2(Input_Frame2)
{
    ui->setupUi(this);
    this->setWindowTitle("Select Filename");
}

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

void Select_File_Dialog::on_buttonBox_accepted()
{
    if (!filename.isEmpty()){
        cv::imwrite(filename.toStdString(),frame1->image);
        frame1 = nullptr;
        frame2 = nullptr;
    } else {
        QMessageBox::information(this, "Enter valid path",filename);
    }
}

void Select_File_Dialog::on_path_button_clicked()
{
    filename = QFileDialog::getSaveFileName(this, "Select a folder", QDir::homePath(),"All File (*.*) ;; PNG File (*.png) ;; JPEG File (*.jpg)");
    //QMessageBox::information(this, "..",file_name);
    if(filename.contains(".png", Qt::CaseInsensitive) || filename.contains(".jpg", Qt::CaseInsensitive)
       || filename.contains(".JPG" || filename.contains(".PNG", Qt::CaseInsensitive), Qt::CaseInsensitive)){
    }else{
        if(filename == ""){
            filename = QDir::homePath() +"/"+ QDateTime::currentDateTimeUtc().toString();
        }
        filename.append(".png");
    }
    ui->path_info->setText("Image will be save as: \n"+filename);
}

void Select_File_Dialog::setFrame(camera::frame *new_frame){
    frame1 = new_frame;
}

void Select_File_Dialog::setFrames(camera::frame *new_frame1, camera::frame *new_frame2){
    frame1 = new_frame1;
    frame2 = new_frame2;
}
