import * as React from 'react';
import clsx from 'clsx';
import styles from './style.module.css';
import {ToggleButtonDerivedElement} from '@/categories/option/react';

export type ToggleTickBoxDerivedElement = HTMLInputElement;

export interface ToggleTickBoxProps extends Omit<React.InputHTMLAttributes<ToggleTickBoxDerivedElement>, 'type' | 'size'> {
  block?: boolean;
  subtext?: React.ReactNode;
  badge?: React.ReactNode;
  indeterminate?: boolean;
}

export const ToggleTickBox = React.forwardRef<ToggleTickBoxDerivedElement, ToggleTickBoxProps>(({
  children,
  block = false,
  id: idProp,
  className,
  subtext,
  badge,
  style,
  indeterminate = false,
  ...etcProps
}, forwardedRef) => {
  const defaultRef = React.useRef<ToggleButtonDerivedElement>(null);
  const ref = forwardedRef ?? defaultRef;
  const defaultId = React.useId();
  const id = idProp ?? defaultId;

  React.useEffect(() => {
    if (!(typeof ref === 'object' && ref)) {
      return;
    }
    const { current: element } = ref;
    if (!element) {
      return;
    }
    element.indeterminate = indeterminate;
  }, [indeterminate, ref]);

  return (
    <div
      className={clsx(
        'gap-x-4 flex-wrap',
        block && 'flex',
        !block && 'inline-flex align-center',
        className,
      )}
      style={style}
    >
      <input
        {...etcProps}
        ref={ref}
        type="checkbox"
        id={id}
        className={clsx(
          'sr-only peer/radio',
          styles['toggle-tick-box'],
        )}
      />
      <label
        htmlFor={id}
        className="peer/children order-2 cursor-pointer peer-disabled/radio:cursor-not-allowed"
      >
        <span
          data-testid="children"
        >
          {children}
        </span>
      </label>
      <label
        htmlFor={id}
        className={clsx(
          'order-1 block rounded ring-secondary/50 overflow-hidden gap-4 leading-none select-none cursor-pointer',
          'peer-focus/radio:outline-0 peer-focus/radio:ring-4 peer-focus/radio:ring-secondary/50',
          'active:ring-tertiary/50 active:ring-4',
          'peer-active/children:ring-tertiary/50 peer-active/children:ring-4 peer-active/children:text-tertiary',
          'peer-disabled/radio:opacity-50 peer-disabled/radio:cursor-not-allowed peer-disabled/radio:ring-0',
          'text-primary peer-disabled/radio:text-primary peer-focus/radio:text-secondary peer-checked/radio:text-tertiary active:text-tertiary',
        )}
      >
        <span
          className={clsx(
            'w-6 h-6 block rounded-inherit border-2 p-0.5 box-border border-current',
          )}
        >
          <svg
            className={clsx(
              'w-full h-full fill-none stroke-3 linejoin-round linecap-round stroke-current',
            )}
            viewBox="0 0 24 24"
            role="presentation"
          >
            <polyline
              points="20 6 9 17 4 12"
            />
          </svg>
          <svg
            className={clsx(
              'w-full h-full fill-none stroke-3 linejoin-round linecap-round stroke-current',
            )}
            viewBox="0 0 24 24"
            role="presentation"
          >
            <polyline
              points="20 12 4 12"
            />
          </svg>
        </span>
      </label>
      {subtext && (
        <div
          className="block w-full font-semi-expanded text-xs pl-10 order-3"
          data-testid="subtext"
        >
          {subtext}
        </div>
      )}
    </div>
  )
});

ToggleTickBox.displayName = 'ToggleTickBox';
