// @flow
import { attr, abil, specialtiesFor } from '..'
import { maxExcellency } from '../excellencies'
import { halfRoundUp } from 'utils'
import type { Character, Pool } from 'utils/flow-types'

export default function rating(
  name: string,
  character: Character,
  attribute: string,
  ability: string,
  penalties: Array<Object>,
  excellencyAbils: Array<string>,
  bonus: Array<Object> = []
): Pool {
  const _attr = attr(character, attribute)
  const _abil = abil(character, ability)
  const pool = _attr + _abil
  const specialties = specialtiesFor(character, ability)

  const excellency = maxExcellency(
    character,
    attribute,
    ability,
    excellencyAbils,
    true
  )
  const excellencyStunt = maxExcellency(
    character,
    attribute,
    ability,
    excellencyAbils,
    true,
    true
  )
  const penalty = penalties.reduce((a, p) => a + p.penalty, 0)

  return {
    name: name,
    attribute: attribute,
    attributeRating: _attr,
    ability: ability,
    abilityRating: _abil,
    raw: halfRoundUp(pool),
    specialtyMatters: pool % 2 === 0 && specialties.length > 0,
    specialties: specialties,
    excellency: excellency,
    excellencyCost: excellency * 2,
    excellencyStunt: excellencyStunt,
    excellencyStuntCost: excellencyStunt * 2,
    penalties: penalties.filter(p => p.penalty > 0),
    totalPenalty: penalty,
    total: Math.max(halfRoundUp(pool) - penalty, 0),
    bonus: bonus,
    rating: true,
    specialAttacks: [],
  }
}

@@ -574,16 +574,124 @@
 ility)%0A%0A
+  let mb = 0%0A  if (bonus.length %3E 0)%0A    mb = bonus.reduce((a, b) =%3E a + (b.situational ? 0 : b.bonus), 0)%0A%0A
   const 
@@ -1503,16 +1503,21 @@
 Up(pool)
+ + mb
  - penal

Make sure non-situational bonuses to ratings actually apply