<script setup lang="ts">
import { LANE_IMAGES, lanePositionToIndex, POSITIONS_STR } from '~/utils/cdragon'

import type { LaneData } from 'match_collector'

defineProps<{
  championName?: string
  championLanes?: Array<LaneData>
  tierlistList?: boolean
}>()
const emit = defineEmits<{
  stateChange: [state: string, lane: number]
}>()

const state = ref('build')
const laneState = ref(0)

function handleStateChange(newState: string, newLane: number) {
  state.value = newState
  laneState.value = newLane
  emit('stateChange', newState, newLane)
}

const route = useRoute()
const selected = ref('')
if (route.path.startsWith('/tierlist/')) {
  const lane = route.params.lane as string
  selected.value = lane
}
</script>

<template>
  <div class="navbar-container">
    <NuxtLink class="logo-link" to="/" prefetch>
      <NuxtImg
        id="navbar-logo-img"
        format="webp"
        src="/buildpath-high-resolution-logo-transparent.png"
      />
    </NuxtLink>

    <div class="nav-scroll-container">
      <div v-for="(lane, i) in championLanes" :key="i" class="lane-section">
        <div class="nav-links-with-icon">
          <NuxtImg
            class="lane-icon"
            format="webp"
            width="24"
            height="24"
            :src="LANE_IMAGES[lanePositionToIndex(lane.data)]"
          />
          <div class="nav-buttons">
            <button
              :class="['nav-button', { active: state == 'build' && laneState == i }]"
              @click="handleStateChange('build', i)"
            >
              Build
            </button>
            <button
              :class="['nav-button', { active: state == 'alternatives' && laneState == i }]"
              @click="handleStateChange('alternatives', i)"
            >
              Alt
            </button>
            <button
              :class="['nav-button', { active: state == 'matchups' && laneState == i }]"
              @click="handleStateChange('matchups', i)"
            >
              Matchups
            </button>
          </div>
        </div>
      </div>

      <div v-if="tierlistList == true" class="tierlist-section">
        <h3 class="tierlist-title">Tierlist</h3>
        <div class="tierlist-icons">
          <NuxtLink
            v-for="(pos, i) in POSITIONS"
            :key="i"
            :to="'/tierlist/' + pos"
            class="tierlist-icon-link"
          >
            <NuxtImg
              class="tierlist-icon"
              format="webp"
              width="24"
              height="24"
              :src="LANE_IMAGES[i]"
              :alt="POSITIONS_STR[i]"
              :class="{ active: selected == pos }"
            />
          </NuxtLink>
        </div>
      </div>
    </div>
  </div>
</template>

<style>
.navbar-container {
  display: flex;
  align-items: center;

  position: fixed;
  bottom: 0;
  z-index: 10;

  background-color: #2b2826;
  width: 100%;
  height: 90px;
  padding: 0 10px;

  margin: 0px;
  box-sizing: border-box;
}

.nav-scroll-container {
  display: flex;
  align-items: center;
  gap: 12px;
  overflow-x: auto;
  flex: 1;
  padding-left: 10px;
  scrollbar-width: none; /* Firefox */
}

.nav-scroll-container::-webkit-scrollbar {
  display: none; /* Chrome, Safari */
}

.lane-section {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 4px;
  min-width: 90px;
  flex-shrink: 0;
}

.nav-links-with-icon {
  display: flex;
  align-items: center;
  gap: 6px;
}

.lane-icon {
  width: 24px;
  height: 24px;
  flex-shrink: 0;
}

.nav-buttons {
  display: flex;
  flex-direction: column;
  gap: 2px;
}

.nav-button {
  background: none;
  border: none;
  color: var(--color-on-surface);
  font-size: 11px;
  padding: 3px 6px;
  border-radius: 5px;
  cursor: pointer;
  white-space: nowrap;
  transition: all 0.2s ease;
  font-weight: 500;
}

.nav-button:hover {
  background-color: var(--color-surface-darker);
}

.nav-button.active {
  background-color: var(--color-surface);
  color: var(--color-on-surface);
}

.tierlist-section {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 6px;
  min-width: 120px;
  flex-shrink: 0;
}

.tierlist-title {
  font-size: 1.4rem;
  margin: 0;
  padding: 0;
  color: var(--color-on-surface);
  font-weight: 500;
}

.tierlist-icons {
  display: flex;
  gap: 6px;
}

.tierlist-icon {
  width: 30px;
  height: 30px;
  border-radius: 6px;
  transition: all 0.2s ease;
}

.tierlist-icon.active {
  background-color: var(--color-surface);
  border-radius: 4px;
}

.logo-link {
  display: flex;
  align-items: center;
  margin-right: 12px;
  flex-shrink: 0;
}

#navbar-logo-img {
  height: 40px;
  width: auto;
  max-width: 32px;
}

@media only screen and (min-width: 1200px) {
  .navbar-container {
    display: none;
  }
}
</style>
