<script setup>
import { ref, onMounted, onUnmounted } from 'vue'
import { theme, accent, setTheme, setAccent } from '~/composables/theme'

// ref for dropdown visibility
const dropdownVisible = ref(false)

// toggle function
const toggleDropdown = () => {
  dropdownVisible.value = !dropdownVisible.value
}

// close dropdown if click outside
const menuContainer = ref(null)

const handleClickOutside = (e) => {
  if (menuContainer.value && !menuContainer.value.contains(e.target)) {
    dropdownVisible.value = false
  }
}

const isNarrow = ref(false)

const checkWidth = () => {
  isNarrow.value = window.innerWidth < 480
}

onMounted(() => {
  document.addEventListener('click', handleClickOutside)
  checkWidth()
  window.addEventListener('resize', checkWidth)
})

onUnmounted(() => {
  document.removeEventListener('click', handleClickOutside)
  window.removeEventListener('resize', checkWidth)
})
</script>

<template>
 <div class="menu-container" ref="menuContainer">
  <button class="menu-button" @click="toggleDropdown">{{ $t('site_options.theme_selector.dropdown') }} ▾</button>

  <Transition name="dropdown">
    <div class="dropdown" v-show="dropdownVisible">
      <div class="two-columns" :class="{ 'stacked': isNarrow }">
          <div class="section">
              <div class="menu-section">
                  <div class="menu-header">
                      {{ $t('site_options.theme_selector.dropdown') }}
                  </div>
              </div>
              <div class="divider"></div>
              <div class="menu-section">
                  <div :class="{'menu-item': true, 'active': theme === 'dark' }" @click="setTheme('dark')">
                      <div class="circle dark"></div>
                      {{ $t('themes.dark') }}
                  </div>
                  <div :class="{'menu-item': true, 'active': theme === 'light' }" @click="setTheme('light')">
                      <div class="circle light"></div>
                      {{ $t('themes.light') }}
                  </div>
              </div>
          </div>
          <div class="section">
              <div class="menu-section">
                  <div class="menu-header">
                      {{ $t('site_options.theme_selector.dragon') }}
                  </div>
              </div>
              <div class="divider"></div>
              <div class="menu-section">
                  <div :class="{'menu-item': true, 'active': accent === 'katlum' }" @click="setAccent('katlum')">
                      <div class="circle katlum"></div>
                      {{ $t('themes.accents.katlum') }}
                  </div>
                  <div :class="{'menu-item': true, 'active': accent === 'solus' }" @click="setAccent('solus')">
                      <div class="circle solus"></div>Solus
                  </div>
                  <div :class="{'menu-item': true, 'active': accent === 'silang' }" @click="setAccent('silang')">
                      <div class="circle silang"></div>Silang
                    </div>
                    <!--
                  <div :class="{'menu-item': true, 'active': accent === 'nozt' }" @click="setAccent('nozt')">
                      <div class="circle nozt"></div>Nozt
                  </div>
                  <div :class="{'menu-item': true, 'active': accent === 'albor' }" @click="setAccent('albor')">
                      <div class="circle albor"></div>Albor
                  </div>
                  -->
              </div>
          </div>
      </div>
    </div>
  </Transition>
</div>
</template>

<style lang="scss" scoped>
.circle {
    width: 10px;
    height: 10px;
    margin-top: -1px;
    border-radius: 0;
    z-index: 200;
    justify-content: center;
    border: 1px solid var(--color-border-color);

    &.dark {
        background-color: rgb(28, 28, 28);
    }
    &.light {
        background-color: rgb(232, 232, 232);
    }
    &.katlum {
        background-color: #4f8fba;
    }
    &.solus {
        background-color: #e9a02b;
    }
    &.silang {
        background-color: #a82bcb;
    }
    &.nozt {
        background-color: #dc4242;
    }
    &.albor {
        background-color: #5cd864;
    }
}

/* Container */
.menu-container {
  position: relative;
  display: inline-block;
}

.two-columns {
    display: flex;
    flex-grow: 1;
    flex-direction: row;
}

.section {
    flex-grow: 1;
}

/* Button */
.menu-button {
  background: var(--color-background-fore);
  color: var(--color-text);
  font-size: 1em;
border: 1px solid var(--color-border-color);
   padding: 8px 12px;
   border-radius: 0;
   cursor: pointer;
   margin-right: 15px;
}

/* Dropdown transitions */
.dropdown-enter-active, .dropdown-leave-active {
  transition: opacity 0.2s ease, transform 0.2s ease;
}
.dropdown-enter-from, .dropdown-leave-to {
  opacity: 0;
  transform: translateY(-8px);
}

/* Dropdown */
.dropdown {
 z-index: 10;
    position: absolute;
    top: 50px;
    right: 0;
    margin-right: 10px;
    max-width: calc(100vw - 32px);
    width: auto;
    min-width: 260px;
    background: var(--color-background-fore);
    border: 1px solid var(--color-border-color);
    border-radius: 0;
    padding: 8px 0;
    box-shadow: 4px -4px 0px 0px rgba(0,0,0,0.3);
}

/* Sections */
.menu-section {
  padding: 4px 0;
}

/* Items */
.menu-item {
  padding: 8px 16px;
  color: var(--color-text);
  cursor: pointer;
  display: flex;
  border-radius: 0;
  gap: 10px;
  margin: 8px;
  align-items: center;
  min-height: 36px;

  &.active {
    background: var(--color-selected);
  }
}

.menu-header {
  padding: 8px 16px;
  color: var(--color-text);
  display: flex;
  gap: 10px;
  align-items: center;
  font-weight: bold;
  justify-content: center;
}

.menu-item:hover {
  background: var(--color-hover);
}

/* Divider */
.divider {
  height: 0;
  color: var(--color-border-color);
  font-family: 'Hurmit', monospace;
  font-size: 0;
  line-height: 0;
  white-space: nowrap;
  margin: 8px 16px;

  &::before {
    content: "═══════════";
  }
}

.two-columns {
    display: flex;
    flex-grow: 1;
    flex-direction: row;
}

.section {
    flex-grow: 1;
}

.stacked {
    flex-direction: column;
}

@media (max-width: 480px) {
    .dropdown {
        top: 42px;
        padding: 6px 0;
    }

    .menu-item {
        margin: 4px;
        padding: 10px 12px;
    }

    .menu-header {
        padding: 6px 12px;
    }

    .divider {
        margin: 4px 12px;
    }
}
</style>