// Copyright 2021 Signal Messenger, LLC
// SPDX-License-Identifier: AGPL-3.0-only

import { memo } from 'react';
import type { FunctionComponent, JSX } from 'react';

import { HEADER_CONTACT_NAME_CLASS_NAME } from './BaseConversationListItem.dom.tsx';
import type { ConversationType } from '../../state/ducks/conversations.preload.ts';
import type { BadgeType } from '../../badges/types.std.ts';
import type { LocalizerType, ThemeType } from '../../types/Util.std.ts';
import { ContactName } from '../conversation/ContactName.dom.tsx';
import { About } from '../conversation/About.dom.tsx';
import { ListTile } from '../ListTile.dom.tsx';
import { Avatar, AvatarSize } from '../Avatar.dom.tsx';

export enum ContactCheckboxDisabledReason {
  // We start the enum at 1 because the default starting value of 0 is falsy.
  AlreadyAdded = 1,
  MaximumContactsSelected,
}

export type PropsDataType = {
  badge: undefined | BadgeType;
  disabledReason?: ContactCheckboxDisabledReason;
  isChecked: boolean;
} & Pick<
  ConversationType,
  | 'about'
  | 'acceptedMessageRequest'
  | 'avatarUrl'
  | 'color'
  | 'groupId'
  | 'id'
  | 'isMe'
  | 'phoneNumber'
  | 'profileName'
  | 'title'
  | 'type'
  | 'serviceId'
>;

type PropsHousekeepingType = {
  i18n: LocalizerType;
  onClick: (
    id: string,
    disabledReason: undefined | ContactCheckboxDisabledReason
  ) => void;
  theme: ThemeType;
};

type PropsType = PropsDataType & PropsHousekeepingType;

export const ContactCheckbox: FunctionComponent<PropsType> = memo(
  function ContactCheckbox({
    about,
    avatarUrl,
    badge,
    color,
    disabledReason,
    i18n,
    id,
    isChecked,
    isMe,
    onClick,
    phoneNumber,
    profileName,
    theme,
    title,
    type,
  }) {
    const disabled = Boolean(disabledReason);

    const headerName = isMe ? (
      <ContactName
        module={HEADER_CONTACT_NAME_CLASS_NAME}
        title={i18n('icu:noteToSelf')}
        isMe={isMe}
      />
    ) : (
      <ContactName module={HEADER_CONTACT_NAME_CLASS_NAME} title={title} />
    );

    let messageText: undefined | string | JSX.Element;
    if (disabledReason === ContactCheckboxDisabledReason.AlreadyAdded) {
      messageText = i18n('icu:alreadyAMember');
    } else if (about) {
      messageText = <About className="" text={about} />;
    }

    const onClickItem = () => {
      onClick(id, disabledReason);
    };

    return (
      <ListTile.checkbox
        clickable
        disabled={disabled}
        isChecked={isChecked}
        leading={
          <Avatar
            avatarUrl={avatarUrl}
            color={color}
            conversationType={type}
            noteToSelf={isMe}
            i18n={i18n}
            phoneNumber={phoneNumber}
            profileName={profileName}
            title={title}
            size={AvatarSize.THIRTY_TWO}
            // appease the type checker.
            {...(badge ? { badge, theme } : { badge: undefined })}
          />
        }
        title={headerName}
        subtitle={isMe ? undefined : messageText}
        subtitleMaxLines={1}
        onClick={onClickItem}
      />
    );
  }
);
