'''
Created on 6 July 2019

@author: Val
'''
from datetime import datetime
from docutils.nodes import table
from fuzzywuzzy.string_processing import string

__all__ = ["pyrm_database", "rootsmagic_family", "rootsmagic_profile", "collate_temp", "return_date_from_event", "transform_edit_date"]

GEOLOCATION = {}
REFERENCE_ORDINAL = 693594


def collate_temp(string1, string2):
    '''
    This is a fake function used to solve the problem of the RMNOCASE issue inside the database
    '''
    if string1 == string2:
        return 0
    elif string1 > string2:
        return 1
    else:
        return -1
def return_date_from_event(event_data):
    '''
    This function will provide a given a date formated into RootsMagic from a given event
    '''
    if event_data.has_date():
        init_part = "D.+"
        date1_part = get_string_from_date(event_data.get_year(), event_data.get_month(), event_data.get_day())
        mid_part = "..+"
        end_part = "00000000.."
        if event_data.get_accuracy() == "BEFORE":
            init_part = "DB+"
        elif event_data.get_accuracy() == "AFTER":
            init_part = "DA+"
        elif event_data.get_accuracy() == "BETWEEN":
            init_part = "DR+"
            end_part = get_string_from_date(event_data.get_year_end(), event_data.get_month_end(), event_data.get_day_end()) +".."
        elif event_data.get_accuracy() == "ABOUT":
            mid_part = ".A+"
        return init_part + date1_part + mid_part + end_part
    #If no data in the event we skip and return None
    return None
def get_string_from_date(year, month, day):
    '''
    Returns the needed string in the RootsMagic format
    '''
    month_str = "00"
    if month and month < 10: month_str = "0" + str(month)
    elif month: month_str = str(month)
    day_str = "00"
    if day and day < 10: day_str = "0" + str(day)
    elif day: day_str = str(day)
    return str(year) + month_str + day_str
def get_geolocated_before(geo_string):
    '''
    This function will return the location data of an already geolocated string
    '''
    return GEOLOCATION.get(geo_string, None)
def set_geolocated(geo_string, location):
    '''
    It will link an specific string with the obtained geolocation
    '''
    GEOLOCATION[geo_string] = location
def transform_edit_date(date_info):
    '''
    This function will transform an input string from RootsMagic and return Back
    in datetime format
    '''
    return datetime.fromordinal(int(date_info) + REFERENCE_ORDINAL)
def edit_data_value():
    '''
    Returns the current edit date as string as understood by RM
    '''
    return str( (datetime.today()- datetime(1899,12,30) ).days + 0.01)