#!/usr/bin/python3
# -*- coding: UTF-8 -*-

import sys
import os
import json

sys.path.insert(0, '/var/www/html/vol02/sctpls/ndpls/')
import ndplsLib

tests = {
  "IPNAMEMAP" : "HASREC" 
}


print('''

# TESTING
#  valid test IP
# GiveFile=True:   testUserAuthentication.py A 218.18.162.146
# GiveFile=False  testUserAuthentication.py A  999.18.162.
#    testUserAuthentication.py B terrillfrantz@outlook.com  terrill


# testing options
###   IP ADDRESS
#  TEST A: check for ip address in /var/chroot/home/content/24/3605924/www/sctplsData/ndpls/ippswd.txt

### EMAILACCESSPSWD CHECKS
#  /var/chroot/home/content/24/3605924/www/sctplsData/ndpls/emailAccessPswd.txt
#  TEST E: get email password recs
#  TEST F: check for email and password valididity validPswdInEmailAccessPswdFile
# TEST N: get live emailaccessPwd rec for email

### ONETIMEPASSCODES
# /var/chroot/home/content/24/3605924/www/sctplsData/ndpls/emailOnetimePasscode.txt
# TEST O: create and store a new passcode for an email
# TEST P: deactivate onetimepasscode for email
# TEST Q: validate onetimepasscode for email
#  TEST R: get email onetimepasscode recs
#  TEST S: get email onetimepasscode live rec
#  TEST T: destructive validate the  emailonetimePasscod rec for email

### MEMBER
#  B test email in member


###  OFFERS
#  C  test str/email in offer
#  TEST G: get offer records via email
#  TEST H: get offer record via email
#  TEST I: get offer activity records via email
#  TEST J: get offer status via email'
#    returns 0 remain
#      testUserAuthentication.py JZ malcolmdean@runbox.com
#    returns 2 avail
#      testUserAuthentication.py JZ sguastello@msn.com
#  TEST K: get offer activated signal via email
#    returns False
#      testUserAuthentication.py KZ malcolmdean@runbox.com
#    returns TRUE
#      testUserAuthentication.py KZ sguastello@msn.com

###   MODIFY EMAILACCESSPSWD FILE
#   TEST L :  add email and pswd to the emailAccessPswd file
#   TEST M: remove passwrd from emailaccesspswd file')

### MISC 
#  D email is pre-registered (member of offer)

###  FULL GIVE FILE TEST AS PER ASKFILE CODE
#   Z  give file test 
''')



testingOptions = sys.argv[1] if len(sys.argv) > 1 else ""

HUMANUSERID = sys.argv[2] if len(sys.argv) > 2 else None
HUMANPSWD   = sys.argv[3] if len(sys.argv) > 3 else None
isIpAddr = HUMANUSERID[0] in [str(i) for i in range(0,10)]
IPADDR = HUMANUSERID if isIpAddr else ""
DOCOBJID   = sys.arg[4] if len(sys.argv) > 4 else "290101ABSTRACT"
wantVolId = DOCOBJID[0:1]


giveFile = False

if "A" in testingOptions:
  print(f'TEST A: check for ip address in /var/chroot/home/content/24/3605924/www/sctplsData/ndpls/ippswd.txt')

  if isIpAddr:
    print(f'  input is IP ADDR')
    ipUserId, ipPswd = ndplsLib.getIpUserPswdFromTemplatesFile(IPADDR)
    if ipUserId != "":
      print(f'    IP ADDR FOUND, pswd is {ipPswd}')
    else:
      print(f'    IP ADDR not found')
  else:
    print(f'  input is NOT ip addr')

if "B" in testingOptions:
  print(f'TEST B: email in member /var/chroot/home/content/24/3605924/www/sctplsData/ndpls/membersAccess.txt')

  rec = ndplsLib.getMembersAccessRecByMembersEmailFromFile(HUMANUSERID)
  print(f'  IN MEMBER LIST:  {rec["recFound"]}')
  print('   ',json.dumps(rec, indent=2))

if "C" in testingOptions:
#  print(f'TEST C: email is in offer /var/chroot/home/content/24/3605924/www/sctplsData/ndpls/promocode_email.txt')
  print(f'TEST C: check email or string in offer file /var/chroot/home/content/24/3605924/www/sctplsData/ndpls/offersAccess.txt')

  print(f'  Working on: {HUMANUSERID}')
#  rec = ndplsLib.getOfferAccessRecByEmailAddr(HUMANUSERID)
  rec = ndplsLib.getOffersAccessRecByEmailFromFile(HUMANUSERID)
  print(f'  IN OFFER LIST:  {rec["recFound"]}')
  print('   ',json.dumps(rec, indent=2))


