<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>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" :key="stat.seasonNumber">
              <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>{{ formatIRSPercentage(stat) }}</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-for="(stat, idx) in careerPitchingStats" :key="idx">
              <th>Career{{ idx > 0 ? ' / Playoffs' : '' }}</th>
              <th>{{ stat.win }}</th>
              <th>{{ stat.loss }}</th>
              <th>{{ winPercentage(stat) }}</th>
              <th>{{ stat.era.toFixed(2) }}</th>
              <th>{{ stat.games }}</th>
              <th>{{ stat.gs }}</th>
              <th>{{ stat.save }}</th>
              <th>{{ stat.hold }}</th>
              <th>{{ stat.bsave }}</th>
              <th>{{ outsToInnings(stat) }}</th>
              <th>{{ stat.hits }}</th>
              <th>{{ stat.run }}</th>
              <th>{{ stat.e_run }}</th>
              <th>{{ stat.hr }}</th>
              <th>{{ stat.bb }}</th>
              <th>{{ stat.so }}</th>
              <th>{{ stat.hbp }}</th>
              <th>{{ stat.balk }}</th>
              <th>{{ stat.wp }}</th>
              <th>{{ stat.ir }}</th>
              <th>{{ stat.ir_sc }}</th>
              <th>{{ formatIRSPercentage(stat) }}</th>
              <th>{{ stat.whip.toFixed(2) }}</th>
              <th>{{ hitsPer9(stat) }}</th>
              <th>{{ hrsPer9(stat) }}</th>
              <th>{{ stat.bbPer9.toFixed(1) }}</th>
              <th>{{ stat.kPer9.toFixed(1) }}</th>
              <th>{{ stat.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 },
    showPostSeasonStats: { type: Boolean, required: true }
  },
  computed: {
    hasPitchingStats(): boolean {
      return !!(this.regularSeasonPitchingStats.length + this.postSeasonPitchingStats.length)
    },
    careerPitchingStats(): PitchingStat[] {
      let careerStats = []

      if (this.regularSeasonPitchingStats.length > 0) {
        careerStats.push(aggregatePitchingStats(this.regularSeasonPitchingStats))
      }

      if (this.showPostSeasonStats && this.postSeasonPitchingStats.length > 0) {
        careerStats.push(aggregatePitchingStats(this.postSeasonPitchingStats))
      }

      return careerStats
    },
    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
          }
        }))
      }

      if (this.showPostSeasonStats && 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)
    },
    formatIRSPercentage(stat: PitchingStat): string {
      if (stat.ir === 0) return '-'

      return `${(stat.ir_sc_pct * 100).toFixed(1)}%`
    }
  }
}
</script>
