// @flow
import React, { useState, useCallback } from 'react';
import styled from 'styled-components';

import { getPaletteColor } from '../../../mixins/branding';
import { UnstyledButton } from '../unstyled-button';

import {
  getBgColor,
  getColor,
  getTabButtonFontSize,
  getAnimation,
  getBoxShadow,
} from './tab-set.css-rules';
import type { TabSetProps } from './tab-set.proptypes';
import { bpMedium } from '../../../breakpoints';

const TAB_BUTTON_WIDTH_MD = 190;
const TAB_BUTTON_WIDTH_SM = 120;

export const TabButton = styled(UnstyledButton)`
  margin: 0px;
  position: relative;
  min-width: ${TAB_BUTTON_WIDTH_MD}px;
  border-radius: 25px;
  color: ${getColor};
  font-weight: 900;
  font-size: ${getTabButtonFontSize};
  text-align: center;
  z-index: 1;

  @media (max-width: ${bpMedium}) {
    min-width: ${TAB_BUTTON_WIDTH_SM}px;
  }
`;

const Pill = styled.div`
  position: absolute;
  min-width: ${TAB_BUTTON_WIDTH_MD}px;
  height: 40px;
  border-radius: 25px;
  background-color: ${getBgColor};
  transform: translateX(${(props) => `${props.currentIndex * 100}%`});
  ${getAnimation}

  @media screen and (-ms-high-contrast: active), (-ms-high-contrast: none) {
    left: ${(props) => `${props.currentIndex * TAB_BUTTON_WIDTH_MD}px`};
    transform: none;
  }
  @media (max-width: ${bpMedium}) {
    min-width: ${TAB_BUTTON_WIDTH_SM}px;
  }
`;

export const TabList = styled.div`
  display: inline-flex;
  height: 40px;
  margin: 0;
  padding: 0;
  background-color: ${getPaletteColor('greyscale-light')};
  border-radius: 25px;
  box-shadow: ${getBoxShadow};
`;

const useTabSetState = ({ selectedTabIndex, onSelect }) => {
  const [tabIndex, setTabIndex] = useState(selectedTabIndex);
  const [prevTabIndex, setPrevTabIndex] = useState(undefined);

  const handleTabSelect = useCallback(
    (index) => {
      setPrevTabIndex(tabIndex);
      setTabIndex(index);
    },
    [tabIndex],
  );

  const handleTabClick = useCallback(
    (index) => (_e) => {
      if (typeof onSelect === 'function') {
        onSelect(index);
      }
      handleTabSelect(index);
    },
    [handleTabSelect, onSelect],
  );

  return { tabIndex, prevTabIndex, handleTabClick };
};

export const TabSet = ({
  selectedTabIndex = 0,
  tabs,
  onSelect,
}: TabSetProps) => {
  const { tabIndex, prevTabIndex, handleTabClick } = useTabSetState({
    selectedTabIndex,
    onSelect,
  });

  return (
    <>
      <TabList role="tablist">
        {tabs.map((tab, index) => (
          <React.Fragment key={tab.id}>
            <TabButton
              role="tab"
              key={tab.id}
              onClick={handleTabClick(index)}
              selected={tabIndex === index}
              insetFocus
            >
              {tab.label}
            </TabButton>
            {tabIndex === index && (
              <Pill
                selected={tabIndex === index}
                currentIndex={index}
                prevTabIndex={prevTabIndex}
              />
            )}
          </React.Fragment>
        ))}
      </TabList>
      <div role="tabpanel">{tabs[tabIndex].content}</div>
    </>
  );
};

TabSet.displayName = 'TabSet';
