from datetime import datetime, timedelta
import os
import sys
import subprocess
import hashlib
import re
from io import BytesIO
from contextlib import closing

import yaml
import requests
from tqdm import tqdm

from .utils import ydump, mkdir_p, slugify


TRANSITFEEDS_API_URL = 'https://api.transitfeeds.com/v1/'


def get_transitfeeds_locations(location_ids):
    for location_id in location_ids:
        response = requests.get(TRANSITFEEDS_API_URL + 'getFeeds', params={
            'key': os.environ.get('TRANSITFEED_API_KEY'),
            'location': location_id,
            'descendants': 1,
            'page': 1,
            'limit': 50,
            'type': 'gtfs'
        })
        for feed in response.json()['results']['feeds']:
            yield feed['id']


def download_transitfeeds_feed(feed_id):
    try:
        return download_url(TRANSITFEEDS_API_URL + 'getLatestFeedVersion', params={
            'key': os.environ.get('TRANSITFEED_API_KEY'),
            'feed': feed_id
        })
    except ValueError:
        return None, None


def download_with_script(script, basepath):
    print('Starting script', script)
    proc = subprocess.Popen(script, shell=True, cwd=basepath,
                            stdout=subprocess.PIPE, stderr=subprocess.PIPE)
    outs, errs = proc.communicate()
    out_file = outs.decode('utf-8').splitlines()[-1]
    print('Saving', out_file)
    out_file = os.path.join(basepath, out_file)
    with open(out_file, 'rb') as f:
        return BytesIO(f.read()), None


def download_url(url, params=None):
    response_content = BytesIO()
    with closing(requests.get(url, params=params, stream=True)) as response:
        if response.headers.get('Content-Type') == 'application/json':
            raise ValueError('API error')
        size = None
        if response.headers.get('Content-Length'):
            size = int(response.headers.get('Content-Length').strip())
        with tqdm(total=size, unit='B', unit_scale=True) as pbar:
            for buf in response.iter_content(1024):
                response_content.write(buf)
                pbar.update(len(buf))
        url = response.url
    return response_content, url


def main(path):
    basepath = os.path.abspath(path)
    cityid = os.path.basename(basepath)
    filename = os.path.join(basepath, '{cityid}.md'.format(cityid=cityid))
    if not os.path.exists(filename):
        print('Filename does not exist', filename)
        return
    with open(filename) as f:
        for doc in yaml.load_all(f):
            data = doc
            break

    data_path = os.path.join(path, 'data')
    mkdir_p(data_path)

    now = datetime.utcnow()
    age = timedelta(days=7)

    new_files = False

    tf_location_ids = data.get('tf_location_ids')
    if tf_location_ids is not None:
        data.setdefault('gtfs', {})
        feed_ids = get_transitfeeds_locations(tf_location_ids)
        for feed_id in feed_ids:
            slug_feed_id = slugify(feed_id)
            data['gtfs'][slug_feed_id] = {'tf_feed_id': feed_id}

    if 'gtfs' not in data:
        print('No GTFS key', filename)
        return

    gtfs = data['gtfs']
    if not gtfs:
        print('No GTFS feeds present', filename)

    for key in gtfs.keys():
        print('Checking', key)
        info = gtfs[key]