#!/usr/bin/env python3
"""
Write your code in this file. Fill out the defined functions with your solutions.
You are free to write additional functions and modules as you see fit.
"""

import re

import analyze_functions
import date_time_functions

def analyze_text():
    """
    Analyzes text finding the number of spaces, letters and the paragraph with the most special characters.
    """

    text = None
    with open('value-of-time.txt', 'r') as f:
        text = f.read()

    run = True
    while run:
        choice = input('> ')

        if choice == 's' or choice == 'spaces':
            print(str(analyze_functions.count_spaces(text)))
        elif choice == 'l' or choice == 'letters':
            print(str(analyze_functions.count_letters(text)))
        elif choice == 'c' or choice == 'specials':
            print(str(analyze_functions.paragraph_max_specials(text)))
        elif choice == 'q' or choice == 'quit':
            run = False
        else:
            print('Not an option!')

    return True

def validate_mobile(number):
    """
    Validates a moblie phone number.
    """
    pattern = re.compile('^07[02369]-[0-9]{3} [0-9]{2} [0-9]{2}$')
    match = pattern.match(number)
    if match:
        return True
    return False

def verify_credit_card(number):
    """
    Verifies a creditcard number.
    """
    last_index = len(number) - 1

    if last_index != 13 and last_index != 15:
        return False

    control = int(number[last_index])

    result = 0
    counter = 0
    while counter < last_index:
        n = int(number[counter])
        if counter % 2 == 0:
            n *= 2
        if n > 9:
            n = n % 10 + int(n / 10)
        result += n
        counter += 1
    result = result * 9 % 10

    return result == control

def find_difference(items, items2):
    """
    Finds the difference between two lists.
    """
    result = list()

    for item in items:
        item = item.lower()
        if item in result:
            continue
        result.append(item)

    for item in items2:
        item = item.lower()
        if item in result:
            result.remove(item)
        else:
            result.append(item)

    result.sort()
    return result

def validate_date_time():
    """
    Finds valid dates and times following ISO-8601 in supplied text.
    """

    text = None
    with open('value-of-time.txt', 'r') as f:
        text = f.read()

    run = True
    while run:
        choice = input('> ')

        if choice == 'd' or choice == 'date':
            print(", ".join(date_time_functions.find_dates(text)))
        elif choice == 't' or choice == 'time':
            print(", ".join(date_time_functions.find_times(text)))
        elif choice == 'q' or choice == 'quit':
            run = False
        else:
            print('Not an option!')

    return True

if __name__ == '__main__':
    analyze_text()
    validate_mobile("")
    verify_credit_card("")
    find_difference([], [])
    validate_date_time()
