from creation_helpers import mround


def total_chances(chance_data):
    sum_chances = 0
    for key in chance_data:
        if key not in ['id', 'player_id', 'cardset_id', 'vs_hand', 'is_prep']:
            sum_chances += chance_data[key]

    return sum_chances


def soft_rate(pct):
    if pct > .2:
        return 'high'
    elif pct < .1:
        return 'low'
    else:
        return 'avg'


def med_rate(pct):
    if pct > .65:
        return 'high'
    elif pct < .4:
        return 'low'
    else:
        return 'avg'


def hard_rate(pct):
    if pct > .4:
        return 'high'
    elif pct < .2:
        return 'low'
    else:
        return 'avg'


def hr_per_fb_rate(pct):
    if pct > .18:
        return 'high'
    elif pct < .08:
        return 'low'
    else:
        return 'avg'


def all_singles(row, hits_vl, hits_vr):
    tot_singles_vl = hits_vl * ((int(row[7]) - int(row[8]) - int(row[9]) - int(row[12]))
                                / int(row[7]))
    tot_singles_vr = hits_vr * ((int(row[40]) - int(row[41]) - int(row[42]) - int(row[45]))
                                / int(row[40]))

    return mround(tot_singles_vl), mround(tot_singles_vr)


def bp_singles(singles_vl, singles_vr):
    bpsi_vl = 5 if singles_vl >= 5 else 0
    bpsi_vr = 5 if singles_vr >= 5 else 0

    return mround(bpsi_vl), mround(bpsi_vr)


def wh_singles(rem_si_vl, rem_si_vr, hard_rate_vl, hard_rate_vr):
    if hard_rate_vl == 'low':
        whs_vl = 0
    else:
        whs_vl = rem_si_vl / 2

    if hard_rate_vr == 'low':
        whs_vr = 0
    else:
        whs_vr = rem_si_vr / 2

    return mround(whs_vl), mround(whs_vr)


def one_singles(rem_si_vl, rem_si_vr, soft_rate_vl, soft_rate_vr):
    if soft_rate_vl == 'high':
        oss_vl = rem_si_vl
    else:
        oss_vl = 0

    if soft_rate_vr == 'high':
        oss_vr = rem_si_vr
    else:
        oss_vr = 0

    return mround(oss_vl), mround(oss_vr)


def bp_homerun(hr_vl, hr_vr, hr_rate_vl, hr_rate_vr):
    if hr_rate_vl == 'low':
        bphr_vl = hr_vl
    elif hr_rate_vl == 'avg':
        bphr_vl = hr_vl * .75
    else:
        bphr_vl = hr_vl * .4

    if hr_rate_vr == 'low':
        bphr_vr = hr_vr
    elif hr_rate_vr == 'avg':
        bphr_vr = hr_vr * .75
    else:
        bphr_vr = hr_vr * .4

    return mround(bphr_vl), mround(bphr_vr)


def triples(all_xbh_vl, all_xbh_vr, triple_rate_vl, triple_rate_vr):
    tr_vl = all_xbh_vl * triple_rate_vl if all_xbh_vl > 0 else 0
    tr_vr = all_xbh_vr * triple_rate_vr if all_xbh_vr > 0 else 0

    return mround(tr_vl), mround(tr_vr)


def two_doubles(all_doubles_vl, all_doubles_vr, soft_rate_vl, soft_rate_vr):
    two_doubles_vl = all_doubles_vl if soft_rate_vl == 'high' else 0
    two_doubles_vr = all_doubles_vr if soft_rate_vr == 'high' else 0

    return mround(two_doubles_vl), mround(two_doubles_vr)


def hbp_rate(hbp, bb):
    if hbp == 0:
        return 0
    elif bb == 0:
        return 1
    else:
        return hbp / bb


def hbps(all_ob, this_hbp_rate):
    if all_ob == 0 or this_hbp_rate == 0:
        return 0
    else:
        return mround(all_ob * this_hbp_rate)


def xchecks(pos, all_chances=True):
    if pos.lower() == 'p':
        return 1 if all_chances else 0
    elif pos.lower() == 'c':
        return 3 if all_chances else 2
    elif pos.lower() == '1b':
        return 2 if all_chances else 1
    elif pos.lower() == '2b':
        return 6 if all_chances else 5
    elif pos.lower() == '3b':
        return 3 if all_chances else 2
    elif pos.lower() == 'ss':
        return 7 if all_chances else 6
    elif pos.lower() == 'lf':
        return 2 if all_chances else 1
    elif pos.lower() == 'cf':
        return 3 if all_chances else 2
    else:
        return 2 if all_chances else 1


def oppo_fly(all_fly, oppo_rate):
    if all_fly == 0 or oppo_rate == 0:
        return 0
    else:
        return mround(all_fly * oppo_rate)


def groundball_a(all_gb, dp_rate):
    if all_gb == 0 or dp_rate == 0:
        return 0
    elif dp_rate > .6:
        return all_gb
    else:
        return mround(all_gb * (dp_rate * 1.5))


def balks(total_balks: int, innings: float, season_pct):
    if innings == 0:
        return 0
    return min(round((total_balks * 290 * season_pct) / innings), 20)


def wild_pitches(total_wps: int, innings: float, season_pct):
    if innings == 0:
        return 0
    return min(round((total_wps * 200 * season_pct) / innings), 20)


def closer_rating(gf: int, saves: int, games: int) -> str:
    if gf == 0 or games == 0 or saves == 0:
        return 'N'

    if gf / games >= .875:
        return '6'
    elif gf / games >= .8:
        return '5'
    elif gf / games >= .7:
        return '4'
    elif gf / games >= .55:
        return '3'
    elif gf / games >= .4:
        return '2'
    elif gf / games >= .25:
        return '1'
    elif gf / games >= .1:
        return '0'
    else:
        return 'N'
