#include "dialogedittrainee.h"
#include "ui_dialogedittrainee.h"
#include "hashtools.h"
#include <QPushButton>
#include <QRegExpValidator>
#include <QToolTip>

DialogEditTrainee::DialogEditTrainee(bool adminMode, QWidget *parent) :
    QDialog(parent),
    ui(new Ui::DialogEditTrainee),
    traineeInput(),
    flNeedHashPassword(false),
    adminMode(adminMode)
{
    ui->setupUi(this);
    verify();

    ui->editName->setProperty("mandatoryField", true);
    ui->editLogin->setProperty("mandatoryField", true);
    ui->editPassword->setProperty("mandatoryField", true);

    ui->btnViewPassword->setObjectName("btnViewPassword");
    ui->btnChangePassword->setObjectName("btnChangePassword");

    ui->editName->setValidator(new QRegExpValidator(QRegExp("[A-Za-zА-Яа-я0-9 _\\d]+"), this));
    ui->editLogin->setValidator(new QRegExpValidator(QRegExp("[A-Za-z\\d]+"), this));

    ui->editPassword->setEnabled(false);
//#ifndef PROJECT_TYPE_DEBUG
    ui->editPassword->setEchoMode(QLineEdit::EchoMode::Password);
//#endif

    ui->btnViewPassword->setEnabled(false);
    ui->btnChangePassword->setEnabled(false);

    ui->btnOK->setEnabled(false);
}

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

void DialogEditTrainee::setTrainee(Trainee trainee)
{
    traineeInput = trainee;

    ui->editName->setText(trainee.getName());
    ui->editLogin->setText(trainee.getLogin());
    ui->editPassword->setText(trainee.getPassword());

    ui->checkArchived->setChecked(trainee.getArchived());
    ui->checkLoggedIn->setChecked(trainee.getLoggedIn());

    if(trainee.getNeedSetPassword())
    {
        ui->editPassword->setEnabled(true);
        ui->btnViewPassword->setEnabled(true);
    }
    else
    {
        //if(adminMode)
            ui->btnChangePassword->setEnabled(true);
    }

    ui->btnOK->setEnabled(false);
}

Trainee DialogEditTrainee::getTrainee()
{
    Trainee trainee = traineeInput;

    trainee.setName(ui->editName->text());
    trainee.setLogin(ui->editLogin->text());

    if(flNeedHashPassword)
    {
        QString psw = ui->editPassword->text();
        psw = HashTools::hashingMD5string(psw);
        trainee.setPassword(psw);
    }
    else
        trainee.setPassword(ui->editPassword->text());

    trainee.setArchived(ui->checkArchived->isChecked());
    trainee.setLoggedIn(ui->checkLoggedIn->isChecked());

    return trainee;
}

void DialogEditTrainee::on_editName_textChanged(const QString &arg1)
{
    verify();
}

void DialogEditTrainee::on_editLogin_textChanged(const QString &arg1)
{
    verify();
}

void DialogEditTrainee::on_editPassword_textChanged(const QString &arg1)
{
    verify();
}

void DialogEditTrainee::verify()
{
    if(ui->editName->text().trimmed() == QStringLiteral("") ||
            ui->editLogin->text().trimmed() == QStringLiteral("") ||
            ui->editPassword->text().trimmed() == QStringLiteral(""))
        ui->btnOK->setEnabled(false);
    else
        ui->btnOK->setEnabled(true);
}

void DialogEditTrainee::on_btnOK_clicked()
{
    this->accept();
}

void DialogEditTrainee::on_btnViewPassword_pressed()
{
    ui->editPassword->setEchoMode(QLineEdit::EchoMode::Normal);
}

void DialogEditTrainee::on_btnViewPassword_released()
{
    ui->editPassword->setEchoMode(QLineEdit::EchoMode::Password);
}

void DialogEditTrainee::on_btnChangePassword_clicked()
{
    ui->editPassword->setEnabled(true);
    ui->btnViewPassword->setEnabled(true);

    ui->editPassword->setText("");
    flNeedHashPassword = true;

    ui->editPassword->setFocus();
}

void DialogEditTrainee::on_editLogin_inputRejected()
{
    QToolTip::showText(QCursor::pos(),tr("Only Latin letters and numbers"));
}
