import time
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.common.keys import Keys
import selsearch
import re
import pandas as pd 
import nlpIE
import pdf_saver as ps 
import datetime
import argparse
from legissouthafrica import legisSouthAfrica

## test for searching South Africa Parliament website for legal aid content. 
## class legisSouthAfrica was originally generated by Selenium IDE
## but I converted it to a Chromedriver for ease of use, and added lots of customization.

## ARGPARSE: args for this script. 
parser = argparse.ArgumentParser(description='Extract PDFs of legislation from South African Parliament website.')
parser.add_argument('webpage', metavar='webpage', type=int, 
                    help='webpage to process. 1=Constitution,2=Mandates,3=Acts,4=Other Bills')
parser.add_argument('driver',help='Path for Chromedriver')
parser.add_argument('-path',help='Path for PDF downloads')

args = parser.parse_args()
download_path = str(args.path)

## setting up options for Chromedriver, mostly to ensure any PDF links automatically download the PDFs to download_path
options = webdriver.ChromeOptions()
options.add_experimental_option('prefs', {
"download.default_directory": download_path, #Change default directory for downloads
"download.prompt_for_download": False, #To auto download the file
"download.directory_upgrade": True,
"plugins.always_open_pdf_externally": True #It will not show PDF directly in chrome
})

pd.options.display.max_colwidth = 1000
keywords = ['legal aid']
#keywords = ['legal aid',
#           'legal assistance',
#           'legal service',
#           'judicial assistance']
new_za = legisSouthAfrica(args.driver,download_path,options)
new_za.checkers() 

if args.webpage == 1:
    matches_constit = new_za.run_constitution(keywords)
    specs = 'SouthAfrica-constit'
    if len(matches_constit) > 0:
        print(matches_constit) 
        new_za.print_matches(matches_constit,specs)
        new_za.delete_no_matches(specs,path=new_za.downloadPath+'/constit')
    else:
        new_za.delete_unneeded_files('duplicates-nomatch-'+specs,[],files_path=new_za.downloadPath+'/constit')
    new_za.delete_unneeded_files('duplicates-'+specs,[])
    new_za.teardown()
elif args.webpage == 2: 
    matches_mandates = new_za.run_mandates(keywords)
    specs = 'SouthAfrica-mandates'
    if len(matches_mandates) > 0:
        print(matches_mandates) 
        new_za.print_matches(matches_mandates,specs)
        new_za.delete_no_matches(specs,path=new_za.downloadPath+'/mandates')
    else:
        new_za.delete_unneeded_files('duplicates-nomatch-'+specs,[],files_path=new_za.downloadPath+'/mandates')
    new_za.delete_unneeded_files('duplicates-'+specs,[])
    new_za.teardown()
elif args.webpage == 3: 
    matches_acts = new_za.run_acts(keywords)
    specs = 'SouthAfrica-acts'
    if len(matches_acts) > 0:
        print(matches_acts) 
        new_za.print_matches(matches_acts,specs)
        new_za.delete_no_matches(specs,path=new_za.downloadPath+'/acts')
    else:
        new_za.delete_unneeded_files('duplicates-nomatch-'+specs,[],files_path=new_za.downloadPath+'/acts')
    new_za.delete_unneeded_files('duplicates-'+specs,[])
    new_za.teardown()
elif args.webpage == 4: 
    matches_bills = new_za.run_bills(keywords)
    specs = 'SouthAfrica-bills'
    if len(matches_bills) > 0:
        print(matches_bills) 
        new_za.print_matches(matches_bills,specs)
        new_za.delete_no_matches(specs,path=new_za.downloadPath+'/bills')
    else:
        new_za.delete_unneeded_files('duplicates-nomatch-'+specs,[],files_path=new_za.downloadPath+'/bills')
    new_za.delete_unneeded_files('duplicates-'+specs,[])
    new_za.teardown()
else: 
    print("ERROR: webpage integer indicator not found. Try 1=Constitution,2=Mandates,3=Acts,4=Other Bills.")
    exit()

