<template>
  <div class="main-layout">
    <header class="app-header">
      <div class="toolbar-title">mitlist</div>



      <div class="flex align-end">
        <div class="language-selector" v-if="authStore.isAuthenticated">
          <button @click="toggleLanguageMenu" class="language-menu-button">
            <svg xmlns="http://www.w3.org/2000/svg" height="20px" viewBox="0 0 24 24" width="20px" fill="#ff7b54">
              <path d="M0 0h24v24H0z" fill="none" />
              <path
                d="m12.87 15.07-2.54-2.51.03-.03c1.74-1.94 2.98-4.17 3.71-6.53H17V4h-7V2H8v2H1v1.99h11.17C11.5 7.92 10.44 9.75 9 11.35 8.07 10.32 7.3 9.19 6.69 8h-2c.73 1.63 1.73 3.17 2.98 4.56l-5.09 5.02L4 19l5-5 3.11 3.11.76-2.04zM18.5 10h-2L12 22h2l1.12-3h4.75L21 22h2l-4.5-12zm-2.62 7l1.62-4.33L19.12 17h-3.24z" />
            </svg>
            <span class="current-language">{{ currentLanguageCode.toUpperCase() }}</span>
          </button>
          <div v-if="languageMenuOpen" class="dropdown-menu language-dropdown" ref="languageMenuDropdown">
            <div class="dropdown-header">{{ $t('languageSelector.title') }}</div>
            <a v-for="(name, code) in availableLanguages" :key="code" href="#" @click.prevent="changeLanguage(code)"
              class="language-option" :class="{ 'active': currentLanguageCode === code }">
              {{ name }}
            </a>
          </div>
        </div>

        <div class="user-menu" v-if="authStore.isAuthenticated">
          <button @click="toggleUserMenu" class="user-menu-button">
            <!-- Placeholder for user icon -->
            <svg xmlns="http://www.w3.org/2000/svg" height="24px" viewBox="0 0 24 24" width="24px" fill="#ff7b54">
              <path d="M0 0h24v24H0z" fill="none" />
              <path
                d="M12 12c2.21 0 4-1.79 4-4s-1.79-4-4-4-4 1.79-4 4 1.79 4 4 4zm0 2c-2.67 0-8 1.34-8 4v2h16v-2c0-2.66-5.33-4-8-4z" />
            </svg>
          </button>
          <div v-if="userMenuOpen" class="dropdown-menu" ref="userMenuDropdown">
            <a href="#" @click.prevent="handleLogout">Logout</a>
          </div>
        </div>
      </div>
    </header>

    <main class="page-container">
      <router-view v-slot="{ Component, route }">
        <keep-alive v-if="route.meta.keepAlive">
          <component :is="Component" />
        </keep-alive>
        <component v-else :is="Component" :key="route.fullPath" />
      </router-view>
    </main>

    <OfflineIndicator />

    <footer class="app-footer">
      <nav class="tabs">
        <router-link to="/lists" class="tab-item" active-class="active">
          <span class="material-icons">list</span>
          <span class="tab-text">Lists</span>
        </router-link>
        <a @click.prevent="navigateToGroups" href="/groups" class="tab-item"
          :class="{ 'active': $route.path.startsWith('/groups') }">
          <span class="material-icons">group</span>
          <span class="tab-text">Groups</span>
        </a>
        <router-link to="/chores" class="tab-item" active-class="active">
          <span class="material-icons">person_pin_circle</span>
          <span class="tab-text">Chores</span>
        </router-link>
        <!-- <router-link to="/account" class="tab-item" active-class="active">
          <span class="material-icons">person</span>
          <span class="tab-text">Account</span>
        </router-link> -->
      </nav>
    </footer>
  </div>
</template>

<script setup lang="ts">
import { ref, defineComponent, onMounted, computed } from 'vue';
import { useRouter, useRoute } from 'vue-router';
import { useAuthStore } from '@/stores/auth';
import OfflineIndicator from '@/components/OfflineIndicator.vue';
import { onClickOutside } from '@vueuse/core';
import { useNotificationStore } from '@/stores/notifications';
import { useGroupStore } from '@/stores/groupStore';
import { useI18n } from 'vue-i18n';

defineComponent({
  name: 'MainLayout'
});

const router = useRouter();
const route = useRoute();
const authStore = useAuthStore();
const notificationStore = useNotificationStore();
const groupStore = useGroupStore();
const { t, locale } = useI18n();

// Add initialization logic
const initializeApp = async () => {
  if (authStore.isAuthenticated) {
    try {
      await authStore.fetchCurrentUser();
    } catch (error) {
      console.error('Failed to initialize app:', error);
      // Don't automatically logout - let the API interceptor handle token refresh
      // The response interceptor will handle 401s and refresh tokens automatically
    }
  }
};

// Call initialization when component is mounted
onMounted(() => {
  initializeApp();
  if (authStore.isAuthenticated) {
    groupStore.fetchGroups();
  }

  // Load saved language from localStorage
  const savedLanguage = localStorage.getItem('language');
  if (savedLanguage && ['en', 'de', 'nl', 'fr', 'es'].includes(savedLanguage)) {
    locale.value = savedLanguage;
  }
});

const userMenuOpen = ref(false);
const userMenuDropdown = ref<HTMLElement | null>(null);

const toggleUserMenu = () => {
  userMenuOpen.value = !userMenuOpen.value;
};

