/* NAME: Kirill Vasil'evich Timofeev, 322
 * ASGN: N1
 */

#include "Controller.h"
#include "liblinear-1.8\linear.h"
#include <iostream>
#include <iomanip>
#include <cstring>
using namespace std;

Controller::Controller(View& vie, char * temp)
{
    view = &vie;
    view->controlActions(this, temp);
}

Controller::~Controller()
{
}

void Controller::training()
{
    msg1 = "Input the directory with training Images";
    msg2 = "Input the file with locations";
    msg3 = "Output the file with model";
    files(true ,true ,true, false);
    bool b = logic.getFilesTr();
    if (!b) view->showMessage(QString("Error"));
    view->showMessage(QString("Training Completed"));
}

void Controller::search(bool InImage)
{
    if (InImage)
    {
        msg2 = "Input the Image";
        msg3 = "Input the file with model";
        msg4 = "Output the file with locations";
        files(false ,true ,true ,true);
    }
    else
    {
        msg1 = "Input the directory with testing Images";
        msg2 = "Input the file with names of images";
        msg3 = "Input the file with model";
        msg4 = "Output the file with locations";
        files(true ,true, true, true);
    }
    logic.outFile = new QFile(logic.curOutFile);
    logic.outFile->open(QIODevice::ReadWrite | QIODevice::Truncate);
    logic.outFile->close();
    // NOW LOAD MODEL AND CLASSIFY
    if((logic.myModel=load_model(qPrintable(logic.curModelFile)))!=0)
    {
        bool b;
        if (InImage) b = logic.getFilesSImg();
        else b = logic.getFilesSDir();
        if (!b)
        {
            view->showMessage("Error");
            return;
        }
        if (InImage) view->showMessage("Searching in the Image Completed");
        else view->showMessage("Searching in the directory Completed");
    }
    else view->showMessage("can't load model from file");
    free_and_destroy_model(&logic.myModel);
}

void Controller::serIn()
{
    search(true);
}

void Controller::serDir()
{
    search(false);
}

void Controller::PRcount()
{
    msg2 = "Input the file with found locations";
    msg3 = "Input the file with right locations";
    files(false, true, true, false);
    char *temp = logic.PRcount();
    view->showMessage(QString(temp));
    view->showMessage(QString("Counting Recall and Precision Completed"));
}

void Controller::files(bool a , bool b , bool c , bool d)
{
    if (a) logic.curCat = view->message(msg1);
    if (b) logic.curInFile = view->message(msg2);
    if (c) logic.curModelFile = view->message(msg3);
    if (d) logic.curOutFile = view->message(msg4);

}

void Controller::exit()
{
    view->exit();
}
