<template>
  <div v-if="hasBattingStats" class="row" id="career-batting-row">
    <div class="col-sm-12">
      <h3>Batting Stats</h3>
      <div class="table-responsive-xl">
        <table class="table table-sm table-striped" id="career-batting">
          <thead class="thead-dark">
            <tr>
              <th>Season</th>
              <th>PA</th>
              <th>AB</th>
              <th>R</th>
              <th>H</th>
              <th>2B</th>
              <th>3B</th>
              <th>HR</th>
              <th>RBI</th>
              <th>SB</th>
              <th>CS</th>
              <th>BB</th>
              <th>SO</th>
              <th>BA</th>
              <th>OBP</th>
              <th>SLG</th>
              <th>OPS</th>
              <th>wOBA</th>
              <th>K%</th>
              <th>BPHR</th>
              <th>BPFO</th>
              <th>BP1B</th>
              <th>BPLO</th>
              <th>GIDP</th>
              <th>HBP</th>
              <th>SAC</th>
              <th>IBB</th>
            </tr>
          </thead>
          <tbody id="career-batting-table">
            <tr v-for="stat in sortedRegularAndPostSeasonBatting">
              <td>S{{ stat.seasonNumber }}{{ stat.isRegularSeason ? '' : ' / Playoffs' }}</td>
              <td>{{ stat.pa }}</td>
              <td>{{ stat.ab }}</td>
              <td>{{ stat.run }}</td>
              <td>{{ stat.hit }}</td>
              <td>{{ stat.double }}</td>
              <td>{{ stat.triple }}</td>
              <td>{{ stat.hr }}</td>
              <td>{{ stat.rbi }}</td>
              <td>{{ stat.sb }}</td>
              <td>{{ stat.cs }}</td>
              <td>{{ stat.bb }}</td>
              <td>{{ stat.so }}</td>
              <td>{{ stat.avg.toFixed(3) }}</td>
              <td>{{ stat.obp.toFixed(3) }}</td>
              <td>{{ stat.slg.toFixed(3) }}</td>
              <td>{{ stat.ops.toFixed(3) }}</td>
              <td>{{ stat.woba.toFixed(3) }}</td>
              <td>{{ calculateStrikeoutPercent(stat) }}</td>
              <td>{{ stat.bphr }}</td>
              <td>{{ stat.bpfo }}</td>
              <td>{{ stat.bp1b }}</td>
              <td>{{ stat.bplo }}</td>
              <td>{{ stat.gidp }}</td>
              <td>{{ stat.hbp }}</td>
              <td>{{ stat.sac }}</td>
              <td>{{ stat.ibb }}</td>
            </tr>
          </tbody>
          <tfoot>
            <tr v-for="(stat, idx) in careerBattingStats">
              <th>Career{{ idx > 0 ? ' / Playoffs' : '' }}</th>
              <th>{{ stat.pa }}</th>
              <th>{{ stat.ab }}</th>
              <th>{{ stat.run }}</th>
              <th>{{ stat.hit }}</th>
              <th>{{ stat.double }}</th>
              <th>{{ stat.triple }}</th>
              <th>{{ stat.hr }}</th>
              <th>{{ stat.rbi }}</th>
              <th>{{ stat.sb }}</th>
              <th>{{ stat.cs }}</th>
              <th>{{ stat.bb }}</th>
              <th>{{ stat.so }}</th>
              <th>{{ stat.avg.toFixed(3) }}</th>
              <th>{{ stat.obp.toFixed(3) }}</th>
              <th>{{ stat.slg.toFixed(3) }}</th>
              <th>{{ stat.ops.toFixed(3) }}</th>
              <th>{{ stat.woba.toFixed(3) }}</th>
              <th>{{ calculateStrikeoutPercent(stat) }}</th>
              <th>{{ stat.bphr }}</th>
              <th>{{ stat.bpfo }}</th>
              <th>{{ stat.bp1b }}</th>
              <th>{{ stat.bplo }}</th>
              <th>{{ stat.gidp }}</th>
              <th>{{ stat.hbp }}</th>
              <th>{{ stat.sac }}</th>
              <th>{{ stat.ibb }}</th>
            </tr>
          </tfoot>
        </table>
      </div>
    </div>
  </div>
</template>

<script lang="ts">
import { aggregateBattingStats, type BattingStat } from '@/services/battingStatsService'

interface BattingStatWithSeason extends BattingStat {
  seasonNumber: number
  isRegularSeason: boolean
}

export default {
  name: "PlayerCareerBattingTable",
  props: {
    regularSeasonBattingStats: { type: Array<BattingStat>, required: true },
    postSeasonBattingStats: { type: Array<BattingStat>, required: true },
    showPostSeasonStats: { type: Boolean, required: true }
  },
  computed: {
    hasBattingStats(): boolean {
      return !!(this.regularSeasonBattingStats.length + this.postSeasonBattingStats.length)
    },
    careerBattingStats(): BattingStat[] {
      let careerStats = []

      if (this.regularSeasonBattingStats.length > 0) {
        careerStats.push(aggregateBattingStats(this.regularSeasonBattingStats))
      }

      if (this.showPostSeasonStats && this.postSeasonBattingStats.length > 0) {
        careerStats.push(aggregateBattingStats(this.postSeasonBattingStats))
      }

      return careerStats
    },
    sortedRegularAndPostSeasonBatting(): BattingStatWithSeason[] {
      let seasonStats: BattingStatWithSeason[] = []

      if (this.regularSeasonBattingStats?.length) {
        seasonStats = seasonStats.concat(this.regularSeasonBattingStats.map(stat => {
          return {
            ...stat,
            seasonNumber: stat.player.season,
            isRegularSeason: true
          }
        }))
      }

      if (this.showPostSeasonStats && this.postSeasonBattingStats?.length) {
        seasonStats = seasonStats.concat(this.postSeasonBattingStats.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: {
    calculateStrikeoutPercent(stat: BattingStat): string {
      if (!stat.pa) return 'N/A'
      return (stat.so * 100 / stat.pa).toFixed(1)
    },
  }
}
</script>
