/** @jsxRuntime classic */
/** @jsx jsx */
import { FC, Fragment } from 'react';
import { useTheme, jsx, css } from '@emotion/react';
import { getIcon, Popup } from '..';
import { CourtsButton } from './chooseLayer';
import { IColors } from '../../types/theme';
import { ISpaceProps } from '../../SACalendar/types';

export const SpaceHorizontalView: FC<ISpaceProps> = ({ state }) => {
  const colors = useTheme();
  const noChildren = state.children.length === 0;

  return (
    <Fragment>
      <div css={spaceContainer}>
        <div
          style={{
            padding: '10px 12px 16px 10px',
            flexGrow: 1,
            height: '100%',
            boxSizing: 'border-box',
          }}
        >
          <div style={{ justifyContent: 'left', display: 'flex' }}>
            {getIcon('sport_tennis')}
            {getIcon('sport_vollyball', 'l')}
          </div>
          <div
            style={{
              display: 'flex',
              flexDirection: 'column',
              marginTop: '4px',
            }}
          >
            <div className="title">{state.name}</div>
            <div style={{ marginTop: '12px' }}>
              <Popup body={<div>aide</div>}>
                <CourtsButton />
              </Popup>
            </div>
          </div>
        </div>
        <div style={{ display: 'flex', flexDirection: 'column' }}>
          {noChildren ? (
            <div css={spaceName(colors, true)}>{1}</div>
          ) : (
            state.children.map((space, index) => {
              const isLast = index + 1 === state.children.length;
              return (
                <div key={index} css={spaceName(colors, isLast)}>
                  {index + 1}
                </div>
              );
            })
          )}
        </div>
      </div>
    </Fragment>
  );
};

export const spaceContainer = css`
  box-sizing: border-box;
  background: white;
  border-bottom: 1px solid #e2e7eb;
  flex-grow: 1;
  display: flex;
  // flex-direction: column;
  height: 100%;
  width: 148px;
  box-sizing: border-box;
  -moz-box-sizing: border-box;
  -webkit-box-sizing: border-box;
  .title {
    flex-grow: 1;
    // min-width: 100px;
    font-family: Montserrat;
    font-style: normal;
    font-weight: normal;
    font-size: 14px;
    line-height: 130%;
    color: #0d4774;
    max-width: max-content;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
  }
`;

export const spaceName = (colors: IColors, isLast: boolean) => css`
  font-family: Montserrat;
  font-style: normal;
  font-weight: 500;
  font-size: 12px;
  line-height: 17px;
  text-align: center;
  color: ${colors.tx_text_secondary};
  flex-basis: 100%;
  border-bottom: ${isLast ? '1px solid white' : '1px solid #e2e7eb'};
  background: #fafcfd;
  box-sizing: border-box;
  -moz-box-sizing: border-box;
  -webkit-box-sizing: border-box;
  padding: 3.5px 0;
  display: flex;
  align-items: center;
  justify-content: center;
  width: 24px;
  box-sizing: border-box;
`;
