import trackAnalyticsEvent from '@/lib/trackAnalyticsEvent';
import { ash, darkLine, smoke, yellow } from '@/styles/colors';
import { BELOW_W_1024, BELOW_W_768, paddedMainContentWidth } from '@/styles/dimensions';
import styled from '@emotion/styled';
import Link from 'next/link';
import { useState } from 'react';
import { MainButton } from './Buttons';

const SERVER_URL = process.env.NEXT_PUBLIC_SERVER_URL;

const menuData: any[] = [];

export const HeaderContainer = styled.header`
  backdrop-filter: blur(5px);
  /* opacity: 0.5; */
  background-color: rgba(255, 255, 255, 0.8);
  border-bottom: 1px solid ${darkLine};

  z-index: 14;
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  height: 48px; // Using PX here because this needs to match the square corners, which are related to screen sizes that trigger important breakpoints.
  > div {
    height: 100%;
    max-width: 152rem;
    margin: 0 auto;
    display: flex;
    justify-content: space-between;
    align-items: center;
  }
  > div > div > svg {
    width: 4rem;
    height: 4rem;
  }
  @media screen and (${BELOW_W_768}) {
    background-color: white;
  }
  &:before {
    content: '';
    display: block;
    position: absolute;
    left: 0;
    right: 0;
    top: 100%;
    border-bottom: 1px solid var(--scroll-anim-border-color);
  }
`;

const HeaderLogoContainer = styled.div`
  display: flex;
  justify-content: center;

  position: absolute;
  left: 50%;
  transform: translateX(-50%);
  @media screen and (max-width: 450px) {
    display: none;
  }
`;

const HeaderLogo = styled(Link)`
  text-decoration: none;
  display: flex;
  align-items: center;
  margin-right: 3rem;
  img {
    margin-top: 0.2rem;
    height: 100%;
    width: auto;
  }
`;

const HeaderLink = styled(Link)`
  font-family: Architekt;
  font-size: max(1.5rem, 12px);
  font-weight: 900;
  color: ${smoke};
  text-decoration: none;
  margin: 0 1.7rem;
  display: inline-flex;
  align-items: center;
  height: 100%;

  @media screen and (${BELOW_W_1024}) {
    font-size: 1.2rem;
  }
  @media screen and (${BELOW_W_768}) {
    display: none;
  }
`;

const MobileMenuContainer = styled.div<{ isOpen: boolean }>`
  position: absolute;
  border-bottom: 1px solid ${darkLine};
  border-top: 1px solid ${darkLine};
  z-index: 2;
  background: white;

  top: 100%;
  left: 0;
  width: 100svw;
  display: none;

  @media screen and (${BELOW_W_768}) {
    display: ${(props) => (props.isOpen ? 'block' : 'none')};
  }
`;

const MobileHeaderLink = styled(HeaderLink)`
  display: none;
  height: 48px;
  padding: 0 20px 0 20px;
  margin: 0;
  align-items: center;

  @media screen and (${BELOW_W_768}) {
    display: flex;
  }
`;

const MenuButton = styled.div`
  display: none;
  font-family: Architekt;
  font-size: 12px;
  font-weight: 900;
  color: ${smoke};
  text-decoration: none;
  flex-grow: 1;
  width: 100%;
  padding: 0 20px;
  height: 100%;
  align-items: center;
  user-select: none;
  @media screen and (${BELOW_W_768}) {
    display: flex;
    cursor: pointer;
  }
`;

const MobileMenu = () => {
  const [isOpen, setIsOpen] = useState(false);

  return (
    <>
      <MenuButton onClick={() => setIsOpen((prev) => !prev)}>Menu</MenuButton>
      <MobileMenuContainer isOpen={isOpen}>
        {menuData.map((item) => (
          <MobileHeaderLink
            onClick={() => {
              setIsOpen(false);
              trackAnalyticsEvent({
                name: 'Landing Link Clicked',
                properties: {
                  text: item.label,
                  location: 'Header',
                },
              });
            }}
            key={item.href}
            href={item.href}
          >
            {item.label}
          </MobileHeaderLink>
        ))}
      </MobileMenuContainer>
    </>
  );
};

const HeaderContent = styled.div`
  width: ${paddedMainContentWidth};
  margin: 0 40px;
  display: flex;
  height: 100%;
  justify-content: space-between;
  border-left: 1px solid ${darkLine};
  @media screen and (${BELOW_W_768}) {
    width: 100%;
    border-left: none;
  }
`;

const HeaderLinkContainer = styled.div`
  height: 100%;
  flex: 1;
  margin-right: 14rem;
`;

const HeaderButton = styled(MainButton)<{ primary?: boolean }>`
  font-size: 1.5rem;
  font-weight: 800;
  padding: 0 20px;
  height: 100%;
  margin: 0;
  background-color: ${({ primary }) => (primary ? yellow : ash)};
  white-space: nowrap;

  @media screen and (${BELOW_W_1024}) {
    font-size: 1.2rem;
  }
`;

const Header = () => {
  return (
    <HeaderContainer>
      <HeaderContent>
        <HeaderLogoContainer>
          <HeaderLogo href="/" style={{ textDecoration: 'none' }}>
            <img src="/images/wordmark-black.svg" height={36} width={36} alt="WavTool Logo" />
          </HeaderLogo>
        </HeaderLogoContainer>
      </HeaderContent>
    </HeaderContainer>
  );
};

export default Header;