if "D" in testingOptions:
  print(f'TEST D: email is registered as member OR offer')

  userIdRegistrationTypes = ndplsLib.locateUserIdRegistrationTypes(HUMANUSERID)
  if userIdRegistrationTypes["MEMBER"]["found"] or userIdRegistrationTypes["OFFER"]["found"]:
    print(f'  EMAIL IS REGISTERED as member or offer')
  print('   ',json.dumps(userIdRegistrationTypes, indent=2))

if "E" in testingOptions:
  print(f'TEST E: get email password recs')
  emailAccessPswdRecs = ndplsLib.getEmailAccessPswdRecsByEmailFromFile(HUMANUSERID)
  print('   ',json.dumps(emailAccessPswdRecs, indent=2))
#  emailAccessPswdRec = ndplsLib.getEmailAccessPswdRecFromEmailAccessPswdRecs(email=HUMANUSERID, emailAccessPswdRecs=emailAccessPswdRecs)
#  if  emailAccessPswdRec != {}:
#    print(f'  Pswd rec FOUND:')
#    print('   ',json.dumps(emailAccessPswdRec, indent=2))
#  else:
#    print(f'  pswd rec NOT FOUND')


if "F" in testingOptions:
  print(f'TEST F: check for email and password valididity validPswdInEmailAccessPswdFile')
#  emailAccessPswdRecs = ndplsLib.getEmailAccessPswdRecsByEmailFromFile(HUMANUSERID)
#  print('   ',json.dumps(emailAccessPswdRecs, indent=2))
#  memberAccessGranted = False
#  if emailAccessPswdRecs != {}:
#    emailAccessPswdRec = ndplsLib.getEmailAccessPswdRecFromEmailAccessPswdRecs(email=HUMANUSERID, emailAccessPswdRecs=emailAccessPswdRecs)
#    memberAccessGranted = ndplsLib.getMembersAccessGranted(emailAccessPswdRec, HUMANPSWD)
  if ndplsLib.validPswdInEmailAccessPswdFile(HUMANUSERID, HUMANPSWD):
    print(f'  PASSWORD MATCHES')
  else:
    print(f'  PASSWORD NOT MATCHES')

if "G" in testingOptions:
  print(f'TEST G: get offer record via email')

  rec = ndplsLib.getOfferAccessRecByEmailAddr(HUMANUSERID)
  print('   ',json.dumps(rec, indent=2))
  if rec['recFound']:
    promocode = rec['promocodeStr']
    print(f'  OFFER REC FOUND promocode: {promocode}:')
    print('   ',json.dumps(rec, indent=2))
  else:
    print(f'  OFFER REC NOT FOUND')

#if "H" in testingOptions:
#  print(f'TEST H: get offer record via email')

if "I" in testingOptions:
  print(f'TEST I: get offer activity records via email')
  rec = ndplsLib.getOfferAccessRecByEmailAddr(HUMANUSERID)
  if rec['recFound']:
    promocode = rec['promocodeStr']
    print(f'  OFFER REC FOUND promocode: {promocode}:')
    print('   ',json.dumps(rec, indent=2))
    recs = ndplsLib.getOfferActivityRecsByPromocode(promocode)
    print(f'  ACTIVITY RECS:')
    print('   ',json.dumps(recs, indent=2))
  else:
    print(f'  OFFER REC NOT FOUND')

if "J" in testingOptions:
  print(f'TEST J: get offer status via email')
  numAvailable = 0
  rec = ndplsLib.getOfferAccessRecByEmailAddr(HUMANUSERID)
  if rec['recFound']:
    promocode = rec['promocodeStr']
    print(f'  OFFER REC FOUND promocode: {promocode}:')
    print('   ',json.dumps(rec, indent=2))
    recs = ndplsLib.getOfferActivityRecsByPromocode(promocode)
    print(f'  ACTIVITY RECS:')
    print('   ',json.dumps(recs, indent=2))
    for rec in recs:
      num = int(rec['sumStr'])
      numAvailable += num
    print(f'  NUM ARTICLES STILL AVAILABLE: {numAvailable}')
  else:
    print(f'  OFFER REC NOT FOUND')
    print(f'  NUM ARTICLES STILL AVAILABLE: {numAvailable}')

if "K" in testingOptions:
  print(f'TEST K: get offer activated signal via email')
  rec = ndplsLib.getOfferAccessRecByEmailAddr(HUMANUSERID)
  hasActivatedRec = False
  if rec['recFound']:
    promocode = rec['promocodeStr']
    print(f'  OFFER REC FOUND promocode: {promocode}:')
    print('   ',json.dumps(rec, indent=2))
    recs = ndplsLib.getOfferActivityRecsByPromocode(promocode)
    print(f'  ACTIVITY RECS:')
    print('   ',json.dumps(recs, indent=2))
    for rec in recs:
      num = int(rec['sumStr'])
      if num == 0:
        hasActivatedRec = True
        break

  if hasActivatedRec:
    print(f'  ACTIVATED SIGNAL FOUND')
  else:
    print(f'  ACTIVATED SIGNAL NOT FOUND')

