<template>
  <div v-if="battingStats.length" class="row">
    <div class="col-sm-12">
      <h3>Team Batting {{ isRegularSeason ? '' : ' - Postseason' }}</h3>
      <div class="table-responsive-xl">
        <table class="table table-sm table-striped" id="table-batting-stats">
          <thead class="thead-dark">
            <tr style="min-width: 75px">
              <th>Player</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 battingStats">
              <td>
                <RouterLink
                  :to="{ name: 'player', params: { seasonNumber: seasonNumber, playerName: stat.player.name } }">
                  {{ stat.player.name }}
                </RouterLink>
              </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-if="totalBattingStat">
              <th>Total</th>
              <th>{{ totalBattingStat.pa }}</th>
              <th>{{ totalBattingStat.ab }}</th>
              <th>{{ totalBattingStat.run }}</th>
              <th>{{ totalBattingStat.hit }}</th>
              <th>{{ totalBattingStat.double }}</th>
              <th>{{ totalBattingStat.triple }}</th>
              <th>{{ totalBattingStat.hr }}</th>
              <th>{{ totalBattingStat.rbi }}</th>
              <th>{{ totalBattingStat.sb }}</th>
              <th>{{ totalBattingStat.cs }}</th>
              <th>{{ totalBattingStat.bb }}</th>
              <th>{{ totalBattingStat.so }}</th>
              <th>{{ totalBattingStat.avg.toFixed(3) }}</th>
              <th>{{ totalBattingStat.obp.toFixed(3) }}</th>
              <th>{{ totalBattingStat.slg.toFixed(3) }}</th>
              <th>{{ totalBattingStat.ops.toFixed(3) }}</th>
              <th>{{ totalBattingStat.woba.toFixed(3) }}</th>
              <th>{{ calculateStrikeoutPercent(totalBattingStat) }}</th>
              <th>{{ totalBattingStat.bphr }}</th>
              <th>{{ totalBattingStat.bpfo }}</th>
              <th>{{ totalBattingStat.bp1b }}</th>
              <th>{{ totalBattingStat.bplo }}</th>
              <th>{{ totalBattingStat.gidp }}</th>
              <th>{{ totalBattingStat.hbp }}</th>
              <th>{{ totalBattingStat.sac }}</th>
              <th>{{ totalBattingStat.ibb }}</th>
            </tr>
          </tfoot>
        </table>
      </div>
    </div>
  </div>
</template>

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

export default {
  name: "TeamBattingTable",
  props: {
    seasonNumber: { type: Number, required: true },
    teamId: { type: Number, required: true },
    isRegularSeason: { type: Boolean, required: true }
  },
  data() {
    return {
      battingStats: [] as BattingStat[]
    }
  },
  computed: {
    totalBattingStat(): BattingStat | undefined {
      if (this.battingStats.length > 0) {
        return aggregateBattingStats(this.battingStats)
      }

      return undefined
    }
  },
  created() {
    this.fetchData()
  },
  watch: {
    teamId(newValue, oldValue) {
      if (newValue !== oldValue)
        this.fetchData()
    }
  },
  methods: {
    async fetchData(): Promise<void> {
      const unsortedBattingStats: BattingStat[] = await fetchBattingStatsBySeasonAndTeamId(this.seasonNumber, this.teamId, this.isRegularSeason)
      this.battingStats = unsortedBattingStats.sort((s1, s2) => s2.pa - s1.pa)
    },
    calculateStrikeoutPercent(stat: BattingStat): string {
      if (!stat.pa) return 'N/A'
      return (stat.so * 100 / stat.pa).toFixed(1)
    }
  }
}
</script>
