<template>
  <div class="taskbar">
    <div class="start-section">
      <button class="start-button" @click="toggleStartMenu">
        <span class="start-icon">🪟</span>
        <span>{{ t('start') }}</span>
      </button>
      <div v-if="showStartMenu" class="start-menu">
        <div class="menu-item" @click="openAboutMe">
          {{ t('about_me') }}
        </div>
        <!-- More menu items can go here -->
        <div class="menu-item" @click="closeAllWindows">
          {{ t('close_all_windows') }}
        </div>
      </div>
    </div>
    <div class="task-buttons">
      <button
        v-for="window in minimizedWindows"
        :key="window.id"
        class="task-button"
        @click="restoreMinimizedWindow(window.id)"
      >
        {{ t(window.title) }}
      </button>
    </div>
    <div class="right-section">
      <div class="language-toggle" @click="toggleLanguage">
        <span>{{ locale === 'zh' ? '注音' : 'ABC' }}</span>
      </div>
      <div class="clock">
        <span>{{ currentTime }}</span>
      </div>
    </div>
  </div>
</template>

<script setup>
import { ref, onMounted, onUnmounted } from 'vue';
import { useI18n } from 'vue-i18n';

const { t, locale } = useI18n();

const props = defineProps({
  minimizedWindows: {
    type: Array,
    default: () => [],
  },
});

const currentTime = ref(new Date().toLocaleTimeString());
let timerId = null;
const showStartMenu = ref(false);

const emit = defineEmits(['open-about-me', 'restore-window', 'close-all-windows']);

const toggleStartMenu = () => {
  showStartMenu.value = !showStartMenu.value;
};

const openAboutMe = () => {
  emit('open-about-me');
  showStartMenu.value = false;
};

const closeAllWindows = () => {
  emit('close-all-windows');
  showStartMenu.value = false;
};

const toggleLanguage = () => {
  locale.value = locale.value === 'zh' ? 'en' : 'zh';
};

const restoreMinimizedWindow = (id) => {
  emit('restore-window', id);
};

onMounted(() => {
  timerId = setInterval(() => {
    currentTime.value = new Date().toLocaleTimeString();
  }, 1000);
});

onUnmounted(() => {
  if (timerId) {
    clearInterval(timerId);
  }
});
</script>

<style scoped>
.taskbar {
  display: flex;
  justify-content: space-between;
  align-items: center;
  background-color: #c0c0c0; /* Classic Windows gray */
  padding: 4px 6px;
  border: 2px solid;
  border-color: #ffffff #808080 #808080 #ffffff; /* 3D effect */
  width: 100%;
  box-sizing: border-box;
  height: 40px;
  position: relative; /* For positioning the start menu */
}

.start-section {
  position: relative;
  display: flex;
  align-items: center;
  flex-grow: 0; /* Don't let start section grow */
}

.right-section {
  display: flex;
  align-items: center;
  gap: 6px;
}

.task-buttons {
  display: flex;
  flex-grow: 1; /* Allow task buttons to take available space */
  margin-left: 10px; /* Space between start button and task buttons */
  gap: 6px;
  overflow-x: auto; /* Allow horizontal scrolling if many windows */
  padding-bottom: 2px; /* Prevent scrollbar from overlapping border */
}

.task-button {
  padding: 4px 8px;
  border: 2px solid;
  border-color: #808080 #ffffff #ffffff #808080;
  background-color: #c0c0c0;
  font-weight: bold;
  font-size: 0.9rem;
  cursor: pointer;
  color: black;
  white-space: nowrap; /* Prevent text wrapping */
  flex-shrink: 0; /* Prevent buttons from shrinking */
}

.task-button:active {
  border-color: #ffffff #808080 #808080 #ffffff;
}

.start-button {
  display: flex;
  align-items: center;
  padding: 4px 8px;
  border: 2px solid;
  border-color: #ffffff #808080 #808080 #ffffff;
  background-color: #c0c0c0;
  font-weight: bold;
  font-size: 1rem;
  cursor: pointer;
  color: black;
}

.start-button:active {
  border-color: #808080 #ffffff #ffffff #808080;
}

.start-icon {
  margin-right: 8px;
}

.start-menu {
  position: absolute;
  top: 100%;
  left: 0;
  background-color: #c0c0c0;
  border: 2px solid;
  border-color: #ffffff #808080 #808080 #ffffff;
  padding: 4px;
  min-width: 160px;
  z-index: 1001;
}

.menu-item {
  padding: 4px 8px;
  cursor: pointer;
  white-space: nowrap;
  border: 1px solid transparent;
  color: black;
}

.menu-item:hover {
  background-color: #000080;
  color: white;
  border-color: #000080;
}

.language-toggle {
  padding: 4px 8px;
  border: 2px solid;
  border-color: #808080 #ffffff #ffffff #808080;
  font-size: 0.9rem;
  color: black;
  cursor: pointer;
  background-color: #c0c0c0;
}

.language-toggle:active {
  border-color: #ffffff #808080 #808080 #ffffff;
}

.clock {
  padding: 4px 8px;
  border: 2px solid;
  border-color: #808080 #ffffff #ffffff #808080;
  font-size: 0.9rem;
  color: black;
  background-color: #c0c0c0;
}
</style>
