#include "ris_setting.h"
#include "ui_ris_setting.h"

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

    form=new ris_form(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);


    ResultIsValid_riss=false;
    ResultIsValid_ciss=false;
    ResultIsValid_rissciss=false;
    CanRis_Run=false;
    CanCis_Run=false;
    CanRisCis_Run=false;
    this->image=NULL;
    this->image_ref=NULL;
    this->rass=NULL;
    this->ciss=NULL;
    this->riscis=NULL;

    Time_R=0;
    Time_R_ref=0;
    Time_C=0;
    Time_C_ref=0;
    Time_RC=0;
    Time_RC_ref=0;

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

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


    connect(this->ui->Btn_ris,SIGNAL(clicked()),this,SLOT(ris_btn_clicked()));
    connect(this->ui->Btn_ris_start,SIGNAL(clicked()),this,SLOT(ris_btn_run_clicked()));
    connect(this->ui->Btn_ris_show,SIGNAL(clicked()),this,SLOT(ris_btn_show_clicked()));


    //connecty na CIS (source file je  cis_setting.cpp)
    connect(this->ui->Btn_cis,SIGNAL(clicked()),this,SLOT(cis_btn_clicked()));
    connect(this->ui->Btn_cis_start,SIGNAL(clicked()),this,SLOT(cis_btn_run_clicked()));
    connect(this->ui->Btn_cis_show,SIGNAL(clicked()),this,SLOT(cis_btn_show_clicked()));

    connect(this->ui->Btn_riscis,SIGNAL(clicked()),this,SLOT(riscis_btn_clicked()));
    connect(this->ui->Btn_riscis_start,SIGNAL(clicked()),this,SLOT(riscis_btn_run_clicked()));
    connect(this->ui->Btn_riscis_show,SIGNAL(clicked()),this,SLOT(riscis_btn_show_clicked()));

}

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

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


void ris_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 ris_setting::load_image(my_image *img){
    if(this->image!=NULL)
        delete image;
    image=new my_image(img);
    ui->BtnLoadImg->setEnabled(false);
    if(this->CanRis_Run)ui->Btn_ris_start->setEnabled(true);
    if(this->CanCis_Run)ui->Btn_cis_start->setEnabled(true);
    ui->BtnLoadImg->setPalette(this->paletteOK);
}


void ris_setting::btn_method_enabled(bool state){
    ui->Btn_cis->setEnabled(state);
    ui->Btn_ris->setEnabled(state);
    ui->Btn_riscis->setEnabled(state);

    if(!state)ui->Btn_cis_start->setEnabled(state);
    if(!state)ui->Btn_ris_start->setEnabled(state);
    if(!state)ui->Btn_riscis_start->setEnabled(state);


    if(!state)ui->Btn_ris_show->setEnabled(state);
    if(!state)ui->Btn_cis_show->setEnabled(state);
    if(!state)ui->Btn_riscis_show->setEnabled(state);

}


void ris_setting::delete_method(){
    if(this->rass!=NULL){
        delete rass;
        rass=NULL;
    }
    if(this->ciss!=NULL){
        delete ciss;
        ciss=NULL;
    }
    if(this->riscis!=NULL){
        delete riscis;
        riscis=NULL;
    }
}

/////sloty://////////////////////////////////////////////////////////////////////



void ris_setting::RefImageIsLoaded(){
    ui->BtnLoadRef->setEnabled(true);
    ui->BtnLoadRef->setPalette(this->paletteERR);
    ResultIsValid_riss=false;
    ResultIsValid_ciss=false;
    ResultIsValid_rissciss=false;
    CanRis_Run=false;
    CanCis_Run=false;
    CanRisCis_Run=false;
    this->delete_method();
    this->btn_method_enabled(false);
}



void ris_setting::ImageIsLoaded(){
    ui->BtnLoadImg->setEnabled(true);
    ui->BtnLoadImg->setPalette(this->paletteERR);
    this->btn_method_enabled(false);
    ResultIsValid_riss=false;
    ResultIsValid_ciss=false;
    ResultIsValid_rissciss=false;
}


void ris_setting::NeedLoadRefImage(){
    emit this->NeedLoadRef(3);
}

void ris_setting::NeedLoadImage(){
    emit this->NeedLoadImg(3);
}

void ris_setting::ris_btn_clicked(){
    if(this->rass==NULL && this->image_ref!=NULL){
        QTime time;
        my_image * pom=form->apply_filt(image_ref);
        time.start();
        rass=new ris(pom,form->Count_Ris_pocet,form->Count_Ris_delka);
        Time_R_ref = time.elapsed();
        delete pom;
        ui->label_time_ris->setText(QString::number(Time_R_ref));
        CanRis_Run=true;
        ui->Btn_ris->setEnabled(false);

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

void ris_setting::ris_btn_run_clicked(){
    if(CanRis_Run)
        {
            std::cout<<"RASS trida zacina hledat \n"<<std::endl;
            this->ui->Btn_ris_start->setEnabled(false);
            my_image *pom=form->apply_filt(image);

            QTime time;
            time.start();

            rass->find(pom);

            Time_R = time.elapsed();
            delete pom;
            ui->label_time_ris_find->setText(QString::number(Time_R));


            std::vector<BOD> ff(rass->body.begin(),rass->body.begin()+form->Count_Number+1);
            this->position_rass=ff;
            this->ui->Btn_ris_show->setEnabled(true);
            this->ResultIsValid_riss=true;
            std::cout<<"RASS trida hledala \n"<<std::endl;
        }
}

void ris_setting::ris_btn_show_clicked(){
    if(ResultIsValid_riss){
        show_point_on_image(this->image_ref,this->position_rass,image->Width());
    }
    std::cout<<"Zobrazil jsem \n"<<std::endl;

}

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

void ris_setting::form_closed(){
    delete_method();
    ui->BtnLoadRef->setEnabled(false);
    ui->BtnLoadRef->setPalette(this->paletteOK);
    this->btn_method_enabled(true);

}
