import React, { useContext } from 'react';
import { Text, useWindowDimensions, View } from 'react-native';

import I18n from '../../i18n';
import Touchable from './Touchable';
import { CustomIcon } from '../CustomIcon';
import styles from './styles';
import Emoji from './Emoji';
import { BUTTON_HIT_SLOP } from './utils';
import { themes } from '../../lib/constants/colors';
import { type TSupportedThemes, useTheme } from '../../theme';
import MessageContext from './Context';
import { type TGetCustomEmoji } from '../../definitions/IEmoji';

interface IReaction {
	_id: string;
	emoji: string;
	usernames: string[];
}

interface IMessageReaction {
	reaction: IReaction;
	getCustomEmoji: TGetCustomEmoji;
	theme: TSupportedThemes;
}

interface IMessageReactions {
	reactions?: IReaction[];
	getCustomEmoji: TGetCustomEmoji;
}

const AddReaction = React.memo(({ theme }: { theme: TSupportedThemes }) => {
	'use memo';

	const { reactionInit } = useContext(MessageContext);
	const { fontScale } = useWindowDimensions();
	const height = 28 * fontScale;
	return (
		<Touchable
			onPress={reactionInit}
			key='message-add-reaction'
			testID='message-add-reaction'
			accessibilityRole='button'
			accessibilityLabel={I18n.t('Add_reaction')}
			style={[styles.reactionButton, { backgroundColor: themes[theme].surfaceRoom }]}
			hitSlop={BUTTON_HIT_SLOP}
			android_ripple={{ color: themes[theme].strokeLight }}>
			<View style={[styles.reactionContainer, { borderColor: themes[theme].strokeLight, height }]}>
				<CustomIcon name='reaction-add' size={20} color={themes[theme].badgeBackgroundLevel2} />
			</View>
		</Touchable>
	);
});

const Reaction = React.memo(({ reaction, getCustomEmoji, theme }: IMessageReaction) => {
	'use memo';

	const { onReactionPress, onReactionLongPress, user } = useContext(MessageContext);
	const { fontScale } = useWindowDimensions();
	const height = 28 * fontScale;
	const reacted = reaction.usernames.findIndex((item: string) => item === user.username) !== -1;
	return (
		<Touchable
			onPress={() => onReactionPress(reaction.emoji)}
			onLongPress={onReactionLongPress}
			key={reaction.emoji}
			testID={`message-reaction-${reaction.emoji}`}
			accessibilityRole='button'
			accessibilityLabel={`${reaction.emoji}, ${reaction.usernames.length}`}
			accessibilityState={{ selected: reacted }}
			style={[styles.reactionButton, { backgroundColor: reacted ? themes[theme].surfaceNeutral : themes[theme].surfaceRoom }]}
			hitSlop={BUTTON_HIT_SLOP}
			android_ripple={{ color: themes[theme].strokeLight }}>
			<View
				style={[
					styles.reactionContainer,
					{ borderColor: reacted ? themes[theme].badgeBackgroundLevel2 : themes[theme].strokeLight, height }
				]}>
				<Emoji
					content={reaction.emoji}
					standardEmojiStyle={styles.reactionEmoji}
					customEmojiStyle={styles.reactionCustomEmoji}
					getCustomEmoji={getCustomEmoji}
				/>
				<Text style={[styles.reactionCount, { color: themes[theme].badgeBackgroundLevel2 }]}>{reaction.usernames.length}</Text>
			</View>
		</Touchable>
	);
});

const Reactions = React.memo(({ reactions, getCustomEmoji }: IMessageReactions) => {
	'use memo';

	const { theme } = useTheme();

	if (!Array.isArray(reactions) || reactions.length === 0) {
		return null;
	}
	return (
		<View style={styles.reactionsContainer}>
			{reactions.map(reaction => (
				<Reaction key={reaction.emoji} reaction={reaction} getCustomEmoji={getCustomEmoji} theme={theme} />
			))}
			<AddReaction theme={theme} />
		</View>
	);
});

Reaction.displayName = 'MessageReaction';
Reactions.displayName = 'MessageReactions';
AddReaction.displayName = 'MessageAddReaction';

export default Reactions;
