import os
import matplotlib.pyplot as plt
import matplotlib as mpl
import numpy as np
import pandas as pd

from fds_api_pandas_functions import *
import loading

plt.style.use('ggplot')
mpl.rcParams['axes.color_cycle'] = ['#5DBA42', '#42BAB2', '#E24A33',
                                    '#777777', '#348ABD', '#FBC15E', '#E27533']
mpl.rcParams['font.size'] = 16
mpl.rcParams['axes.facecolor'] = 'white'

#%% build dict that translates from url to actual name of jurisdiction

dataframe = loading.load_messages()
    
#%% filters
    
has_attachment = dataframe['attachments'].str.len() > 0

nr_pdfs = dataframe['attachments'].str.len().sum()
    
    
    
    
    
    
    
plt.style.use('ggplot')
# for matplotlib versions below 1.5:
#mpl.rcParams['axes.color_cycle'] = ['#5DBA42', '#42BAB2', '#E24A33',
#                                    '#777777', '#348ABD', '#FBC15E', '#E27533']
mpl.rcParams['axes.prop_cycle'] = cycler('color',['#5DBA42', '#42BAB2', '#E24A33',
                                    '#777777', '#348ABD', '#FBC15E', '#E27533'])

mpl.rcParams['font.size'] = 28
mpl.rcParams['axes.facecolor'] = 'white'

files_path = './data/'
figures_path = './figures/'
#%% load data from files
df_requests = loading.load_requests()

df_messages = loading.load_messages()

#%% create filters for dataframe
is_unique = df_requests['same_as'].isnull()
is_complete = (df_requests['status']=='asleep') | \
               (~(df_requests['resolution']==''))



def compute_response_time(msg_group):
    first_msg = pd.to_datetime(msg_group.iloc[0]['timestamp'])
    resolved_list = ['resolved', 'refused', 'successful',
       'not_held', 'partially_successful', 'request_redirected',
       'user_withdrew_costs', 'user_withdrew']
    is_resolved = msg_group['status'].isin(resolved_list)
    resolved_msgs = msg_group[is_resolved]