import json
import requests


def download_rail_route(rr_file, verbose=False):
    """
    Downloads rail route data from railrouter.sg repo.
    Source: https://github.com/cheeaun/railrouter-sg

    params
    ------
    rr_file (str): filename in the data directory in the repo
    verbose (bool): print downloaded route and pattern info

    returns
    -------
    rr_data (dict): the core component from the raw data
    patterns (list): a list of patterns
    """

    url = 'https://raw.githubusercontent.com/' \
          'cheeaun/railrouter-sg/master/data/v2/'
    rr_data = json.loads(requests.get(url + rr_file).text)['routes'][0]
    patterns = rr_data['patterns']

    if verbose:
        print("[Downloaded]")
        print("Route:", rr_data['name'])
        print("Patterns:")
        print('\n'.join(
            [(str(i) + ': ' + p['name']) for i, p in enumerate(patterns)]))

    return rr_data, patterns