import React, { FC } from 'react';
import styled, { css } from 'styled-components';
import { ifProp } from 'styled-tools';
import { Flex } from 'reflexbox/styled-components';

interface Props {
  changePeriod: any; // TODO: types
  period: string;
  total: number;
}

const Wrapper = styled(Flex).attrs({
  flex: '1 1 auto',
  justifyContent: 'center',
  alignItems: 'center',
  py: [16, 16, 25],
  px: 32,
})`
  background-color: #f1f1f1;
  border-top-left-radius: 12px;
  border-top-right-radius: 12px;
`;

const TotalText = styled.p`
  margin: 0;
  padding: 0;

  span {
    font-weight: bold;
    border-bottom: 1px dotted #999;
  }

  @media only screen and (max-width: 768px) {
    font-size: 13px;
  }
`;

const Button = styled.button<{ active: boolean }>`
  display: flex;
  padding: 6px 12px;
  margin: 0 4px;
  border: none;
  font-size: 12px;
  border-radius: 4px;
  box-shadow: 0 0px 10px rgba(100, 100, 100, 0.1);
  background-color: white;
  cursor: pointer;
  transition: all 0.2s ease-out;
  box-sizing: border-box;

  :last-child {
    margin-right: 0;
  }

  ${ifProp(
    { active: false },
    css`
      border: 1px solid #ddd;
      background-color: #f5f5f5;
      box-shadow: 0 2px 6px rgba(150, 150, 150, 0.2);

      :hover {
        border-color: 1px solid #ccc;
        background-color: white;
      }
    `
  )}

  @media only screen and (max-width: 768px) {
    padding: 4px 8px;
    margin: 0 2px;
    font-size: 11px;
  }
`;

const StatsHead: FC<Props> = ({ changePeriod, period, total }) => {
  const buttonWithPeriod = (periodText, text) => (
    <Button
      active={period === periodText}
      data-period={periodText}
      onClick={changePeriod}
    >
      {text}
    </Button>
  );
  return (
    <Wrapper>
      <TotalText>
        Total clicks: <span>{total}</span>
      </TotalText>
      <Flex>
        {buttonWithPeriod('allTime', 'All Time')}
        {buttonWithPeriod('lastMonth', 'Month')}
        {buttonWithPeriod('lastWeek', 'Week')}
        {buttonWithPeriod('lastDay', 'Day')}
      </Flex>
    </Wrapper>
  );
};

export default StatsHead;
