import warnings

import pandas as pd
from tabulate import tabulate

from chaiverse.competition import get_competitions
from chaiverse import constants
from chaiverse.login_cli import auto_authenticate
from chaiverse.metrics.get_display_leaderboard import get_display_leaderboard
from chaiverse.metrics.get_leaderboard import get_leaderboard
from chaiverse.utils import print_color, cache

pd.set_option('display.max_columns', 50)
pd.set_option('display.max_rows', 500)
pd.set_option('display.width', 500)
pd.set_option("display.colheader_justify","center")

warnings.filterwarnings('ignore', 'Mean of empty slice')


@auto_authenticate
def display_leaderboard(
    developer_key=None,
    regenerate=False,
    detailed=False,
    max_workers=constants.DEFAULT_MAX_WORKERS,
):
    default_competition = {
        'id': 'Default',
        'type': 'default',
        'submission_start_date': None,
        'submission_end_date': None,
    }

    df = display_competition_leaderboard(
        competition=default_competition,
        detailed=detailed,
        regenerate=regenerate,
        developer_key=developer_key,
        max_workers=max_workers,
    )
    return df


def display_competition_leaderboard(
    competition=None,
    detailed=False,
    regenerate=False, 
    developer_key=None,
    max_workers=constants.DEFAULT_MAX_WORKERS
):
    competition = competition if competition else get_competitions()[-1]
    competition_type = competition.get('type') or 'submission_closed_feedback_round_robin'
    fetch_feedback = competition_type != 'default'

    submission_date_range = _get_submission_date_range(competition)
    feedback_date_range = _get_feedback_date_range(competition)
    submission_ids = competition.get('submissions')
    competition_id = competition.get('id')
    display_title = f'{competition_id} Leaderboard'

    df = cache(get_leaderboard, regenerate)(
        developer_key=developer_key,
        max_workers=max_workers,
        submission_date_range=submission_date_range,
        feedback_date_range=feedback_date_range,
        submission_ids=submission_ids,
        fetch_feedback=fetch_feedback
        )

    if len(df) > 0:
        display_df = df.copy()
        display_df = get_display_leaderboard(
            display_df, 
            detailed=detailed, 
            competition_type=competition_type
        )
        _pprint_leaderboard(display_df, display_title)
    else:
        print('No eligible submissions found!')
    return df


def _get_submission_date_range(competition):
    submission_date_range = (
        competition.get('submission_start_date'),
        competition.get('submission_end_date')
    )
    submission_date_range = None if submission_date_range == (None, None) else submission_date_range
    return submission_date_range


def _get_feedback_date_range(competition):
    feedback_date_range = (
        competition.get('start_time') or 0,
        competition.get('end_time') or float('inf')
    )
    return feedback_date_range


def _pprint_leaderboard(df, title):
    print_color(f'\n💎 {title}:', 'red')
    print(tabulate(df.round(3).head(30), headers=df.columns, numalign='decimal'))

