#!/usr/bin/env python3

# Management of public transit tickets
# Modeling a ticket buying system

def redirection(vehicle,
                age,
                class_nb,
                nb_trip):
    """Main function managing the input and formatting the result
    Parameters
    ----------
    vehicle: Type of transport, either "bus", "train", or "subway" (str)

    age: Age of the passenger (int)

    class_nb: In the case of train transportation, you have to specify the class, either 1 or 2
        Defaults to 0 which means it's irrelevant (e.g. in a bus) (int)
    nb_trip: The number of trips you want to buy 
        Defaults to 1 (int)

    Returns
    -------
    The money you'll have to pay in [€] (float)
    """
    price = 0
    if vehicle == "train":
        price = train(age, class_nb, nb_trip)
    elif vehicle == "bus":
        price = bus(nb_trip)
    elif vehicle == "subway":
        price = subway(nb_trip)
    else:
        print("Enter either train, bus, or subway")
    return price


def main_buying(vehicle,
                age=0,
                class_nb=0,
                nb_trip=1):
    """Main function managing the input and formatting the result
    Parameters
    ----------
    vehicle: Type of transport, either "bus", "train", or "subway" (str)

    age: Age of the passenger (int, optional)
        Defaults to 0 meaning it's irrelevant to the prices

    class_nb: In the case of train transportation, you have to specify the class, either 1 or 2
        Defaults to 0 which means it's irrelevant (e.g. in a bus) (int, optional)

    nb_trip: The number of trips you want to buy 
        Defaults to 1 (int, optional)
    """
    # pass the parameters to the sub-function
    # return the result
    if nb_trip < 1:
        print("Number of trip entered not strictly positive")
    price = redirection(vehicle, age, class_nb, nb_trip)
    print("You bought %d %s ticket for %.2f €" % (nb_trip, vehicle, price))
    if class_nb == 1:
        print("You are in the 1st class!")
    elif class_nb == 2:
        print("You are in the 2nd class.")
    pass


def train(age,
          class_nb,
          nb_trip):
    """Sub-function managing train tickets
    Parameters
    ----------
    age: Age of the passenger (int)

    class_nb: In the case of train transportation, you have to specify the class, either 1 or 2
        Defaults to 0 which means it's irrelevant (e.g. in a bus) (int)

    nb_trip: The number of trips you want to buy (int) 

    Returns
    -------
    The price of a ticket in [€] (float)

    Notes
    -----
    Every price is taken from https://belgiantrain.be
    """
    price = 0
    if class_nb > 2 or class_nb < 0:
        print("The class number you entered doesn't exist.")
    elif class_nb == 0:
        print("You did not enter a class number")
    elif age < 0:
        print("Please enter a positive number for the age")
    elif age == 0:
        print("You did not enter the age")
    elif nb_trip == 1:
        if class_nb == 1:
            if age <= 12:
                price = 4.6
            else:
                price = 7.6
        else:
            if age <= 12:
                price = 3.5
            elif age <= 26:
                price = 6.6
            else:
                price = 5.9
    elif nb_trip == 2:
        if class_nb == 1:
            if age <= 12:
                price = 9.2
            elif age >= 65:
                price = 15.3
            else:
                price = 15.2
        else:
            if age <= 12:
                price = 7
            elif age <= 26:
                price = 13.2
            elif age >= 65:
                price = 7.2
            else:
                price = 11.8
    elif nb_trip == 10:
        if class_nb == 1:
            price = 128
        elif age <= 26:
            price = 53
        else:
            price = 83
    else:
        nb_trip_10 = nb_trip // 10
        nb_trip_2 = (nb_trip - nb_trip_10 * 10) // 2
        nb_trip_rest = nb_trip - nb_trip_10 * 10 - nb_trip_2 * 2
        price = nb_trip_10 * train(age, class_nb, 10) + \
            nb_trip_2 * train(age, class_nb, 2) + \
            nb_trip_rest * train(age, class_nb, 1)

    return price


def bus(nb_trip):
    """Sub-function managing bus tickets
    Parameters
    ----------
    nb_trip: The number of trips you want to buy (int)

    Returns
    -------
    The price of a ticket in [€] (float)

    Notes
    -----
    Every price is taken from https://letec.be
    """
    if nb_trip % 8 == nb_trip % 12:
        if nb_trip % 8 >= 0 and nb_trip % 8 <= 3:
            price = (nb_trip // 8) * 9.1 + (nb_trip % 8) * 2.5
        else:
            price = ((nb_trip // 8) + 1) * 9.1
    elif nb_trip % 8 < nb_trip % 12:
        if nb_trip % 8 == 1 or nb_trip % 8 == 0:
            price = (nb_trip // 8) * 9.1 + (nb_trip % 8) * 2.5
        else:
            price = ((nb_trip // 12) + 1) * 13.5
    elif nb_trip % 12 == 1 or nb_trip % 12 == 0:
        price = (nb_trip // 12) * 13.5 + (nb_trip % 12) * 2.5
    else:
        price = ((nb_trip // 8) + 1) * 9.1
    return price


def subway(nb_trip):
    """Sub-function managing subway tickets
    Parameters
    ----------
    nb_trip: The number of trips you want to buy (int)

    Returns
    -------
    The price of a ticket in [€] (float)

    Notes
    -----
    Every price is taken from https://www.stib-mivb.be
    """
    price = 0
    if nb_trip - nb_trip % 5 == nb_trip - nb_trip % 10:  # [X0;X5[
        if nb_trip % 5 >= 0 and nb_trip % 5 <= 3:
            price = (nb_trip // 10) * 14 + (nb_trip % 10) * 2.1
        else:
            price = (nb_trip // 10) * 10 + 8
    elif nb_trip % 5 < nb_trip % 10:  # [X5;X0[ left the if for readability
        if nb_trip % 5 >= 0 and nb_trip % 5 <= 2:
            price = (nb_trip // 10) * 14 + 8 + (nb_trip % 5) * 2.1
        else:
            price = ((nb_trip // 10) + 1) * 14

    return price
