import asyncio
import datetime
import logging
import sys

from creation_helpers import get_args
from db_calls import db_get, url_get

date = f'{datetime.datetime.now().year}-{datetime.datetime.now().month}-{datetime.datetime.now().day}'
log_level = logging.INFO
logging.basicConfig(
    filename=f'logs/{date}.log',
    format='%(asctime)s - check-cards - %(levelname)s - %(message)s',
    level=log_level
)


async def main(args):
    arg_data = get_args(args)

    cardset_name = arg_data['cardset_name']
    print(f'Searching for cardset: {cardset_name}')
    c_query = await db_get('cardsets', params=[('name', cardset_name)])

    if c_query['count'] == 0:
        print(f'I do not see a cardset named {cardset_name}')
        return
    cardset = c_query['cardsets'][0]
    del c_query

    p_query = await db_get(
        'players',
        params=[('inc_dex', False), ('cardset_id', cardset['id']), ('short_output', True)]
    )
    if p_query['count'] == 0:
        raise ValueError(f'No players returned from Paper Dynasty API')
    all_players = p_query['players']
    del p_query

    errors = []
    successes = []
    cxn_error = False
    count = -1
    start_time = datetime.datetime.now()

    for x in all_players:
        if 'pitching' in x['image'] and 'skip_arms' in arg_data and arg_data['skip_arms'].lower() == 'true':
            pass
        elif 'batting' in x['image'] and 'skip_bats' in arg_data and arg_data['skip_bats'].lower() == 'true':
            pass
        elif 'start_id' in arg_data and int(arg_data['start_id']) > x['player_id']:
            pass
        elif 'sombaseball' in x['image']:
            errors.append((x, f'Bad card url: {x["image"]}'))
        else:
            count += 1
            if count % 20 == 0:
                print(f'Card #{count + 1} being pulled is {x["p_name"]}...')
            elif 'test_count' in arg_data and int(arg_data['test_count']) < count:
                print(f'Done test run')
                break

            if 'html_cards' in arg_data and arg_data['html_cards'].lower() == 'true':
                card_url = f'{x["image"]}&html=true'
                timeout = 2
            else:
                card_url = x['image']
                timeout = 6

            try:
                logging.info(f'calling the card url')
                resp = await url_get(card_url, timeout=timeout)

            except ConnectionError as e:
                if cxn_error:
                    raise e

                cxn_error = True
                errors.append((x, e))

            except ValueError as e:
                errors.append((x, e))

            else:
                if x['image2'] is not None:
                    if 'html_cards' in arg_data and arg_data['html_cards'].lower() == 'true':
                        card_url = f'{x["image2"]}&html=true'
                    else:
                        card_url = x['image2']

                    if 'sombaseball' in x['image2']:
                        errors.append((x, f'Bad card url: {x["image"]}'))
                    else:
                        try:
                            resp = await url_get(card_url, timeout=6)

                        except ConnectionError as e:
                            if cxn_error:
                                raise e

                            cxn_error = True
                            errors.append((x, e))

                        except ValueError as e:
                            errors.append((x, e))

                        else:
                            successes.append(x)
                else:
                    successes.append(x)

    if len(errors) > 0:
        logging.error(f'All Errors:')
        for x in errors:
            logging.error(f'ID {x[0]["player_id"]} {x[0]["p_name"]} - Error: {x[1]}')

    if len(successes) > 0:
        logging.debug(f'All Successes:')
        for x in successes:
            logging.info(f'ID {x["player_id"]} {x["p_name"]}')

    p_run_time = datetime.datetime.now() - start_time
    print(f'\nAll done!\nErrors: {len(errors)}\nSuccesses: {len(successes)}')
    print(f'Total runtime: {p_run_time.total_seconds()} seconds')


if __name__ == '__main__':
    asyncio.run(main(sys.argv[1:]))
