<template>
  <div class="app-container">
    <header v-if="isAuthenticated" class="top-bar">
      <div class="user-info-top">
        <span>💰 {{ displayedCoins }}</span>
        <span>✨ {{ displayedExp }}</span>
        <button @click="handleLogout" class="btn btn-danger btn-sm">Выход</button>
      </div>
    </header>

    <main class="main-content">
      <slot />
    </main>

    <footer v-if="isAuthenticated" class="bottom-nav">
      <NuxtLink to="/" class="nav-item">
        <span class="icon">🏠</span>
        <span class="label">Главная</span>
      </NuxtLink>
      <NuxtLink to="/habits" class="nav-item">
        <span class="icon">🎯</span>
        <span class="label">Привычки</span>
      </NuxtLink>
      <NuxtLink to="/village" class="nav-item">
        <span class="icon">🏞️</span>
        <span class="label">Деревня</span>
      </NuxtLink>
      <NuxtLink to="/leaderboard" class="nav-item">
        <span class="icon">🏆</span>
        <span class="label">Лидерборд</span>
      </NuxtLink>
    </footer>
  </div>
</template>

<script setup>
import { ref, watch } from 'vue';

const { user, isAuthenticated, logout } = useAuth();

const handleLogout = async () => {
  await logout();
};

// Refs for the displayed values
const displayedCoins = ref(user.value?.coins ?? 0);
const displayedExp = ref(user.value?.exp ?? 0);

// Animation function
const animateValue = (start, end, onUpdate) => {
  if (start === end) return;

  const range = end - start;
  const increment = range > 0 ? 1 : -1;
  const duration = 500; // Total animation time
  const stepTime = Math.abs(Math.floor(duration / range));

  let current = start;
  const timer = setInterval(() => {
    current += increment;
    onUpdate(current);
    if (current === end) {
      clearInterval(timer);
    }
  }, stepTime > 0 ? stepTime : 1);
};

// Watch for changes in user's coins and animate
watch(() => user.value?.coins, (newCoins, oldCoins) => {
  if (newCoins !== undefined && oldCoins !== undefined) {
    animateValue(oldCoins, newCoins, (value) => {
      displayedCoins.value = value;
    });
  } else if (newCoins !== undefined) {
    displayedCoins.value = newCoins;
  }
});

// Watch for changes in user's exp and animate
watch(() => user.value?.exp, (newExp, oldExp) => {
  if (newExp !== undefined && oldExp !== undefined) {
    animateValue(oldExp, newExp, (value) => {
      displayedExp.value = value;
    });
  } else if (newExp !== undefined) {
    displayedExp.value = newExp;
  }
});
</script>

<style scoped>
.app-container {
  display: flex;
  flex-direction: column;
  min-height: 100vh;
  background-color: var(--background-color);
}

.top-bar {
  background-color: var(--container-bg-color);
  padding: 10px 20px;
  border-bottom: 1px solid var(--border-color);
  display: flex;
  justify-content: flex-end;
  align-items: center;
  color: var(--text-color);
  box-shadow: 0 2px 4px rgba(0,0,0,0.05);
}

.user-info-top {
  display: flex;
  align-items: center;
  gap: 20px;
  font-size: 0.95em;
  font-weight: 500;
}

.main-content {
  flex-grow: 1;
  padding-bottom: 70px; /* Space for bottom nav */
}

.bottom-nav {
  position: fixed;
  bottom: 0;
  left: 0;
  width: 100%;
  background-color: var(--container-bg-color);
  border-top: 1px solid var(--border-color);
  display: flex;
  justify-content: space-around;
  padding: 5px 0;
  box-shadow: 0 -2px 5px rgba(0, 0, 0, 0.05);
  z-index: 1000;
}

.nav-item {
  display: flex;
  flex-direction: column;
  align-items: center;
  text-decoration: none;
  color: var(--text-color-light);
  font-size: 0.75em;
  padding: 5px;
  transition: color 0.2s;
}

.nav-item:hover {
  color: var(--primary-color);
}

.nav-item .icon {
  font-size: 1.8em;
  margin-bottom: 2px;
}

.nav-item.router-link-exact-active {
  color: var(--primary-color);
  font-weight: 600;
}
</style>
