import { IoCheckmarkDoneSharp } from 'react-icons/io5';

import Avatar from '../../ui/Avatar';
import Badge from '../../ui/Badge';
import type { DialogRowPresenterProps } from '../types';

const WhatsappDialogRow = ({
  avatarId,
  title,
  preview,
  timeLabel,
  selected,
  closed,
  switchBlocked,
  onClick,
  showReadCheck,
  unread,
}: DialogRowPresenterProps) => (
  <button
    type="button"
    disabled={switchBlocked}
    onClick={onClick}
    className={`
      relative w-full flex items-start gap-3 px-3 py-2.5 text-left
      border-b border-[var(--tg-divider)] tg-press transition-colors duration-100
      ${selected ? 'bg-[var(--tg-bg-selected)]' : 'hover:bg-[var(--tg-bg-secondary)]'}
      ${closed && !selected ? 'opacity-65' : ''}
      ${switchBlocked ? 'opacity-50 cursor-not-allowed' : ''}
    `}
  >
    <Avatar id={avatarId} size={49} className="shrink-0 mt-0.5" />

    <div className="flex-1 min-w-0 flex flex-col gap-0.5">
      <div className="flex items-baseline justify-between gap-2 min-w-0">
        <span
          className={`truncate text-[16px] font-medium ${
            selected
              ? 'text-[var(--tg-bg-selected-text)]'
              : 'text-[var(--tg-text-primary)]'
          }`}
        >
          {title}
        </span>
        <span
          className={`shrink-0 text-[12px] tabular-nums ${
            selected
              ? 'text-[var(--tg-bg-selected-text)] opacity-80'
              : 'text-[var(--tg-accent)]'
          }`}
        >
          {timeLabel}
        </span>
      </div>
      <div className="flex items-center gap-1.5 min-w-0">
        {showReadCheck && (
          <IoCheckmarkDoneSharp
            size={15}
            className={
              selected
                ? 'text-[var(--tg-bg-selected-text)] shrink-0 opacity-90'
                : 'text-[var(--tg-accent)] shrink-0'
            }
          />
        )}
        <span
          className={`flex-1 min-w-0 truncate text-[14px] leading-tight ${
            selected
              ? 'text-[var(--tg-bg-selected-text)] opacity-85'
              : 'text-[var(--tg-text-secondary)]'
          }`}
        >
          {preview}
        </span>
        {unread > 0 && !selected && (
          <Badge variant="accent" className="shrink-0 scale-95">
            {unread}
          </Badge>
        )}
        {unread > 0 && selected && (
          <span
            className="shrink-0 inline-flex min-w-[20px] h-5 px-1.5 items-center justify-center rounded-full text-[11px] font-semibold bg-[var(--tg-accent)] text-white"
          >
            {unread}
          </span>
        )}
      </div>
    </div>
  </button>
);

export default WhatsappDialogRow;
