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

#include <funkce.h>
#include "lib.h"
#include <my_image.h>


#include "automat_run.h"


#include <QProgressDialog>
#include <QTextStream>

void MainWindow::auto_run(){

    if(ui->show_ref->image==NULL){
        QMessageBox::critical(this, tr("QMessageBox::critical()"),tr("Chyba, nebyl nacten referencni snimek."), QMessageBox::Abort);
     return;
    }//*/

    QString foldr=ui->show_ref->File_Path;
    QStringList split=foldr.split("/");
    foldr=split.last();
    foldr=foldr.replace(QRegExp("\\..*$"),"");
    QMessageBox::StandardButton reply=QMessageBox::No;
    if(QDir(foldr).exists()){
        reply = QMessageBox::information(this, tr("QMessageBox::information()"),
                                         "Chces pouzit adresar \""+foldr+"\" pro testovaci snimky?", QMessageBox::Yes | QMessageBox::No| QMessageBox::Cancel);
    }

    if(reply==QMessageBox::No){
        foldr= QFileDialog::getExistingDirectory(this, tr("Find Files"), QDir::currentPath());
    }
    if(reply==QMessageBox::Abort || reply==QMessageBox::Cancel){
        return;
    }


    QDir myDir=QDir(foldr);
    QStringList filters;
    filters << "*.JPG" << "*.png" << "*.jpg";
    //myDir.setNameFilters(filters);
    QStringList test_sada = myDir.entryList (filters);

    //////////////////////////////////////////
    //ZAPOS DO SOUBORU
    QFile file (foldr+".txt");
    if (! (file.open(QFile::WriteOnly | QFile::Truncate))){
        QMessageBox::critical(this, tr("QMessageBox::critical()"),"nelze otevrit soubor pro logovani vysledku, koncim");
        return;
    }
    QTextStream stream( &file );
    stream << foldr << "\n";

////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
    //PRIPRAVA FOTOGRAFII

    stream << "LOAD" << "\n";

    QProgressDialog progressDialog(this);
    progressDialog.setCancelButtonText(tr("&Cancel"));
    progressDialog.setRange(0,6);
    progressDialog.setWindowTitle(tr("Load ref"));
    QStringList foundFiles;


    progressDialog.setValue(0.1);
    progressDialog.setLabelText(tr("Ukladani ref. snimku"));
    progressDialog.show();
    qApp->processEvents();

    //načtení referenčních snímku:
    if(ui->automat->Korel){
        this->LoadingRefImage(4);
        this->ui->korl->korel_btn_clicked();
        stream << "Korelace"<<"\t"<< this->ui->korl->Time_ref << "\n";
        std::cout<<"korelace nactena\n";
    }

    progressDialog.setValue(2);
    qApp->processEvents();

    if(ui->automat->HD){
        this->LoadingRefImage(1);
        stream << "HD"<<"\t"<< this->ui->haus->Time_ref << "\n";
        std::cout<<"HD nactena\n";
    }

    progressDialog.setValue(3);
    qApp->processEvents();


    if(ui->automat->MHD){
        this->LoadingRefImage(2);
        stream << "MHD"<<"\t"<< this->ui->haus_m->Time_ref << "\n";
        std::cout<<"MHD nactena\n";
    }
    progressDialog.setValue(4);
    qApp->processEvents();


    if(ui->automat->CISS ||ui->automat->RISS ){
        this->LoadingRefImage(3);
        if(ui->automat->CISS){
            this->ui->ris_cis->cis_btn_clicked();
            stream << "CIS"<<"\t"<< this->ui->ris_cis->Time_C_ref << "\n";
            std::cout<<"CIS nactena\n";
        }
        progressDialog.setValue(5);
        qApp->processEvents();


        if(ui->automat->RISS){
            this->ui->ris_cis->ris_btn_clicked();
            stream << "RIS"<<"\t"<< this->ui->ris_cis->Time_R_ref<< "\n";
            std::cout<<"RIS nactena\n";
        }

        if(ui->automat->RISSCISS){
            this->ui->ris_cis->riscis_btn_clicked();
            stream << "DRISCIS"<<"\t"<< this->ui->ris_cis->Time_RC_ref<< "\n";
            std::cout<<"DRISCIS nactena\n";
        }
    }

    progressDialog.setValue(6);
    qApp->processEvents();


    //////////////////////////////////////////////////////////////////////////////////
    int widht_r=(ui->show_ref->image->Width()/2);//1/2 sirka ref. obrazu
    int height_r=(ui->show_ref->image->Height()/2);//1/2 vyska ref. obrazu
    progressDialog.setRange(0,test_sada.size());
    progressDialog.setWindowTitle(tr("Obr"));
    progressDialog.setValue(0.1);
    progressDialog.show();
    qApp->processEvents();

    stream<<"RUN"<<"\n";
    file.close();

    for(int i=0;i<test_sada.size();i++){
        file.open(QFile::WriteOnly | QFile::Append);
        progressDialog.setWindowTitle(tr("Obr")+test_sada.at(i));
        progressDialog.setValue(i);
        qApp->processEvents();

        stream<<test_sada.at(i)<<"\n";
        ui->show_img->Load_Image(foldr+"/"+test_sada.at(i));

//KOREL
        progressDialog.setLabelText(tr("Korelace"));
        qApp->processEvents();//prekresleni baru
        if(ui->automat->Korel){
            this->LoadingImage(4);
            this->ui->korl->korel_btn_run_clicked();
            stream<<"Korel"<<"\t"<<this->ui->korl->Time<<"\t"<<widht_r-this->ui->korl->position[0].x<<"\t"<<height_r - this->ui->korl->position[0].y<<"\n";
            std::cout<<"korelace vyhledala\n";
        }
        else
            stream<<"Korel"<<"\t"<<0<<"\t"<< 0<<"\t"<<0<<"\n";


//HD
        progressDialog.setLabelText(tr("HD"));
        qApp->processEvents();//prekresleni baru
        if(ui->automat->HD){
            this->LoadingImage(1);
            stream<<"HD"<<"\t"<<this->ui->haus->Time<<"\t"<< widht_r- this->ui->haus->position[0].x<<"\t"<<height_r - this->ui->haus->position[0].y<<"\n";
            std::cout<<"HD vyhledala\n";
        }
        else
           stream<<"HD"<<"\t"<<0<<"\t"<< 0<<"\t"<<0<<"\n";

//HHD
        progressDialog.setLabelText(tr("HHD"));
        qApp->processEvents();//prekresleni baru
        if(ui->automat->MHD){
            this->LoadingImage(2);
            stream<<"MHD"<<"\t"<<this->ui->haus_m->Time<<"\t"<< widht_r- this->ui->haus_m->position[0].x<<"\t"<<height_r - this->ui->haus_m->position[0].y<<"\n";
            std::cout<<"HHD vyhledala\n";
        }
        else
            stream<<"MHD"<<"\t"<<0<<"\t"<< 0<<"\t"<<0<<"\n";


//RIS CIS

        if(ui->automat->CISS ||ui->automat->RISS ){
            progressDialog.setLabelText(tr("CISS"));
            qApp->processEvents();//prekresleni baru
            this->LoadingImage(3);
            if(ui->automat->CISS){
                this->ui->ris_cis->cis_btn_run_clicked();
                stream<<"CIS"<<"\t"<<this->ui->ris_cis->Time_C<<"\t"<< widht_r- this->ui->ris_cis->position_ciss[0].x<<"\t"<<height_r - this->ui->ris_cis->position_ciss[0].y<<"\n";
                std::cout<<"CIS vyhledala\n";
            }
            else
               stream<<"CIS"<<"\t"<<0<<"\t"<< 0<<"\t"<<0<<"\n";

            progressDialog.setLabelText(tr("RASS"));
            qApp->processEvents();//prekresleni baru
            if(ui->automat->RISS){
                this->ui->ris_cis->ris_btn_run_clicked();
                stream<<"RIS"<<"\t"<<this->ui->ris_cis->Time_R<<"\t"<< widht_r- this->ui->ris_cis->position_rass[0].x<<"\t"<<height_r - this->ui->ris_cis->position_rass[0].y<<"\n";
                std::cout<<"RIS vyhledala\n";
            }
            else
               stream<<"RIS"<<"\t"<<0<<"\t"<< 0<<"\t"<<0<<"\n";

            progressDialog.setLabelText(tr("CIR RAS"));
            qApp->processEvents();//prekresleni baru
            if(ui->automat->RISSCISS){
                this->ui->ris_cis->riscis_btn_run_clicked();
                stream<<"DRICCIS"<<"\t"<<this->ui->ris_cis->Time_RC<<"\t"<< widht_r- this->ui->ris_cis->position_riscis[0].x<<"\t"<<height_r - this->ui->ris_cis->position_riscis[0].y<<"\n";
                std::cout<<"DRICCIS vyhledala\n";
            }
            else
               stream<<"DRICCIS"<<"\t"<<0<<"\t"<< 0<<"\t"<<0<<"\n";
        }

        file.close();
        if (progressDialog.wasCanceled())
                    break;
   }


}




