#include "robotcomunnication_gui.h"
#include "ui_robotcomunnication_gui.h"

RobotComunnication_gui::RobotComunnication_gui(ri::Staubli* interface) :
    ui(new Ui::RobotComunnication_gui), interface(*interface)
{
    ui->setupUi(this);
    ui->indicator->setEnabled(false);
    std::vector<uint8_t> address = getAddress();
    ui->IPv4_0->setText(QString::number(address.at(0)));
    ui->IPv4_1->setText(QString::number(address.at(1)));
    ui->IPv4_2->setText(QString::number(address.at(3)));
    ui->IPv4_3->setText(QString::number(address.at(2)));
    ui->Port->setText(QString::number(this->interface.getParameters().port));
    if(this->interface.isEstablished()){
        ui->indicator->setStyleSheet("background-color: green");
        ui->IPv4_0->setEnabled(false);
        ui->IPv4_1->setEnabled(false);
        ui->IPv4_2->setEnabled(false);
        ui->IPv4_3->setEnabled(false);
        ui->Port->setEnabled(false);
        ui->button_Disconnect->setEnabled(true);
        ui->button_Connect->setEnabled(false);
    }else{
        ui->indicator->setStyleSheet("background-color: red");
        ui->IPv4_0->setEnabled(true);
        ui->IPv4_1->setEnabled(true);
        ui->IPv4_2->setEnabled(true);
        ui->IPv4_3->setEnabled(true);
        ui->Port->setEnabled(true);
        ui->button_Disconnect->setEnabled(false);
        ui->button_Connect->setEnabled(true);
        this->setWindowTitle("Set Connection");
    }
}

RobotComunnication_gui::~RobotComunnication_gui()
{
    delete ui;
}

void RobotComunnication_gui::on_button_Connect_clicked()
{
    if(this->interface.isEstablished()) return;
    std::stringstream ss;
    tcpc::sRobot_interface updated = this->interface.getParameters();
    ss << ui->IPv4_0->text().toStdString() << "." << ui->IPv4_1->text().toStdString() << "." << ui->IPv4_3->text().toStdString() << "." << ui->IPv4_2->text().toStdString();
    updated.ipv4_Address = ss.str();
    updated.port = ui->Port->text().toInt();
    this->interface.setParameters(updated);
    try {
        this->interface.connect();
        if(this->interface.isEstablished()) ui->indicator->setStyleSheet("background-color: green");
        ui->IPv4_0->setEnabled(false);
        ui->IPv4_1->setEnabled(false);
        ui->IPv4_2->setEnabled(false);
        ui->IPv4_3->setEnabled(false);
        ui->Port->setEnabled(false);
        ui->button_Disconnect->setEnabled(true);
        ui->button_Connect->setEnabled(false);
        QMessageBox text;
        text.setWindowTitle("WARNING");
        text.setText("Do NOT forget to set Default position");
        text.exec();
    } catch (...) {
        QMessageBox text;
        text.setWindowTitle("ERROR");
        text.setText("Not possible to connect to this address");
        text.exec();
    }
}

void RobotComunnication_gui::on_button_Disconnect_clicked()
{
    this->interface.disconnect();
    if(this->interface.isEstablished()) ui->indicator->setStyleSheet("background-color: red");
    ui->IPv4_0->setEnabled(true);
    ui->IPv4_1->setEnabled(true);
    ui->IPv4_2->setEnabled(true);
    ui->IPv4_3->setEnabled(true);
    ui->Port->setEnabled(true);
    ui->button_Disconnect->setEnabled(false);
    ui->button_Connect->setEnabled(true);
}

std::vector<uint8_t> RobotComunnication_gui::getAddress()
{
    std::vector<uint8_t> result;
    std::string tmp;
    std::stringstream address(this->interface.getParameters().ipv4_Address);
    std::getline(address, tmp, '.');
    result.push_back(std::atoi(tmp.c_str()));
    std::getline(address, tmp, '.');
    result.push_back(std::atoi(tmp.c_str()));
    std::getline(address, tmp, '.');
    result.push_back(std::atoi(tmp.c_str()));
    std::getline(address, tmp, '.');
    result.push_back(std::atoi(tmp.c_str()));
    return result;
}
