#include "korel_setting.h"
#include "ui_korel_setting.h"

korel_setting::korel_setting(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::korel_setting)
{
    ui->setupUi(this);

    paletteERR = ui->BtnLoadRef->palette();
    paletteOK = ui->BtnLoadRef->palette();
    paletteERR.setColor(QPalette::Button,QColor("red"));
    //paletteOK.setColor(QPalette::Button,QColor("green"));
    ui->BtnLoadImg->setPalette(paletteERR);
    ui->BtnLoadRef->setPalette(paletteERR);

    Time=0;
    Time_ref=0;
    ResultIsValid=false;
    CanRun=false;
    this->image=NULL;
    this->image_ref=NULL;
    this->korel=NULL;

    form=new korel_form(this);

    //connecty
    connect(ui->Btn_nastaveni,SIGNAL(clicked()),this,SLOT(show_form()));
    connect(form,SIGNAL(Form_Close()),this,SLOT(form_closed()));

    connect(this->ui->BtnLoadRef,SIGNAL(clicked()),this,SLOT(NeedLoadRefImage()));
    connect(this->ui->BtnLoadImg,SIGNAL(clicked()),this,SLOT(NeedLoadImage()));


    connect(this->ui->Btn_korel,SIGNAL(clicked()),this,SLOT(korel_btn_clicked()));
    connect(this->ui->Btn_korel_start,SIGNAL(clicked()),this,SLOT(korel_btn_run_clicked()));
    connect(this->ui->Btn_korel_show,SIGNAL(clicked()),this,SLOT(korel_btn_show_clicked()));

}

korel_setting::~korel_setting()
{
    delete ui;
    if(image!=NULL)
        delete image;
    if(image_ref!=NULL)
        delete image_ref;
    this->delete_method();
}

void korel_setting::delete_method(){
    if(this->korel!=NULL){
        delete korel;
        korel=NULL;
    }
}

void korel_setting::changeEvent(QEvent *e)
{
    QWidget::changeEvent(e);
    switch (e->type()) {
    case QEvent::LanguageChange:
        ui->retranslateUi(this);
        break;
    default:
        break;
    }
}



void korel_setting::load_ref_image(my_image *img){
    if(this->image_ref!=NULL)
        delete image_ref;
    image_ref=new my_image(img);
    ui->BtnLoadRef->setEnabled(false);
    ui->BtnLoadRef->setPalette(this->paletteOK);
    this->btn_method_enabled(true);
}



void korel_setting::load_image(my_image *img){
    if(this->image!=NULL)
        delete image;
    image=new my_image(img);
    ui->BtnLoadImg->setEnabled(false);
    if(this->CanRun)ui->Btn_korel_start->setEnabled(true);
    ui->BtnLoadImg->setPalette(this->paletteOK);
}


void korel_setting::btn_method_enabled(bool state){
    ui->Btn_korel->setEnabled(state);
    if(!state)ui->Btn_korel_start->setEnabled(state);
    if(!state)ui->Btn_korel_show->setEnabled(state);
}







/////sloty:


void korel_setting::RefImageIsLoaded(){
    ui->BtnLoadRef->setEnabled(true);
    ui->BtnLoadRef->setPalette(this->paletteERR);
    ResultIsValid=false;
    CanRun=false;
    this->delete_method();
    this->btn_method_enabled(false);
}


void korel_setting::ImageIsLoaded(){
    ui->BtnLoadImg->setEnabled(true);
    ui->BtnLoadImg->setPalette(this->paletteERR);
    this->btn_method_enabled(false);
    ResultIsValid=false;
}


void korel_setting::NeedLoadRefImage(){
    emit this->NeedLoadRef(4);
}

void korel_setting::NeedLoadImage(){
    emit this->NeedLoadImg(4);
}

void korel_setting::korel_btn_clicked(){
    if(this->korel==NULL && this->image_ref!=NULL){
        my_image * pom=form->apply_filt(image_ref);
        QTime time;
        time.start();
        korel=new korelace(pom);
        this->Time_ref = time.elapsed();
        ui->label_time->setText(QString::number(this->Time_ref));
        CanRun=true;
        ui->Btn_korel->setEnabled(false);
        delete pom;

        if(this->image!=NULL)ui->Btn_korel_start->setEnabled(true);
        std::cout<<"Korelace-> trida vytvorena \n"<<std::endl;
    }
}

void korel_setting::korel_btn_run_clicked(){
    if(CanRun)
        {
            std::cout<<"Korelace ->  trida zacina hledat \n"<<std::endl;
            this->ui->Btn_korel_start->setEnabled(false);
            my_image * pom=form->apply_filt(image);
            QTime time;
            time.start();

            korel->find(pom);

            this->Time= time.elapsed();
            ui->label_time_find->setText(QString::number(this->Time));

            std::vector<BOD> ff;
            ff.push_back(korel->location);
            this->position=ff;
            this->ui->Btn_korel_show->setEnabled(true);
            this->ResultIsValid=true;
            delete pom;
            std::cout<<"Korelace -> trida vyhledala \n"<<std::endl;
        }
}

void korel_setting::korel_btn_show_clicked(){
    if(ResultIsValid){
        show_point_on_image(this->image_ref,this->position,this->image->Width());
    }
    std::cout<<"Zobrazil jsem \n"<<std::endl;

}

void korel_setting::show_form(){
    if(this->image_ref==NULL)
        return;
    form->load_img(this->image_ref);
    form->show();
}


void korel_setting::form_closed(){
    if(this->korel!=NULL){
        delete korel;
        korel=NULL;
    }

    ui->BtnLoadRef->setEnabled(false);
    ui->BtnLoadRef->setPalette(this->paletteOK);
    this->btn_method_enabled(true);
}
