<template>
  <Transition name="header-controls" appear>
    <div v-if="isVisible" class="header-controls">
      <div class="today-date" @click="goToToday">{{ todayString }}</div>
      <button
        type="button"
        class="hist-btn"
        :disabled="!calendarStore.historyCanUndo"
        @click="calendarStore.$history?.undo()"
        title="Undo (Ctrl+Z)"
        aria-label="Undo"
      >
        ↶
      </button>
      <button
        type="button"
        class="hist-btn"
        :disabled="!calendarStore.historyCanRedo"
        @click="calendarStore.$history?.redo()"
        title="Redo (Ctrl+Shift+Z)"
        aria-label="Redo"
      >
        ↷
      </button>
      <button
        type="button"
        class="settings-btn"
        @click="openSettings"
        aria-label="Open settings"
        title="Settings"
      >
        ⚙
      </button>
      <!-- Settings dialog now lives here -->
      <SettingsDialog ref="settingsDialog" />
    </div>
  </Transition>
  <button
    type="button"
    class="toggle-btn"
    @click="toggleVisibility"
    :aria-label="isVisible ? 'Hide controls' : 'Show controls'"
    :title="isVisible ? 'Hide controls' : 'Show controls'"
  >
    ⋯
  </button>
</template>

<script setup>
import { computed, ref, onMounted, onBeforeUnmount } from 'vue'
import { useCalendarStore } from '@/stores/CalendarStore'
import { formatTodayString } from '@/utils/date'
import SettingsDialog from '@/components/SettingsDialog.vue'

const calendarStore = useCalendarStore()

const todayString = computed(() => {
  const d = new Date(calendarStore.now)
  return formatTodayString(d)
})

const emit = defineEmits(['go-to-today'])

function goToToday() {
  // Emit the event so the parent can handle the viewport scrolling logic
  // since this component doesn't have access to viewport refs
  emit('go-to-today')
}

// Screen size detection and visibility toggle
const isVisible = ref(false)

function checkScreenSize() {
  const isSmallScreen = window.innerHeight < 600
  // Default to open on large screens, closed on small screens
  isVisible.value = !isSmallScreen
}

function toggleVisibility() {
  isVisible.value = !isVisible.value
}

// Settings dialog integration
const settingsDialog = ref(null)
function openSettings() {
  settingsDialog.value?.open()
}

onMounted(() => {
  checkScreenSize()
  window.addEventListener('resize', checkScreenSize)
})

onBeforeUnmount(() => {
  window.removeEventListener('resize', checkScreenSize)
})
</script>

<style scoped>
.header-controls {
  display: flex;
  justify-content: end;
  align-items: center;
  margin-right: 1.5rem;
}
.toggle-btn {
  position: fixed;
  top: 0;
  right: 0;
  background: transparent;
  border: none;
  color: var(--muted);
  padding: 0;
  margin: 0.5em;
  cursor: pointer;
  font-size: 1em;
  font-weight: 700;
  line-height: 1;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  outline: none;
  width: 1em;
  height: 1em;
  transition: all 0.2s ease;
}
.toggle-btn:hover {
  color: var(--strong);
}

.toggle-btn:active {
  transform: scale(0.9);
}
.header-controls-enter-active,
.header-controls-leave-active {
  transition: all 0.3s ease;
  overflow: hidden;
}

.header-controls-enter-from,
.header-controls-leave-to {
  opacity: 0;
  max-height: 0;
  transform: translateY(-20px);
}

.header-controls-enter-to,
.header-controls-leave-from {
  opacity: 1;
  max-height: 100px;
  transform: translateY(0);
}

.settings-btn {
  background: transparent;
  border: none;
  color: var(--muted);
  padding: 0;
  margin: 0;
  margin-right: 0.6rem;
  cursor: pointer;
  font-size: 1.5rem;
  line-height: 1;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  outline: none;
}

.hist-btn {
  background: transparent;
  border: none;
  color: var(--muted);
  padding: 0;
  margin: 0;
  cursor: pointer;
  font-size: 1.2rem;
  line-height: 1;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  outline: none;
  width: 1.9rem;
  height: 1.9rem;
}

.hist-btn:disabled {
  opacity: 0.35;
  cursor: default;
}

.hist-btn:not(:disabled):hover,
.hist-btn:not(:disabled):focus-visible {
  color: var(--strong);
}

.hist-btn:active:not(:disabled) {
  transform: scale(0.88);
}

.settings-btn:hover {
  color: var(--strong);
}

.today-date {
  white-space: pre-line;
  text-align: center;
  margin-right: 2rem;
}
</style>
