import pandas as pd, time
from process_text import process_answers

def print_time(start):
    secs = time.time() - start
    s = int(secs%60)
    m = int(secs/60%60)
    h = int(secs/3600)
    return "{:0>2}H:{:0>2}M:{:0>2}S".format(h,m,s)

pd.set_option("display.precision",3,'display.float_format', '{:0.3f}'.format)
        
statusFile = rf'../logs/0_1_glz_answers.txt'

status = 'Beginning to read in files to process at ' + time.strftime("%m/%d/%Y %H:%M:%S", time.localtime()) + '.\n'
with open(statusFile,'w') as f_out:
    f_out.write(status)

answers = pd.read_pickle(rf'../data/answers.pkl',compression='zip')
status = 'Read answers file at '+time.strftime("%m/%d/%Y %H:%M:%S", time.localtime())+'.\n'
with open(statusFile,'a') as f_out:
    f_out.write(status)
print(status)

#Get subsample
#ids = answers.transcriptid.unique()[:5]
#answers = answers[answers.transcriptid.isin(ids)].copy()
status = '{} answers to parse.\n'.format(len(answers))
with open(statusFile,'a') as f_out:
    f_out.write(status)
print(status)

# Process text
## Process answers
print(answers.info())
start = time.time()
answers = answers.reset_index(drop=True)
answers['processed'] = answers['componenttext'].apply(process_answers)
status = 'Processed answers at '+time.strftime("%m/%d/%Y %H:%M:%S", time.localtime())+'. Took {} to process.\n'.format(print_time(start))
answers['processed'] = answers['processed'].apply(lambda x: {} if pd.isna(x) else x) 
print(answers.info())
a_processed = pd.json_normalize(answers['processed'])
with open(statusFile,'a') as f_out:
    f_out.write(status+'\nJSON normalized at '+time.strftime("%m/%d/%Y %H:%M:%S", time.localtime())+'.\n')
print('Parsed answers in {}'.format(print_time(start)))

start = time.time()
answers = answers.join(a_processed)
answers = answers.drop(['processed','componenttext'],axis=1)
status = 'Join processed answer data to answers at '+time.strftime("%m/%d/%Y %H:%M:%S", time.localtime())+'. Took {} to process.\n'.format(print_time(start))
with open(statusFile,'a') as f_out:
    f_out.write(status)
print('Join processed answer data to answers in {}'.format(print_time(start)))
print(answers.info())
answers.to_pickle(rf'../data/answers_parsed.pkl',compression='zip')

