import { useTranslation } from 'react-i18next';
import { StyledLangButton, StyledNavbar, StyledNavBrand, StyledNavlinksWrapper } from './styles/Navbar.styled';

interface NavLink {
  title: string;
  href: string;
}

const navlinks = [
  {
    title: 'about',
    href: '#about',
  },
  {
    title: 'projects',
    href: '#projects',
  },
  {
    title: 'contact',
    href: '#contact',
  },
];

export default function Navbar() {
  const { t } = useTranslation();

  return (
    <StyledNavbar>
      <div className="navbar-content">
        <StyledNavBrand href="#">{t('navbar.title')}</StyledNavBrand>

        <Navlinks navlinks={navlinks} />
      </div>
    </StyledNavbar>
  );
}

function Navlinks(props: { navlinks: NavLink[] }) {
  const { t } = useTranslation();

  return (
    <StyledNavlinksWrapper>
      <ul>
        {props.navlinks.map((navlink) => (
          <li key={navlink.title}>
            <a href={navlink.href}>{t(`navbar.${navlink.title}`)}</a>
          </li>
        ))}
      </ul>

      <LanguageChoice />
    </StyledNavlinksWrapper>
  );
}

function LanguageChoice() {
  const { i18n } = useTranslation();

  function changeLanguage() {
    i18n.changeLanguage(i18n.language == 'pl' ? 'en' : 'pl');
  }

  return (
    <StyledLangButton aria-description="Change language" onClick={changeLanguage}>
      {i18n.language}
    </StyledLangButton>
  );
}
