import { type FC } from 'react';

import { alpha, Stack, Typography, useTheme } from '@mui/material';

import Toggle from '../Toggle';
import Tooltip from '../Tooltip';

import { type SwitcherProps } from './types';

const Switcher: FC<SwitcherProps> = props => {
  const {
    title,
    titleProps,
    description,
    descriptionProps,
    disabled = false,
    value,
    onChange,
    sx,
    Icon = null,
    disabledTooltip,
  } = props;

  const theme = useTheme();
  const colors = {
    title: disabled
      ? theme.palette.new.text.neutral.lighter
      : theme.palette.new.text.neutral.default,
    description: disabled
      ? alpha(theme.palette.new.text.neutral.lighter, 0.7)
      : theme.palette.new.text.neutral.lighter,
  };

  return (
    <Stack
      sx={{
        flexDirection: 'row',
        width: '100%',
        justifyContent: 'space-between',
        alignItems: 'center',
        gap: 2,
        ...sx,
      }}
    >
      {(title || description) && (
        <Stack>
          {title && (
            <Stack
              sx={{ flexDirection: 'row', alignItems: 'center', gap: 1.5 }}
            >
              <Typography
                variant="globalS"
                color={colors.title}
                {...titleProps}
              >
                {title}
              </Typography>
              {Icon}
            </Stack>
          )}
          {description && (
            <Typography
              variant="globalXS"
              color={colors.description}
              {...descriptionProps}
            >
              {description}
            </Typography>
          )}
        </Stack>
      )}
      <Stack sx={{ justifyContent: 'center' }}>
        <Tooltip
          disableTooltip={!disabledTooltip}
          {...disabledTooltip}
        >
          <span>
            <Toggle
              disabled={disabled}
              checked={!!value}
              onChange={onChange}
            />
          </span>
        </Tooltip>
      </Stack>
    </Stack>
  );
};

export type { SwitcherProps };

export default Switcher;
