#include "datadialog.h"

DataDialog::DataDialog()
{
  wid->setGeometry(0,0,300,350);
  LabelDomain.setParent(wid);
  LabelLogin.setParent(wid);
  LabelPass.setParent(wid);
  LabelText.setParent(wid);
  LineEditDomain.setParent(wid);
  LineEditLogin.setParent(wid);
  LineEditPass.setParent(wid);
  TextEdit.setParent(wid);
  Button.setParent(wid);
  Status.setParent(wid);

  LabelDomain.setGeometry(10,5,50,16);
  LabelDomain.setText("Domain:");
  LineEditDomain.setGeometry(60,5,200,21);


  LabelLogin.setGeometry(10,30,50,16);
  LabelLogin.setText("Login:");
  LineEditLogin.setGeometry(60,30,200,21);

  LabelPass.setGeometry(10,55,30,16);
  LabelPass.setText("Pass:");
  LineEditPass.setGeometry(60,55,200,21);

  LabelText.setGeometry(10,80,300,16);
  LabelText.setText("Дополнительная информация:(max 300 символов)");
  TextEdit.setGeometry(10,100,250,200);

  Button.setGeometry(10,310,100,21);
  Status.setGeometry(120,310,100,16);

  connect(&Button,SIGNAL(clicked(bool)),this,SLOT(on_pushButton_clicked()));



}

void DataDialog::SetMod(const bool& _val){
  mod = _val;
  if(mod == mod_edit){
      LineEditDomain.setEnabled(false);
      Button.setText("Edit");
    }else{
      LineEditDomain.setEnabled(true);
      Button.setText("Add");
    }
  TextEdit.clear();
  Status.clear();
  LineEditDomain.clear();
  LineEditLogin.clear();
  LineEditPass.clear();
}
void DataDialog::SetId(const QString& _val){
  id = _val;
  QSqlQuery query;
  if(query.exec("SELECT `domain`,`login`,`pass`,`info` FROM `Data` WHERE `id` = "+_val)){
      query.next();
      QByteArray key,temp;
      TinyAES crypto;
      key = crypto.HexStringToByte(pass);

      LineEditDomain.setText(query.value(0).toString());
      LineEditLogin.setText(query.value(1).toString());

      temp = crypto.Decrypt(QByteArray::fromBase64(query.value(2).toByteArray()), key);
      LineEditPass.setText(temp);
      temp = crypto.Decrypt(QByteArray::fromBase64(query.value(3).toByteArray()), key);



      QTextCodec * codec = QTextCodec::codecForName("windows-1251");
      TextEdit.appendPlainText(codec->toUnicode(temp));


    }
}
void DataDialog::on_pushButton_clicked(){
  QSqlQuery query;
  QByteArray key,temp,temp2;
  TinyAES crypto;
  key = crypto.HexStringToByte(pass);


  if(LineEditDomain.text().isEmpty()){
       Status.setText("<font color = 'red'>Domain is Empty</font>");
       return;
    }
  if(LineEditLogin.text().isEmpty()){
       Status.setText("<font color = 'red'>Login is Empty</font>");
       return;
    }
  if(LineEditPass.text().isEmpty()){
       Status.setText("<font color = 'red'>Pass is Empty</font>");
       return;
    }


  temp = crypto.Encrypt(LineEditPass.text().toLocal8Bit(), key).toBase64();
  temp2 = crypto.Encrypt(TextEdit.toPlainText().toLocal8Bit(), key).toBase64();


  if(mod == mod_add){
      if(query.exec("INSERT INTO `Data`(`domain`,`login`,`pass`,`info`) VALUES ('"+LineEditDomain.text()+"','"+LineEditLogin.text()+"','"+temp+"','"+temp2+"')")){
          Status.setText("<font color = 'green'>Successful</font>");
        }else{
          Status.setText("<font color = 'red'>error</font>");
        }

    }else{
      if(query.exec("UPDATE `Data` SET `domain` = '"+LineEditDomain.text()+"',`login` = '"+LineEditLogin.text()+"' ,`pass` = '"+temp+"',`info` = '"+temp2+"' where `id` = "+id)){
          Status.setText("<font color = 'green'>Successful</font>");
        }else{
          Status.setText("<font color = 'red'>error</font>");
        }
    }
}
void DataDialog::SetPass(const QString& _val){
  pass = _val;
}
