<template>
  <div v-if="hasPitchingStats" class="row" id="career-pitching-row">
    <div class="col-sm-12">
      <h3>Pitching Stats</h3>
      <div class="table-responsive-xl">
        <table class="table table-sm table-striped" id="career-pitching">
          <thead class="thead-dark">
            <tr>
              <th>Season</th>
              <th>W</th>
              <th>L</th>
              <th>W-L%</th>
              <th>ERA</th>
              <th>G</th>
              <th>GS</th>
              <th>SV</th>
              <th>HD</th>
              <th>BSV</th>
              <th>IP</th>
              <th>H</th>
              <th>R</th>
              <th>ER</th>
              <th>HR</th>
              <th>BB</th>
              <th>SO</th>
              <th>HBP</th>
              <th>BK</th>
              <th>WP</th>
              <th>IR</th>
              <th>IRS</th>
              <th>WHIP</th>
              <th>H/9</th>
              <th>HR/9</th>
              <th>BB/9</th>
              <th>SO/9</th>
              <th>SO/BB</th>
            </tr>
          </thead>
          <tbody id="career-pitching-table">
            <tr v-for="stat in sortedRegularAndPostSeasonPitching">
              <td>S{{ stat.seasonNumber }}{{ stat.isRegularSeason ? '' : ' / Playoffs' }}</td>
              <td>{{ stat.win }}</td>
              <td>{{ stat.loss }}</td>
              <td>{{ winPercentage(stat) }}</td>
              <td>{{ stat.era.toFixed(2) }}</td>
              <td>{{ stat.games }}</td>
              <td>{{ stat.gs }}</td>
              <td>{{ stat.save }}</td>
              <td>{{ stat.hold }}</td>
              <td>{{ stat.bsave }}</td>
              <td>{{ outsToInnings(stat) }}</td>
              <td>{{ stat.hits }}</td>
              <td>{{ stat.run }}</td>
              <td>{{ stat.e_run }}</td>
              <td>{{ stat.hr }}</td>
              <td>{{ stat.bb }}</td>
              <td>{{ stat.so }}</td>
              <td>{{ stat.hbp }}</td>
              <td>{{ stat.balk }}</td>
              <td>{{ stat.wp }}</td>
              <td>{{ stat.ir }}</td>
              <td>{{ stat.ir_sc }}</td>
              <td>{{ stat.whip.toFixed(2) }}</td>
              <td>{{ hitsPer9(stat) }}</td>
              <td>{{ hrsPer9(stat) }}</td>
              <td>{{ stat.bbPer9.toFixed(1) }}</td>
              <td>{{ stat.kPer9.toFixed(1) }}</td>
              <td>{{ stat.kPerBB.toFixed(1) }}</td>
            </tr>
          </tbody>
          <tfoot>
            <tr v-if="careerPitchingStat" id="career-pitching-footer">
              <th>Career</th>
              <th>{{ careerPitchingStat.win }}</th>
              <th>{{ careerPitchingStat.loss }}</th>
              <th>{{ winPercentage(careerPitchingStat) }}</th>
              <th>{{ careerPitchingStat.era.toFixed(2) }}</th>
              <th>{{ careerPitchingStat.games }}</th>
              <th>{{ careerPitchingStat.gs }}</th>
              <th>{{ careerPitchingStat.save }}</th>
              <th>{{ careerPitchingStat.hold }}</th>
              <th>{{ careerPitchingStat.bsave }}</th>
              <th>{{ outsToInnings(careerPitchingStat) }}</th>
              <th>{{ careerPitchingStat.hits }}</th>
              <th>{{ careerPitchingStat.run }}</th>
              <th>{{ careerPitchingStat.e_run }}</th>
              <th>{{ careerPitchingStat.hr }}</th>
              <th>{{ careerPitchingStat.bb }}</th>
              <th>{{ careerPitchingStat.so }}</th>
              <th>{{ careerPitchingStat.hbp }}</th>
              <th>{{ careerPitchingStat.balk }}</th>
              <th>{{ careerPitchingStat.wp }}</th>
              <th>{{ careerPitchingStat.ir }}</th>
              <th>{{ careerPitchingStat.ir_sc }}</th>
              <th>{{ careerPitchingStat.whip.toFixed(2) }}</th>
              <th>{{ hitsPer9(careerPitchingStat) }}</th>
              <th>{{ hrsPer9(careerPitchingStat) }}</th>
              <th>{{ careerPitchingStat.bbPer9.toFixed(1) }}</th>
              <th>{{ careerPitchingStat.kPer9.toFixed(1) }}</th>
              <th>{{ careerPitchingStat.kPerBB.toFixed(1) }}</th>
            </tr>
          </tfoot>
        </table>
      </div>
    </div>
  </div>
</template>

<script lang="ts">
import { aggregatePitchingStats, type PitchingStat } from '@/services/pitchingStatsService'
import { hitsPer9, hrsPer9, outsToInnings, winPercentage } from '@/services/utilities'

interface PitchingStatWithSeason extends PitchingStat {
  seasonNumber: number
  isRegularSeason: boolean
}

export default {
  name: "PlayerCareerPitchingTable",
  props: {
    regularSeasonPitchingStats: { type: Array<PitchingStat>, required: true },
    postSeasonPitchingStats: { type: Array<PitchingStat>, required: true }
  },
  computed: {
    hasPitchingStats(): boolean {
      return !!(this.regularSeasonPitchingStats.length + this.postSeasonPitchingStats.length)
    },
    careerPitchingStat(): PitchingStat | undefined {
      if (this.regularSeasonPitchingStats.length > 0) {
        // old site behavior just summed regular season stats for the career line total
        return aggregatePitchingStats(this.regularSeasonPitchingStats)
      }

      return undefined
    },
    sortedRegularAndPostSeasonPitching(): PitchingStatWithSeason[] {
      let seasonStats: PitchingStatWithSeason[] = []

      if (this.regularSeasonPitchingStats?.length) {
        seasonStats = seasonStats.concat(this.regularSeasonPitchingStats.map(stat => {
          return {
            ...stat,
            seasonNumber: stat.player.season,
            isRegularSeason: true
          }
        }))
      }
      // TODO: here would be where you could filter out postseason stats if desired (like Josef requested)
      if (this.postSeasonPitchingStats?.length) {
        seasonStats = seasonStats.concat(this.postSeasonPitchingStats.map(stat => {
          return {
            ...stat,
            seasonNumber: stat.player.season,
            isRegularSeason: false
          }
        }))
      }

      return seasonStats.sort((s1, s2) => {
        return s1.seasonNumber - s2.seasonNumber === 0
          ? s1.isRegularSeason
            ? -1
            : 1
          : s1.seasonNumber - s2.seasonNumber
      })
    },
  },
  methods: {
    outsToInnings(stat: PitchingStat): string {
      return outsToInnings(stat)
    },
    winPercentage(stat: PitchingStat): string {
      return winPercentage(stat)
    },
    hitsPer9(stat: PitchingStat): string {
      return hitsPer9(stat)
    },
    hrsPer9(stat: PitchingStat): string {
      return hrsPer9(stat)
    }
  }
}
</script>
