#include "hausdorfm_setting.h"
#include "ui_hausdorfm_setting.h"



hausdorfm_setting::hausdorfm_setting(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::hausdorfm_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);


    CanHRun=false;
    ResultIsValid=false;
    image=NULL;
    image_ref=NULL;
    haus=NULL;
    this->form=new hausdorfm_form(this);
    Time=0;
    Time_ref=0;

    connect(ui->vysledek,SIGNAL(clicked()),this,SLOT(ShowResult()));
    connect(ui->pushButton_2,SIGNAL(clicked()),this,SLOT(ShowForm()));
    connect(ui->BtnLoadRef,SIGNAL(clicked()),this,SLOT(NeedLoadRefImage()));
    connect(ui->BtnLoadImg,SIGNAL(clicked()),this,SLOT(NeedLoadImage()));
}

hausdorfm_setting::~hausdorfm_setting()
{
    delete form;
    delete ui;
    if(image!=NULL)
        delete image;
    if(image_ref!=NULL)
        delete image_ref;
    if(haus!=NULL)
        delete haus;
}


void hausdorfm_setting::load_image(my_image *img){
    if(this->image!=NULL)
        delete this->image;
    this->image=new my_image(img);
    ui->BtnLoadImg->setEnabled(false);
    ui->BtnLoadImg->setPalette(this->paletteOK);
    if(CanHRun==true){
       // QMessageBox::information(this,tr("Vypocet"),tr("Zacninam s vypoctem HD\nOperace muze trvat dlouho, vyckej\nasi tu taky dodelam kontrolu velikosti"));
        std::cout<<"MHD -> aplikuji fltr\n"<<std::endl;
        my_image * pom=form->apply_filt(this->image);
        std::cout<<"MHD -> fltr ok  jdu na metodu find \n"<<std::endl;
        QTime time;
        time.start();

        haus->find(pom);
        Time = time.elapsed();
        delete pom;
        ui->time_find->setText(QString::number(Time));

        this->position.erase(position.begin(),position.end());
        if(this->form->Count_Number>=haus->body.size())
            this->position=haus->body;
        else{
            std::vector<BOD> ff(haus->body.begin(),haus->body.begin()+this->form->Count_Number+1);
            this->position=ff;
        }
        ResultIsValid=true;
        //QMessageBox::information(this,tr("Vypocet"),tr("Vypocet byl dokoncen"));
    }
}


void hausdorfm_setting::load_ref_image(my_image *img){
    if(this->image_ref!=NULL)
        delete this->image_ref;

    if(this->image!=NULL){
        delete this->image;
        this->image=NULL;
        ui->BtnLoadImg->setEnabled(true);
        ui->BtnLoadImg->setPalette(this->paletteERR);
    }

    this->image_ref=new my_image(img);
    ui->BtnLoadRef->setEnabled(false);
    ui->BtnLoadRef->setPalette(this->paletteOK);
    if(haus!=NULL)
        delete haus;
    std::cout<<"MHD -> aplikuji fltr\n";
    my_image * pom=form->apply_filt(this->image_ref);
    std::cout<<"MHD -> filtr ok, jdu na vypocet ref. snimku\n"<<std::endl;
    QTime time;
    time.start();
    haus=new hausdorfm(pom,this->form->registruj);
    Time_ref = time.elapsed();
    delete pom;
    ui->time->setText(QString::number(Time_ref));
    CanHRun=true;
}




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

void hausdorfm_setting::ShowForm(){
    if(this->image_ref==NULL)
        return;
    RefImageIsLoaded();
    form->load_image(image_ref);
    this->form->show();
}


void hausdorfm_setting::RefImageIsLoaded(){
    ui->BtnLoadRef->setEnabled(true);
    ui->BtnLoadRef->setPalette(this->paletteERR);
    CanHRun=false;
    ResultIsValid=false;
}

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

void hausdorfm_setting::NeedLoadRefImage(){
    emit NeedLoadRef(2);
}

void hausdorfm_setting::NeedLoadImage(){
    emit NeedLoadImg(2);
}



void hausdorfm_setting::ShowResult(){
    if(ResultIsValid){
        show_point_on_image(this->image_ref,this->position,this->image->Width());
    }
}
