// PassDlg.cpp : implementation file
//
// Written by John A deLaubenfels, https://www.adaptune.com/steg.htm .
// Permission granted to use freely in any non-fraudulent way.
//
//

#include "pch.h"
#include "Steg2.h"
#include "afxdialogex.h"
#include "PassDlg.h"
#include <vector>
#include <string>
#include "mt64.h"
#include "stufpic.h"

#pragma warning( disable : 4996 )

// PassDlg dialog

IMPLEMENT_DYNAMIC(PassDlg, CDialogEx)

PassDlg::PassDlg(CWnd* pParent /*=nullptr*/)
   : CDialogEx(IDD_PASSWORD, pParent)
{
    m_enterAction = VK_TAB;

}

PassDlg::~PassDlg()
{
}

void PassDlg::DoDataExchange(CDataExchange* pDX)
{
    CDialogEx::DoDataExchange(pDX);
    DDX_Control(pDX, IDC_EDT_PWD, m_cEdt_pwd);
    DDX_Control(pDX, IDC_STC_PWD_HINT, m_stcPwdHint);
    DDX_Control(pDX, IDC_STC_BADPWD, m_stcBadPwd);
}


BEGIN_MESSAGE_MAP(PassDlg, CDialogEx)
   ON_EN_KILLFOCUS(IDC_EDT_PWD, &PassDlg::OnKillfocusEdtPwd)
   ON_EN_SETFOCUS(IDC_EDT_PWD, &PassDlg::OnSetfocusEdtPwd)
END_MESSAGE_MAP()



BOOL PassDlg::OnInitDialog() {
   CDialogEx::OnInitDialog();

   m_stcBadPwd.SetWindowText(m_ssBadPwd.c_str());

   bool bSetFocus = false;
   if(bSetFocus) {
      m_cEdt_pwd.SetFocus();

      return FALSE;  // return TRUE unless you set the focus to a control
   } else {
      return TRUE;   // return TRUE unless you set the focus to a control
   } // !if(bSetFocus)
   // EXCEPTION: OCX Property Pages should return FALSE
}



// PassDlg message handlers


BOOL PassDlg::PreTranslateMessage(MSG* pMsg)
{
   if (pMsg->message == WM_KEYDOWN) {
      if (pMsg->wParam == VK_RETURN)
          pMsg->wParam = m_enterAction;
   }
   return CDialogEx::PreTranslateMessage(pMsg);
}


void PassDlg::OnKillfocusEdtPwd()
{
   m_enterAction = VK_RETURN;

   char cbuf[256];
   m_cEdt_pwd.GetWindowText(cbuf, 256);

// std::vector<unsigned long long> passwordSeedVector;
   MT64::bytesToVector((const unsigned char *)cbuf, (int)strlen(cbuf), m_passwordSeedVector);

   MT64 anMT, anM2;
   anMT.init_by_array64(&(m_passwordSeedVector[0]), m_passwordSeedVector.size());
   anM2.init_by_otherMT64(anMT);

   int maxLen = 36;             // everybody will pass
   std::string ssSilly;
   for(int i=0; i<256; i++) {
      ssSilly = makeSillyWord(anM2, 2);
      if(ssSilly.size() <= maxLen)
         break;
   } // for(int i=0; i<256; i++)


   sprintf(cbuf, "Hopefully the compiler will actually write these bytes.");
   m_cEdt_pwd.SetWindowText(cbuf);
   m_cEdt_pwd.SetWindowText("");

   if(m_pCommonInfo->bPassHint) {
      m_cEdt_pwd.SetWindowText(ssSilly.c_str());

      sprintf(cbuf, "Password sanity check:     %s", ssSilly.c_str());
      m_stcPwdHint.SetWindowText(cbuf);
   } else {
      PostMessage(WM_COMMAND, IDOK);
   } // !if(m_pCommonInfo->bPassHint)

}


void PassDlg::OnSetfocusEdtPwd()
{
   m_enterAction = VK_TAB;
   m_cEdt_pwd.SetWindowText("");
}


