<template>
  <div class="app-container">
    <header v-if="isAuthenticated" class="top-bar">
      <div class="user-info-top">
        <span>{{ user.nickname }}</span>
        <span>💰 {{ displayedCoins }}</span>
        <span>✨ {{ displayedExp }}</span>
        <button @click="handleLogout" class="logout-button">Logout</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;
}

.top-bar {
  background-color: #5a4b3a; /* Dark earthy brown */
  padding: 10px 15px;
  border-bottom: 2px solid #4a3b2a; /* Darker brown border */
  display: flex;
  justify-content: flex-end; /* Align user info to the right */
  align-items: center;
  color: #f0ead6; /* Creamy white text */
}

.user-info-top {
  display: flex;
  align-items: center;
  gap: 15px;
  font-size: 0.9em;
}

.logout-button {
  background-color: #a34a2a; /* Burnt orange/reddish brown */
  color: white;
  border: 1px solid #7b3b22;
  padding: 5px 10px;
  border-radius: 5px;
  cursor: pointer;
  font-size: 0.8em;
  transition: background-color 0.2s;
}

.logout-button:hover {
  background-color: #8e3f22;
}

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

.bottom-nav {
  position: fixed;
  bottom: 0;
  left: 0;
  width: 100%;
  background-color: #5a4b3a; /* Dark earthy brown */
  border-top: 2px solid #4a3b2a; /* Darker brown border */
  display: flex;
  justify-content: space-around;
  padding: 5px 0;
  box-shadow: 0 -2px 5px rgba(0, 0, 0, 0.2); /* Slightly darker shadow */
  z-index: 1000;
}

.nav-item {
  display: flex;
  flex-direction: column;
  align-items: center;
  text-decoration: none;
  color: #f0ead6; /* Creamy white text */
  font-size: 0.7em;
  padding: 5px;
  transition: color 0.2s;
}

.nav-item:hover {
  color: #ffcc00; /* Gold/yellow on hover */
}

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

.nav-item.router-link-active {
  color: #ffcc00; /* Gold/yellow for active link */
}
</style>
