/**
 * Copyright 2024 Phenix Real Time Solutions, Inc. Confidential and Proprietary. All Rights Reserved.
 */
import React, {Fragment} from 'react';
import {Tooltip, Position} from 'components/tooltip';
import {Label} from 'components/label';
import {RadioGroup, RadioWrapper, RadioButtonContainer, VisibleCheckBox} from './style';

interface IRadioItems {
  label: string;
  value: string;
  tooltipMessage?: string;
  tooltipPosition?: Position;
  className?: string;
  children?: React.JSX.Element;
  disabled?: boolean;
}

interface IRadioButtonGroup {
  items: IRadioItems[];
  handleOnChange: (value: string) => void;
  currentValue: string;
}

const RadioButton = (props: {currentValue: string; value: string}) => {
  const {currentValue, value} = props;

  return (
    <RadioButtonContainer>
      <input type="radio" readOnly={true} value={value} checked={currentValue === value} />
      <VisibleCheckBox checked={currentValue === value}>
        <div />
      </VisibleCheckBox>
    </RadioButtonContainer>
  );
};

const RadioButtonGroup = (props: IRadioButtonGroup): React.JSX.Element => {
  const {items, handleOnChange, currentValue} = props;

  return (
    <RadioGroup>
      {items.map(({label, value, disabled, tooltipPosition, tooltipMessage, children, className}: IRadioItems, index: number) => (
        <RadioWrapper
          tabIndex={-1}
          onKeyPress={() => null}
          disabled={disabled}
          className="button-container"
          role="button"
          key={label + index}
          onClick={() => handleOnChange(value)}>
          <RadioButton value={value} currentValue={currentValue} />
          <Fragment>
            {tooltipMessage ? (
              <Tooltip position={tooltipPosition || Position.Top} message={tooltipMessage}>
                <Label className={className} text={label} />
              </Tooltip>
            ) : (
              <Label className={className} text={label} />
            )}
            {children}
          </Fragment>
        </RadioWrapper>
      ))}
    </RadioGroup>
  );
};

export default RadioButtonGroup;