if "L" in testingOptions:
  print(f'Test L: add email passwd to email access pswd')
  ndplsLib.addRecToEmailAccessPswd(email=HUMANUSERID, pswd=HUMANPSWD)
  print(f'email and pswd (ecrypted) added to /var/chroot/home/content/24/3605924/www/sctplsData/ndpls/emailAccessPswd.txt')

if "M" in testingOptions:
  print(f'TEST M: remove passwrd from emailaccesspswd file')
  ndplsLib.deactivateEmailAccessPswd(email=HUMANUSERID)
  print(f'email and BLANK pswd  added to /var/chroot/home/content/24/3605924/www/sctplsData/ndpls/emailAccessPswd.txt')

if "N" in testingOptions:
  print(f'TEST N: get live emailaccessPwd rec for email')
  emailAccessPswdRecs = ndplsLib.getEmailAccessPswdRecsByEmailFromFile(HUMANUSERID)
#  print('   ',json.dumps(emailAccessPswdRecs, indent=2))
  emailAccessPswdRec = ndplsLib.getEmailAccessPswdRecFromEmailAccessPswdRecs(email=HUMANUSERID, emailAccessPswdRecs=emailAccessPswdRecs)
  if  emailAccessPswdRec != {}:
    print(f'  Pswd rec FOUND:')
    print('   ',json.dumps(emailAccessPswdRec, indent=2))
  else:
    print(f'  pswd rec NOT FOUND')

if "O" in testingOptions:
  print(f'TEST O: create and store a new passcode for an email')
  print(f'New passcode created and stored: {ndplsLib.createAndStoreOnetimePasscodeForEmail(HUMANUSERID)}')

if "P" in testingOptions:
  print(f'TEST P: deactivate onetimepasscode for email')
  print(f'DEACTIVATED: {ndplsLib.deactivateOnetimePasscodeForEmail(HUMANUSERID)}')

if "Q" in testingOptions:
  print(f'TEST Q: validate onetimepasscode for email')
  print(f'Is valid passcode for email: {ndplsLib.validOnetimePasscodeForEmail(HUMANUSERID, HUMANPSWD)}')


if "R" in testingOptions:
  print(f'TEST R: get email onetimepasscode recs')
  emailOnetimePasscodeRecs = ndplsLib.getEmailOnetimePasscodeRecsByEmailFromFile(HUMANUSERID)
  print('   ',json.dumps(emailOnetimePasscodeRecs, indent=2))

if "S" in testingOptions:
  print(f'TEST S: get live emailonetimePasscod rec for email')
#  emailOnetimePasscodeRecs = ndplsLib.getEmailOnetimePasscodeRecsByEmailFromFile(HUMANUSERID)
#  print('   ',json.dumps(emailOnetimePasscodeRecs, indent=2))
  emailOnetimePasscodeRec = ndplsLib.getEmailOnetimePasscodeRecFromEmailOnetimePasscodeRec(email=HUMANUSERID)
  if  emailOnetimePasscodeRec != {}:
    print(f'  Pswd rec FOUND:')
    print('   ',json.dumps(emailOnetimePasscodeRec, indent=2))
  else:
    print(f'  pswd rec NOT FOUND')

if "T" in testingOptions:
  print(f'TEST T: idestructive validate the  emailonetimePasscod rec for email')
  print(f'Is valid passcode for email: {ndplsLib.destructiveValidateOnetimePasscodeForEmail(email=HUMANUSERID, passcode=HUMANPSWD)}')


if "Z" in testingOptions:
  print(f'TEST Z:')

#  print(f'Working on: {HUMANUSERID}')
#  userIdRegistrationTypes = ndplsLib.locateUserIdRegistrationTypes(HUMANUSERID)
#  print(f'IN MEMBER LIST:  {userIdRegistrationTypes["MEMBER"]["found"]}')
#  print(f'IN OFFER LIST:  {userIdRegistrationTypes["OFFER"]["found"]}')
  if isIpAddr:
    giveFile = ndplsLib.getGiveFileForIpAddr(IPADDR, wantVolId)
  else:
    giveFile = ndplsLib.getGiveFileForMember(HUMANUSERID, HUMANPSWD, wantVolId)
  print(f'  GIVEFILE: {giveFile}')


#  if userIdRegistrationTypes["OFFER"]["found"]:
#    pass



