#include "qgeneraterandpass.h"

QGenerateRandPass::QGenerateRandPass()
{
  UseNumber = 1;
  UseChar = 1;
  UseSpecialChar = 0;
  UseUpperChar = 1;
  PassLength = 6;

  SpecialCharList = "!@#$%^&*()_+=-<>?.,;:{}";
  QTime midnight(0,0,0);
  qsrand(midnight.secsTo(QTime::currentTime()));
}
QGenerateRandPass::QGenerateRandPass(bool _UseInt,bool _UseChar,bool _UseSpecialChar,bool _UseUpperChar,int _PassLength)
{
  UseNumber = _UseInt;
  UseChar = _UseChar;
  UseSpecialChar = _UseSpecialChar;
  UseUpperChar = _UseUpperChar;
  PassLength = _PassLength;

  SpecialCharList = "!@#$%^&*()_+=-<>?.,;:{}";
  QTime midnight(0,0,0);
  qsrand(midnight.secsTo(QTime::currentTime()));
}
int QGenerateRandPass::NumberRand(int _max, int _min)
{ return _min + (int)floor((double(qrand())/RAND_MAX)*(_max - _min )); }
void QGenerateRandPass::SetUseNumber(bool val){
  UseNumber = val;
}
void QGenerateRandPass::SetUseChar(bool val){
  UseChar = val;
}
void QGenerateRandPass::SetUseSpecialChar(bool val){
  UseSpecialChar = val;
}
void QGenerateRandPass::SetUseUpperChar(bool val){
  UseUpperChar = val;
}
void QGenerateRandPass::SetPassLength(int val){
  PassLength = val;
}
char QGenerateRandPass::GenNumber(){
  return char(NumberRand(48,57));
}
char QGenerateRandPass::GenChar(){
  return char(NumberRand(97,122));
}
char QGenerateRandPass::GenUpperChar(){
  return char(NumberRand(65,90));
}
char QGenerateRandPass::GenSpecialChar(){
  return SpecialCharList.at(NumberRand(0,SpecialCharList.length()));
}
QString QGenerateRandPass::GeneratePass(){

  QString result;

  int Count = 0;
  int RandInt = 0;

  char (QGenerateRandPass::*pfn_MyFuncType[4])(void);

  if(UseNumber){
      pfn_MyFuncType[Count] = &QGenerateRandPass::GenNumber;
      Count++;
    }
  if(UseChar){
      pfn_MyFuncType[Count] = &QGenerateRandPass::GenChar;
      Count++;
    }
  if(UseUpperChar){
      pfn_MyFuncType[Count] = &QGenerateRandPass::GenUpperChar;
      Count++;
    }
  if(UseSpecialChar){
      pfn_MyFuncType[Count] = &QGenerateRandPass::GenSpecialChar;
      Count++;
    }

  for(int i = 0;i<PassLength;i++){
      RandInt = NumberRand(0,Count);
      result += (this->*pfn_MyFuncType[RandInt])( );
    }
  return result;
}
