#!/usr/bin/env python3

import yaml
import os
import datetime


YESTERDAY = (datetime.datetime.utcnow() - datetime.timedelta(days=1)).date()
TODAY = datetime.datetime.utcnow().date()
PATH_DATA_RAW = os.path.join('bld', 'out', 'data_raw')
PATH_TWITTER_CONFIG_MAIN = os.path.join(
    'src', 'data_collection', 'Twitter', 'twitter_config.yml')
PATH_PREPARATION = os.path.join('src', 'preparation')
PATH_TWITTER_CONFIG_MOCK = os.path.join(
    PATH_PREPARATION, 'preparation_config.yml')


def twitter_timeframe(indicator: str, start: str) -> list:
    date = datetime.datetime.strptime(start, '%Y-%m-%d').date()
    arr = []

    arr += weeks(date)

    return arr


def halves(year: int) -> list:
    arr = []

    if year == YESTERDAY.year:
        arr.append((
            str(datetime.date(year, 1, 1)), str(datetime.date(year, 6, 30)),
            '{}_1_partial'.format(year)
        ))
    else:
        arr.append((
            str(datetime.date(year, 1, 1)), str(datetime.date(year, 6, 30)),
            '{}_1'.format(year)
        ))
        arr.append((
            str(datetime.date(year, 6, 30)), str(datetime.date(year, 12, 31)),
            '{}_2'.format(year)
        ))

    return arr


def weeks(dt: datetime.date, until: datetime.date = TODAY) -> list:
    arr = []
    dt = dt - datetime.timedelta(days=dt.weekday())

    while dt < until:
        arr.append((
            str(dt), str(dt + datetime.timedelta(weeks=1)),
            '{}_{}'.format(dt.isocalendar()[0], dt.isocalendar()[1])
        ))
        dt += datetime.timedelta(weeks=1)
    arr.append((
        str(dt), str(TODAY),
        '{}_{}_partial'.format(dt.isocalendar()[0], dt.isocalendar()[1])
    ))

    return arr


def build(ctx):
    # # Tests
    ctx(
        features='run_py_script',
        source=os.path.join('Poloniex', 'test_poloniex_data.py'),
        deps=ctx.path_to(
            ctx, 'IN_DATA_COLLECTION', 'Poloniex', 'poloniex_data.py')
    )

    ctx.add_group()

    # Poloniex builder
    # Open main poloniex config file
    with open(os.path.join('src', 'data_collection', 'Poloniex',
                           'poloniex_config.yml'), 'r') as file:
        polo_config = yaml.load(file.read())

    # Loop over exchange key
    for key in polo_config:
        ctx(
            features='run_py_script',
            source=os.path.join('Poloniex', 'poloniex_data.py'),
            deps=[
                ctx.path_to(
                    ctx, 'IN_DATA_COLLECTION', 'Poloniex',
                    'poloniex_config.yml')
            ],
            target=[
                ctx.path_to(
                    ctx, 'OUT_DATA_RAW', '{}_trade_history.csv'.format(key)),
                ctx.path_to(
                    ctx, 'OUT_DATA_RAW', '{}_chart_data.csv'.format(key)),
                ctx.path_to(ctx, 'OUT_DATA_RAW', '{}.yml'.format(key)),
            ],
            append=key,
        )

    # Twitter builder
    # Open main Twitter config

    with open(PATH_TWITTER_CONFIG_MAIN, 'r') as file:
        twitter_config_main = yaml.load(file.read())

    # Check if data was downloaded before. If yes, load temporary config and
    # adjust ``since`` to ``until`` and check if ``until`` is today, then exit.
    # Twitter data is only downloaded till yesterday, because a more
    # fine-grained seems not possible up to now.
    for query in twitter_config_main:
        query_conf = twitter_config_main[query]

        try:
            with open(PATH_TWITTER_CONFIG_MOCK) as file:
                date_until = yaml.load(file.read()).get(query)
            date_until = datetime.datetime.strptime(
                date_until, '%Y-%m-%d').date()
            date_since = date_until + datetime.timedelta(days=1)
            query_conf['since'] = str(date_since)
        except (KeyError, FileNotFoundError):
            pass

        if query_conf['since'] == str(YESTERDAY):
            break

        for start, end, suffix in twitter_timeframe(query,
                                                    query_conf['since']):

            query_conf['since'] = start
            query_conf['until'] = end

            ctx(
                features='run_py_script',
                source=os.path.join(
                    'Twitter', 'GetOldTweets-python', 'Exporter.py'),
                deps=[
                    ctx.path_to(
                        ctx, 'IN_DATA_COLLECTION', 'Twitter',
                        'twitter_config.yml'),
                ],
                # target=[
                #     ctx.path_to(
                #         ctx, 'OUT_DATA_RAW', 'twitter_{}_{}.csv'.format(
                #             query, suffix))
                # ],
                append=(
                    ['--' + opt + ' ' + str(query_conf[opt])
                     for opt in query_conf] +
                    ['--filename {}'.format(
                        os.path.join(
                            'out', 'data_raw',
                            'twitter_' + query + '_' + suffix))
                     ]
                ),
            )
