import settings
import re

# get language count
def get_language_count(text, language_list, isupper):
    # Check if keyword in language
    keyword_count = 0
    keyword_count_list = []
    for keyword in language_list:
        k_count = 0
        if isupper:
            # add to counter occurnace of keyword
            k_count = text.count(keyword)
            keyword_count = keyword_count + k_count
        else:
            k_count = text.lower().count(keyword.lower())
            keyword_count = keyword_count + k_count
        keyword_count_list.append(k_count)

    return {'count_list': keyword_count_list}


# Get Language list count
def language_list_count(text, language_dict_list):
    language_count = {}

    for language in language_dict_list:
        isupper = False
        if 'upper' in language['language']:
            isupper = True
        count_dict = get_language_count(text, language['keyword'], isupper)
        converted_count_list = [str(element) for element in count_dict['count_list']]
        language_count[language['language']] = ",".join(converted_count_list)

    return language_count


def remove_stop_words(text):
    nltk_stopWords = ['i', 'me', 'my', 'myself', 'we', 'our', 'ours', 'ourselves', 'you', "you're", "you've", "you'll", "you'd", 'your', 'yours', 'yourself', 'yourselves', 'he', 'him', 'his', 'himself', 'she', "she's", 'her', 'hers', 'herself', 'it', "it's", 'its', 'itself', 'they', 'them', 'their', 'theirs', 'themselves', 'what', 'which', 'who', 'whom', 'this', 'that', "that'll", 'these', 'those', 'am', 'is', 'are', 'was', 'were', 'be', 'been', 'being', 'have', 'has', 'had', 'having', 'do', 'does', 'did', 'doing', 'a', 'an', 'the', 'and', 'but', 'if', 'or', 'because', 'as', 'until', 'while', 'of', 'at', 'by', 'for', 'with', 'about', 'against', 'between', 'into', 'through', 'during', 'before', 'after', 'above', 'below', 'to', 'from', 'up', 'down', 'in', 'out', 'on', 'off', 'over', 'under', 'again', 'further', 'then', 'once', 'here', 'there', 'when', 'where', 'why', 'how', 'all', 'any', 'both', 'each', 'few', 'more', 'most', 'other', 'some', 'such', 'no', 'nor', 'not', 'only', 'own', 'sme', 'so', 'than', 'too', 'very', 's', 't', 'can', 'will', 'just', 'don', "don't", 'should', "should've", 'now', 'd', 'll', 'm', 'o', 're', 've', 'y', 'ain', 'aren', "aren't", 'couldn', "couldn't", 'didn', "didn't", 'doesn', "doesn't", 'hadn', "hadn't", 'hasn', "hasn't", 'haven', "haven't", 'isn', "isn't", 'ma', 'mightn', "mightn't", 'mustn', "mustn't", 'needn', "needn't", 'shan', "shan't", 'shouldn', "shouldn't", 'wasn', "wasn't", 'weren', "weren't", 'won', "won't", 'wouldn', "wouldn't"]
    loughranMcDonald_stopWords = ['about', 'above', 'after', 'again', 'all', 'am', 'among', 'an', 'and', 'any', 'are', 'as', 'at', 'be', 'because', 'been', 'before', 'being', 'below', 'between', 'both', 'but', 'by', 'can', 'did', 'do', 'does', 'doing', 'down', 'during', 'each', 'few', 'for', 'from', 'further', 'had', 'has', 'have', 'having', 'he', 'her', 'here', 'hers', 'herself', 'him', 'himself', 'his', 'how', 'if', 'in', 'into', 'is', 'it', 'its', 'itself', 'just', 'me', 'more', 'most', 'my', 'myself', 'no', 'nor', 'not', 'now', 'of', 'off', 'on', 'once', 'only', 'or', 'other', 'our', 'ours', 'ourselves', 'out', 'over', 'own', 'same', 'she', 'should', 'so', 'some', 'such', 'than', 'that', 'the', 'their', 'theirs', 'them', 'themselves', 'then', 'there', 'these', 'they', 'this', 'those', 'through', 'to', 'too', 'under', 'until', 'up', 'very', 'was', 'we', 'were', 'what', 'when', 'where', 'which', 'while', 'who', 'whom', 'why', 'with', 'you', 'your', 'yours', 'yourself', 'yourselves']
    stop_words = nltk_stopWords + loughranMcDonald_stopWords
    stop_words = list(set(stop_words))
    query_words = text.split()
    resul_words = [word for word in query_words if word.lower() not in stop_words]
    result = ' '.join(resul_words)
    return result


def process_metrics(content):
    metrics = {}

    # Encoding page content with utf-8
    text = content.encode('utf-8').decode('utf-8')

    # Count the number of words
    word_count = len(re.findall(r'\w+', text))

    # Count the number of sentences
    sentence_count = len(re.findall(r'[.!?]+', text))
    if sentence_count == 0:
        sentence_count = 1

    # Calculate the Gunning Fog index
    words_per_sentence = word_count / sentence_count
    complex_words = len([word for word in re.findall(r'\w+', text) if
                         len(word) > 2 and len(re.findall(r'[aeiouy]+', word, re.IGNORECASE)) > 2])

    try:
        gunning_fog = 0.4 * (words_per_sentence + 100 * (complex_words / word_count))
    except:
        return metrics

    metrics = {
        "gunning_fog": gunning_fog,  # Get Gunning_fog Index
        "word_count": word_count,  # Get Word Count
        "sentence_count": sentence_count,  # Get Sentence Count
        # "avg_sentence_length": avg_sentence_length,  # get avg_sentence_length
    }

    # Get language list count
    language_dict_list = settings.language_dict_list
    text = remove_stop_words(text)

    # Cleaning text
    text_cleaned = re.sub('[^0-9a-zA-Z\s\-:()*]', ' ', text).replace('  ', ' ')
    language_counts = language_list_count(text_cleaned, language_dict_list)

    metrics.update(language_counts)

    return metrics

def main():
    metrics = process_metrics('')
    print(metrics)


if __name__ == "__main__":
    main()