<template>
  <div v-if="hasFieldingStats" class="row" id="career-fielding-row">
    <div class="col-sm-6">
      <h3>Fielding Stats</h3>
      <div class="table-responsive-xl">
        <table class="table table-sm table-striped" id="career-fielding">
          <thead class="thead-dark">
            <tr>
              <th>Season</th>
              <th>Pos</th>
              <th>XCh</th>
              <th>XH</th>
              <th>E</th>
              <th>PB</th>
              <th>SBa</th>
              <th>CSc</th>
              <th>CS%</th>
              <th>wF%</th>
            </tr>
          </thead>
          <tbody id="career-fielding-table">
            <tr v-for="stat in sortedRegularAndPostSeasonFielding">
              <td>S{{ stat.seasonNumber }}{{ stat.isRegularSeason ? '' : ' / Playoffs' }}</td>
              <td>{{ stat.pos }}</td>
              <td>{{ stat.xCheckCount }}</td>
              <td>{{ stat.hit }}</td>
              <td>{{ stat.error }}</td>
              <td>{{ stat.passedBallCount }}</td>
              <td>{{ stat.stolenBaseCheckCount }}</td>
              <td>{{ stat.caughtStealingCount }}</td>
              <td>{{ formatCaughtStealingPercent(stat) }}</td>
              <td>{{ formatWeightedFieldingPercent(stat) }}</td>
            </tr>
          </tbody>
          <tfoot>
            <tr v-for="stat in sortedCareerFieldingStats" id="career-fielding-footer">
              <th>Career</th>
              <th>{{ stat.pos }}</th>
              <th>{{ stat.xCheckCount }}</th>
              <th>{{ stat.hit }}</th>
              <th>{{ stat.error }}</th>
              <th>{{ stat.passedBallCount }}</th>
              <th>{{ stat.stolenBaseCheckCount }}</th>
              <th>{{ stat.caughtStealingCount }}</th>
              <th>{{ formatCaughtStealingPercent(stat) }}</th>
              <th>{{ formatWeightedFieldingPercent(stat) }}</th>
            </tr>
          </tfoot>
        </table>
      </div>
    </div>
  </div>
</template>

<script lang="ts">
import { aggregateFieldingStats, type FieldingStat } from '@/services/fieldingStatsService'

interface FieldingStatWithSeason extends FieldingStat {
  seasonNumber: number
  isRegularSeason: boolean
}

const POS_MAP = {
  'P': 1,
  'C': 2,
  '1B': 3,
  '2B': 4,
  '3B': 5,
  'SS': 6,
  'LF': 7,
  'CF': 8,
  'RF': 9,
  'TOT': 10
}

function compareFieldingStats(s1: FieldingStatWithSeason, s2: FieldingStatWithSeason): number {
  if (s1.seasonNumber - s2.seasonNumber !== 0) {
    return s1.seasonNumber - s2.seasonNumber
  }

  if (s1.isRegularSeason !== s2.isRegularSeason) {
    return s1.isRegularSeason
      ? -1
      : 1
  }

  // at this point stats are same season and also both regular or post season stats
  // and must be sorted on position order
  return POS_MAP[s1.pos] - POS_MAP[s2.pos]
}

export default {
  name: "PlayerCareerFieldingTable",
  props: {
    regularSeasonFieldingStats: { type: Array<FieldingStat>, required: true },
    postSeasonFieldingStats: { type: Array<FieldingStat>, required: true }
  },
  computed: {
    hasFieldingStats(): boolean {
      return !!(this.regularSeasonFieldingStats.length + this.postSeasonFieldingStats.length)
    },
    sortedCareerFieldingStats(): FieldingStat[] {
      if (this.regularSeasonFieldingStats.length > 0) {
        // old site behavior just summed regular season stats for the career line total
        return aggregateFieldingStats(this.regularSeasonFieldingStats)
          .sort((a, b) => POS_MAP[a.pos] - POS_MAP[b.pos]) //only need to sort careers totals by position
      }

      return []
    },
    sortedRegularAndPostSeasonFielding(): FieldingStatWithSeason[] {
      let seasonStats: FieldingStatWithSeason[] = []

      if (this.regularSeasonFieldingStats?.length) {
        seasonStats = seasonStats.concat(this.regularSeasonFieldingStats.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.postSeasonFieldingStats?.length) {
        seasonStats = seasonStats.concat(this.postSeasonFieldingStats.map(stat => {
          return {
            ...stat,
            seasonNumber: stat.player.season,
            isRegularSeason: false
          }
        }))
      }
      // TODO: additionally, fielding stats should sort on position P, C, 1B, ..., CF, RF
      return seasonStats.sort(compareFieldingStats)
    },
  },
  methods: {
    formatCaughtStealingPercent(stat: FieldingStat): string {
      if (stat.stolenBaseCheckCount === 0 || Number.isNaN(stat.caughtStealingPercent)) {
        return '-'
      }

      return `${(stat.caughtStealingPercent * 100).toFixed(1)}%`
    },
    formatWeightedFieldingPercent(stat: FieldingStat): string {
      return stat.weightedFieldingPercent.toFixed(3)
    }
  }
}
</script>
