import React from 'react';
import { dequal } from 'dequal';

import I18n from '../../i18n';
import styles from './styles';
import Markdown from '../../containers/markdown';
import { themes } from '../../constants/colors';
import { E2E_MESSAGE_TYPE, E2E_STATUS } from '../../lib/encryption/constants';

interface ILastMessage {
	theme: string;
	lastMessage: {
		u: any;
		pinned: boolean;
		t: string;
		attachments: any;
		msg: string;
		e2e: string;

	};
	type: string;
	showLastMessage: boolean;
	username: string;
	useRealName: boolean;
	alert: boolean;
}

const formatMsg = ({
	lastMessage, type, showLastMessage, username, useRealName
}: Partial<ILastMessage>) => {
	if (!showLastMessage) {
		return '';
	}
	if (!lastMessage || !lastMessage.u || lastMessage.pinned) {
		return I18n.t('No_Message');
	}
	if (lastMessage.t === 'jitsi_call_started') {
		const { u } = lastMessage;
		return I18n.t('Started_call', { userBy: u.username });
	}

	let prefix = '';
	const isLastMessageSentByMe = lastMessage.u.username === username;

	if (!lastMessage.msg && lastMessage.attachments && Object.keys(lastMessage.attachments).length) {
		const user = isLastMessageSentByMe ? I18n.t('You') : lastMessage.u.username;
		return I18n.t('User_sent_an_attachment', { user });
	}

	// Encrypted message pending decrypt
	if (lastMessage.t === E2E_MESSAGE_TYPE && lastMessage.e2e !== E2E_STATUS.DONE) {
		lastMessage.msg = I18n.t('Encrypted_message');
	}

	if (isLastMessageSentByMe) {
		prefix = I18n.t('You_colon');
	}	else if (type !== 'd') {
		const { u: { name } } = lastMessage;
		prefix = `${ useRealName ? name : lastMessage.u.username }: `;
	}

	return `${ prefix }${ lastMessage.msg }`;
};

const arePropsEqual = (oldProps: any, newProps: any) => dequal(oldProps, newProps);

const LastMessage = React.memo(({
	lastMessage, type, showLastMessage, username, alert, useRealName, theme
}: ILastMessage) => (
	// @ts-ignore
	<Markdown
		msg={formatMsg({
			lastMessage, type, showLastMessage, username, useRealName
		})}
		style={[styles.markdownText, { color: alert ? themes[theme].bodyText : themes[theme].auxiliaryText }]}
		customEmojis={false}
		useRealName={useRealName}
		numberOfLines={2}
		preview
		theme={theme}
	/>
), arePropsEqual);

export default LastMessage;