onClickOutside(userMenuDropdown, () => {
  userMenuOpen.value = false;
}, { ignore: ['.user-menu-button'] });

// Language selector state and functions
const languageMenuOpen = ref(false);
const languageMenuDropdown = ref<HTMLElement | null>(null);

const availableLanguages = computed(() => ({
  en: t('languageSelector.languages.en'),
  de: t('languageSelector.languages.de'),
  nl: t('languageSelector.languages.nl'),
  fr: t('languageSelector.languages.fr'),
  es: t('languageSelector.languages.es')
}));

const currentLanguageCode = computed(() => locale.value);

const toggleLanguageMenu = () => {
  languageMenuOpen.value = !languageMenuOpen.value;
};

const changeLanguage = (languageCode: string) => {
  locale.value = languageCode;
  localStorage.setItem('language', languageCode);
  languageMenuOpen.value = false;
  notificationStore.addNotification({
    type: 'success',
    message: `Language changed to ${availableLanguages.value[languageCode as keyof typeof availableLanguages.value]}`,
  });
};

onClickOutside(languageMenuDropdown, () => {
  languageMenuOpen.value = false;
}, { ignore: ['.language-menu-button'] });

const handleLogout = async () => {
  try {
    authStore.logout(); // Pinia action
    notificationStore.addNotification({
      type: 'success',
      message: 'Logged out successfully',
    });
    await router.push('/auth/login'); // Adjusted path
  } catch (error: unknown) {
    notificationStore.addNotification({
      type: 'error',
      message: error instanceof Error ? error.message : 'Logout failed',
    });
  }
  userMenuOpen.value = false;
};

const navigateToGroups = () => {
  // The groups should have been fetched on mount, but we can check isLoading
  if (groupStore.isLoading) {
    // Maybe show a toast or do nothing
    console.log('Groups are still loading...');
    return;
  }
  if (groupStore.groupCount === 1 && groupStore.firstGroupId) {
    router.push(`/groups/${groupStore.firstGroupId}`);
  } else {
    router.push('/groups');
  }
};
</script>

<style lang="scss" scoped>
.main-layout {
  display: flex;
  flex-direction: column;
  min-height: 100vh;
}

.app-header {
  background-color: #fff8f0;
  padding: 0 1rem;
  height: var(--header-height);
  display: flex;
  align-items: center;
  justify-content: space-between;
  box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
  position: sticky;
  top: 0;
  z-index: 100;
}

.toolbar-title {
  font-size: 1.5rem;
  font-weight: 500;
  letter-spacing: 0.1ch;
  color: var(--primary);
}

.language-selector {
  position: relative;
}

.language-menu-button {
  background: none;
  border: none;
  color: var(--primary);
  cursor: pointer;
  padding: 0.5rem;
  border-radius: 8px;
  display: flex;
  align-items: center;
  gap: 0.25rem;

  &:hover {
    background-color: rgba(255, 123, 84, 0.1);
  }
}

.current-language {
  font-size: 0.75rem;
  font-weight: 600;
  letter-spacing: 0.5px;
}

.language-dropdown {
  min-width: 180px;

  .dropdown-header {
    padding: 0.5rem 1rem;
    font-size: 0.75rem;
    font-weight: 600;
    color: var(--text-color);
    opacity: 0.7;
    text-transform: uppercase;
    letter-spacing: 0.5px;
    border-bottom: 1px solid #e0e0e0;
  }

  .language-option {
    display: block;
    padding: 0.75rem 1rem;
    color: var(--text-color);
    text-decoration: none;

    &:hover {
      background-color: #f5f5f5;
    }

    &.active {
      background-color: rgba(255, 123, 84, 0.1);
      color: var(--primary);
      font-weight: 500;
    }
  }
}

.dropdown-menu {
  position: absolute;
  right: 0;
  top: calc(100% + 5px);
  color: var(--primary);
  background-color: #f3f3f3;
  border: 1px solid #ddd;
  border-radius: 4px;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.15);
  min-width: 150px;
  z-index: 101;

  a {
    display: block;
    padding: 0.5rem 1rem;
    color: var(--text-color);
    text-decoration: none;

    &:hover {
      background-color: #f5f5f5;
    }
  }
}

.user-menu {
  position: relative;
}

.user-menu-button {
  background: none;
  border: none;
  color: var(--primary);
  cursor: pointer;
  padding: 0.5rem;
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;

  &:hover {
    background-color: rgba(255, 255, 255, 0.1);
  }
}

.page-container {
  flex-grow: 1;
  padding-bottom: calc(var(--footer-height) + 1rem); // Space for fixed footer
}

.app-footer {
  background-color: white;
  border-top: 1px solid #e0e0e0;
  height: var(--footer-height);
  position: fixed;
  bottom: 0;
  left: 0;
  right: 0;
  z-index: 100;
}

.tabs {
  display: flex;
  height: 100%;
}

.tab-item {
  flex-grow: 1;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  color: var(--text-color);
  text-decoration: none;
  font-size: 0.8rem;
  padding: 0.5rem 0;
  border-bottom: 2px solid transparent;
  gap: 4px;

  .material-icons {
    font-size: 24px;
  }

  .tab-text {
    display: none;
  }

  @media (min-width: 768px) {
    flex-direction: row;
    gap: 8px;

    .tab-text {
      display: inline;
    }
  }

  &.active {
    color: var(--primary);
    border-bottom-color: var(--primary);
  }

  &:hover {
    background-color: #f0f0f0;
  }
}
</style>
