import Link from 'next/link';
import * as React from 'react';
import { css } from 'react-emotion';

import * as Constants from '~/common/constants';
import BrandLogo from '~/components/icons/BrandLogo';
import DismissIcon from '~/components/icons/DismissIcon';
import MenuIcon from '~/components/icons/Menu';
import AlgoliaSearch from '~/components/plugins/AlgoliaSearch';

const STYLES_LOGO = css`
  display: flex;
`;

const STYLES_LEFT = css`
  flex-shrink: 0;
  padding-right: 24px;
  display: flex;
  align-items: center;
  justify-content: flex-end;
`;

const STYLES_RIGHT = css`
  min-width: 5%;
  height: 100%;
  width: 100%;
  display: flex;
  align-items: center;
  justify-content: flex-end;
`;

const STYLES_LOGO_CONTAINER = css`
  display: flex;
  flex-direction: row;
  align-items: center;
  padding-top: 8px;
`;

const STYLES_NAV = css`
  display: flex;
  align-items: center;
  justify-content: space-between;
  height: 58px;
  width: 100%;
  padding: 0 24px 0 24px;

  @media screen and (max-width: ${Constants.breakpoints.mobile}) {
    padding: 0 16px 0 16px;
  }
`;

const HIDDEN_ON_MOBILE = css`
  @media screen and (max-width: ${Constants.breakpoints.mobile}) {
    display: none;
  }
`;

const SECTION_LINKS_WRAPPER = css`
  @media screen and (min-width: ${Constants.breakpoints.mobile}) {
    padding-left: 10px;
    margin-left: 15px;
    border-left-width: 1px;
    border-left-color: #eee;
    border-left-style: solid;
  }

  @media screen and (max-width: ${Constants.breakpoints.mobile}) {
    margin-left: 15px;
  }
`;

const STYLES_TITLE_TEXT = css`
  white-space: nowrap;
  padding: 0 0 0 8px;
  font-size: 1.3rem;
  display: flex;
  padding-bottom: 2px;
  font-family: ${Constants.fonts.demi};

  @media screen and (max-width: ${Constants.breakpoints.mobile}) {
    display: none;
  }
`;

const STYLES_MENU_BUTTON = css`
  cursor: pointer;
  height: 100%;
  align-items: center;
  justify-content: center;
  padding-left: 24px;
  margin-left: 16px;
  border-left: 1px solid ${Constants.colors.border};
  text-decoration: none;
  color: ${Constants.colors.black};

  :visited {
    color: ${Constants.colors.black};
  }

  :hover {
    color: ${Constants.colors.expo};
  }

  @media screen and (max-width: ${Constants.breakpoints.mobile}) {
    padding-left: 16px;
  }
`;

const STYLES_MENU_BUTTON_IS_MOBILE = css`
  display: none;

  @media screen and (max-width: ${Constants.breakpoints.mobile}) {
    display: flex;
  }
`;

const STYLES_MENU_BUTTON_VISIBLE = css`
  display: flex;
`;

const SECTION_LINK_CONTAINER = css`
  display: flex;
`;

const SECTION_LINK = css`
  text-decoration: none;
  font-weight: 900;
  font-family: expo-brand-demi, sans-serif;
  :hover {
    opacity: 0.5;
  }
`;

const SECTION_LINK_ACTIVE = css`
  text-decoration: underline;
`;

const SECTION_LINK_INACTIVE = css`
  color: #000 !important;
`;

function SectionContainer({ spaceBetween = 0, spaceAround = 0, children, style, className }) {
  return (
    <div
      className={className}
      style={{ display: 'flex', paddingLeft: spaceAround, paddingRight: spaceAround, ...style }}>
      {children.map((child, i) => (
        <div key={i.toString()} style={{ paddingLeft: i === 0 ? 0 : spaceBetween }}>
          {child}
        </div>
      ))}
    </div>
  );
}

export default class DocumentationHeader extends React.PureComponent {
  render() {
    return (
      <header className={STYLES_NAV}>
        <div className={STYLES_LEFT}>
          <div className={STYLES_LOGO_CONTAINER}>
            <Link href="/">
              <a className={STYLES_LOGO}>
                <BrandLogo />
              </a>
            </Link>

            <h1 className={STYLES_TITLE_TEXT}>Expo</h1>

            {this._renderSectionLinks()}
          </div>
        </div>
        <div className={STYLES_RIGHT}>
          {!this.props.isAlogliaSearchHidden && (
            <AlgoliaSearch router={this.props.router} version={this.props.version} />
          )}

          {!this.props.isMenuActive && (
            <span
              className={`${STYLES_MENU_BUTTON} ${STYLES_MENU_BUTTON_IS_MOBILE}`}
              onClick={this.props.onShowMenu}>
              <MenuIcon />
            </span>
          )}

          {this.props.isMenuActive && (
            <span
              className={`${STYLES_MENU_BUTTON} ${STYLES_MENU_BUTTON_VISIBLE}`}
              onClick={this.props.onHideMenu}>
              <DismissIcon />
            </span>
          )}
        </div>
      </header>
    );
  }

  _renderSectionLinks = () => {
    return (
      <div className={SECTION_LINKS_WRAPPER}>
        <SectionContainer spaceBetween={15}>
          <Link href="/">
            <a
              className={`${SECTION_LINK} ${
                this.props.activeSection === 'starting'
                  ? SECTION_LINK_ACTIVE
                  : SECTION_LINK_INACTIVE
              } `}>
              Get Started
            </a>
          </Link>
          <Link href="/guides">
            <a
              className={`${SECTION_LINK}
                    ${
                      this.props.activeSection === 'general'
                        ? SECTION_LINK_ACTIVE
                        : SECTION_LINK_INACTIVE
                    }
                  `}>
              Guides
            </a>
          </Link>
          <Link href="/versions/latest/">
            <a
              className={`${SECTION_LINK}
                    ${
                      this.props.activeSection === 'reference'
                        ? SECTION_LINK_ACTIVE
                        : SECTION_LINK_INACTIVE
                    }
                  `}>
              <span className={HIDDEN_ON_MOBILE}>API</span> Reference
            </a>
          </Link>
        </SectionContainer>
      </div>
    );
  };
}

@@ -409,24 +409,107 @@
 : flex;%0A%60;%0A%0A
+const STYLES_UNSTYLED_ANCHOR = css%60%0A  color: inherit;%0A  text-decoration: none;%0A%60;%0A%0A
 const STYLES
@@ -1802,16 +1802,35 @@
 s.demi%7D;
+%0A  cursor: pointer;
 %0A%0A  @med
@@ -3743,16 +3743,101 @@
 /Link%3E%0A%0A
+            %3CLink href=%22/%22%3E%0A              %3Ca className=%7BSTYLES_UNSTYLED_ANCHOR%7D%3E%0A    
         
@@ -3883,16 +3883,55 @@
 xpo%3C/h1%3E
+%0A              %3C/a%3E%0A            %3C/Link%3E
 %0A%0A      

fix textual logo link (#8312)