// tslint:disable:react-this-binding-issue
import React, { useCallback } from 'react';
import classNames from 'classnames';
import * as MIME from '../../../ts/types/MIME';
import * as GoogleChrome from '../../../ts/util/GoogleChrome';
import { MessageBody } from './MessageBody';
import { ContactName } from './ContactName';
import { PubKey } from '../../session/types';
import { ConversationTypeEnum } from '../../models/conversation';
import { useEncryptedFileFetch } from '../../hooks/useEncryptedFileFetch';
export type QuotePropsWithoutListener = {
  attachment?: QuotedAttachmentType;
  authorPhoneNumber: string;
  authorProfileName?: string;
  authorName?: string;
  isFromMe: boolean;
  isIncoming: boolean;
  conversationType: ConversationTypeEnum;
  convoId: string;
  isPublic?: boolean;
  withContentAbove: boolean;
  text: string | null;
  referencedMessageNotFound: boolean;
};
export type QuotePropsWithListener = QuotePropsWithoutListener & {
  onClick?: (e: any) => void;
};
export interface QuotedAttachmentType {
  contentType: MIME.MIMEType;
  fileName: string;
  /** Not included in protobuf */
  isVoiceMessage: boolean;
  thumbnail?: Attachment;
}
interface Attachment {
  contentType: MIME.MIMEType;
  /** Not included in protobuf, and is loaded asynchronously */
  objectUrl?: string;
}
function validateQuote(quote: QuotePropsWithoutListener): boolean {
  if (quote.text) {
    return true;
  }
  if (quote.attachment) {
    return true;
  }
  return false;
}
function getObjectUrl(thumbnail: Attachment | undefined): string | undefined {
  if (thumbnail && thumbnail.objectUrl) {
    return thumbnail.objectUrl;
  }
  return;
}
function getTypeLabel({
  contentType,
  isVoiceMessage,
}: {
  contentType: MIME.MIMEType;
  isVoiceMessage: boolean;
}): string | undefined {
  if (GoogleChrome.isVideoTypeSupported(contentType)) {
    return window.i18n('video');
  }
  if (GoogleChrome.isImageTypeSupported(contentType)) {
    return window.i18n('photo');
  }
  if (MIME.isAudio(contentType) && isVoiceMessage) {
    return window.i18n('voiceMessage');
  }
  if (MIME.isAudio(contentType)) {
    return window.i18n('audio');
  }
  return;
}
export const QuoteIcon = (props: any) => {
  const { icon } = props;
  return (
    
  );
};
export const QuoteImage = (props: any) => {
  const { url, icon, contentType, handleImageErrorBound } = props;
  const { loading, urlToLoad } = useEncryptedFileFetch(url, contentType);
  const srcData = !loading ? urlToLoad : '';
  const onDragStart = useCallback((e: any) => {
    e.preventDefault();
    return false;
  }, []);
  const iconElement = icon ? (
    
  ) : null;
  return (
    
      
      {iconElement}
    
 
  );
};
export const QuoteGenericFile = (props: any) => {
  const { attachment, isIncoming } = props;
  if (!attachment) {
    return <>>;
  }
  const { fileName, contentType } = attachment;
  const isGenericFile =
    !GoogleChrome.isVideoTypeSupported(contentType) &&
    !GoogleChrome.isImageTypeSupported(contentType) &&
    !MIME.isAudio(contentType);
  if (!isGenericFile) {
    return <>>;
  }
  return (
    
  );
};
export const QuoteIconContainer = (props: any) => {
  const { attachment, imageBroken, handleImageErrorBound } = props;
  if (!attachment) {
    return null;
  }
  const { contentType, thumbnail } = attachment;
  const objectUrl = getObjectUrl(thumbnail);
  if (GoogleChrome.isVideoTypeSupported(contentType)) {
    return objectUrl && !imageBroken ? (
      
    ) : (
      
    );
  }
  if (GoogleChrome.isImageTypeSupported(contentType)) {
    return objectUrl && !imageBroken ? (
      
    ) : (
      
    );
  }
  if (MIME.isAudio(contentType)) {
    return ;
  }
  return null;
};
export const QuoteText = (props: any) => {
  const { text, attachment, isIncoming, conversationType, convoId } = props;
  const isGroup = conversationType === ConversationTypeEnum.GROUP;
  if (text) {
    return (
      
        
      
    );
  }
  if (!attachment) {
    return null;
  }
  const { contentType, isVoiceMessage } = attachment;
  const typeLabel = getTypeLabel({ contentType, isVoiceMessage });
  if (typeLabel) {
    return (
      
        {typeLabel}
      
    );
  }
  return null;
};
export const QuoteAuthor = (props: any) => {
  const { authorProfileName, authorPhoneNumber, authorName, isFromMe, isIncoming } = props;
  return (
    
      {isFromMe ? (
        window.i18n('you')
      ) : (
        
      )}
    
  );
};
export const QuoteReferenceWarning = (props: any) => {
  const { isIncoming, referencedMessageNotFound } = props;
  if (!referencedMessageNotFound) {
    return null;
  }
  return (
    
      
      
        {window.i18n('originalMessageNotFound')}
      
     
  );
};
export const Quote = (props: QuotePropsWithListener) => {
  const handleImageErrorBound = null;
  const { isIncoming, onClick, referencedMessageNotFound, withContentAbove } = props;
  if (!validateQuote(props)) {
    return null;
  }
  return (
    
  );
};