#include "dialogeditinstructor.h"
#include "ui_dialogeditinstructor.h"
#include "hashtools.h"
#include <QPushButton>
#include <QRegExpValidator>
#include <QToolTip>

DialogEditInstructor::DialogEditInstructor(bool adminMode, QWidget *parent) :
    QDialog(parent),
    ui(new Ui::DialogEditInstructor),
    instructorInput(),
    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);
}

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

void DialogEditInstructor::setInstructor(Instructor instructor)
{
    instructorInput = instructor;

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

    ui->checkIsAdmin->setChecked(instructor.getIsAdmin());
    ui->checkArchived->setChecked(instructor.getArchived());
    ui->checkLoggedIn->setChecked(instructor.getLoggedIn());

    if(instructor.getIsAdmin())
        ui->editName->setEnabled(false);
    else
        ui->editName->setEnabled(true);

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

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

Instructor DialogEditInstructor::getInstructor()
{
    Instructor instructor = instructorInput;

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

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

    instructor.setIsAdmin(ui->checkIsAdmin->isChecked());
    instructor.setArchived(ui->checkArchived->isChecked());
    instructor.setLoggedIn(ui->checkLoggedIn->isChecked());

    return instructor;
}

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

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

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

void DialogEditInstructor::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 DialogEditInstructor::on_btnOK_clicked()
{
    this->accept();
}

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

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

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

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

    ui->editPassword->setFocus();
}

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