import * as React from 'react'
import styled from 'styled-components'

const CaptureArea = styled('label')({
  display: 'inline-block',
  cursor: 'pointer',
  'fieldset[disabled] &': {
    cursor: 'not-allowed',
  },
  '::after': {
    content: '""',
  },
})

CaptureArea.displayName = 'label'

const Input = styled('input')({
  position: 'absolute',
  left: -999999,
  width: 1,
  height: 1,
})

Input.displayName = 'input'

const IndicatorWrapper = styled('span')({
  borderColor: 'var(--color-accent, blue)',
  boxSizing: 'border-box',
  backgroundColor: 'transparent',
  borderRadius: '0.75rem',
  position: 'relative',
  width: '1.5rem',
  height: '1.5rem',
  minWidth: '1.5rem',
  maxWidth: '1.5rem',
  display: 'inline-flex',
  verticalAlign: 'top',
  justifyContent: 'center',
  alignItems: 'center',
  cursor: 'pointer',
  transitionProperty: 'border-color',
  [`${Input}:focus ~ &`]: {
    '--color-accent': 'var(--color-active, Highlight)',
  },
  [`${Input}:disabled ~ &`]: {
    opacity: 0.5,
  },
})

const Border = styled('span')({
  borderColor: 'var(--color-accent, blue)',
  boxSizing: 'border-box',
  display: 'inline-block',
  borderWidth: '0.125rem',
  borderStyle: 'solid',
  position: 'absolute',
  top: 0,
  left: 0,
  width: '100%',
  height: '100%',
  borderRadius: 'inherit',
  transitionProperty: 'border-color',
  '::before': {
    position: 'absolute',
    top: 0,
    left: 0,
    width: '100%',
    height: '100%',
    content: "''",
    borderRadius: '0.75rem',
    opacity: 0.5,
    pointerEvents: 'none',
  },
  [`${CaptureArea}:focus-within &::before`]: {
    boxShadow: '0 0 0 0.375rem var(--color-accent, blue)',
  },
})

const Indicator = styled('span')({
  backgroundColor: 'var(--color-accent, blue)',
  color: 'var(--color-bg, white)',
  width: 0,
  height: 0,
  opacity: 0,
  boxSizing: 'border-box',
  display: 'inline-grid',
  placeContent: 'center',
  borderRadius: '50%',
  transitionProperty: 'background-color, color, width, height, opacity',
  [`${Input}:checked + ${IndicatorWrapper} &`]: {
    width: `${(100 * 2) / 3}%`,
    height: `${(100 * 2) / 3}%`,
    opacity: 1,
  },
})

const Label = styled('span')({
  color: 'var(--color-accent, blue)',
  width: 'calc(100% - 2.5rem)',
  fontFamily: 'var(--font-family-base, sans-serif)',
  [`${Input}:focus ~ &`]: {
    '--color-accent': 'var(--color-active, Highlight)',
  },
  [`${Input}:disabled ~ &`]: {
    opacity: 0.5,
  },
})

export type Props = Omit<React.HTMLProps<HTMLInputElement>, 'as' | 'className' | 'style'> & {
  /**
   * Short textual description indicating the nature of the component's value.
   */
  label?: React.ReactNode,
  /**
   * Should the label not be visible?
   */
  hiddenLabel?: boolean,
}

/**
 * Component for values which are to be selected from a few list of options.
 * @see {@link Checkbox} for a similar component on selecting values among very few choices.
 * @see {@link Select} for a similar component suitable for selecting more values.
 * @type {React.ComponentType<{readonly label?: string, readonly name?: string} & React.ClassAttributes<unknown>>}
 */
export const RadioButton = React.forwardRef<HTMLInputElement, Props>(({
  label = '',
  disabled = false,
  hiddenLabel = false,
  ...etcProps
}, ref) => (
  <CaptureArea
    style={{
      cursor: disabled ? 'not-allowed' : undefined,
    }}
  >
    <Input
      {...etcProps}
      ref={ref}
      type="radio"
      aria-label={label}
      disabled={disabled}
    />
    <IndicatorWrapper>
      <Border />
      <Indicator />
    </IndicatorWrapper>
    {
      typeof label! !== 'undefined'
      && label !== null
      && !hiddenLabel
      && (
        <>
          {' '}
          <Label>{label}</Label>
        </>
      )
    }
  </CaptureArea>
))

RadioButton.displayName = 'RadioButton'
