'use client'

import { useRouter } from 'next/navigation'
import { usePathname } from 'next/navigation'
import { useTranslation } from 'react-i18next'
import i18nConfig from '@/i18nConfig'
import { Box, FormControl, MenuItem, Select, SelectChangeEvent } from '@mui/material'

export default function LanguageSelector() {
  const { t, i18n } = useTranslation()
  const currentLocale = i18n.language
  const languages = ['en', 'de']
  const router = useRouter()
  const currentPathname = usePathname()

  const handleChange = async (e: SelectChangeEvent) => {
    const newLocale = e.target.value

    // set cookie for next-i18n-router
    const days = 30
    const date = new Date()
    date.setTime(date.getTime() + days * 24 * 60 * 60 * 1000)
    const expires = date.toUTCString()
    document.cookie = `NEXT_LOCALE=${newLocale};expires=${expires};path=/`
    
    await i18n.changeLanguage(newLocale)

    // redirect to the new locale path
    if (currentLocale === i18nConfig.defaultLocale && !i18nConfig.prefixDefault) {
      router.replace('/' + newLocale + currentPathname)
    } else {
      router.replace(currentPathname.replace(`/${currentLocale}`, `/${newLocale}`))
    }
    router.refresh()
  }

  return (
    <Box>
      <FormControl>
        <Select
          aria-label={'language selector'}
          aria-haspopup={'true'}
          value={i18n.language ?? i18nConfig.defaultLocale}
          onChange={handleChange}
          variant={'standard'}
        >
          {languages.map((lang: string) => (
            <MenuItem key={lang} value={lang}>
              {t('lang_' + lang)}
            </MenuItem>
          ))}
        </Select>
      </FormControl>
    </Box>
  )
}
