import React from 'react';
import { StyleSheet, Text } from 'react-native';

import { TActionSheetOptionsItem, useActionSheet } from '../../containers/ActionSheet';
import { CustomIcon } from '../../containers/CustomIcon';
import * as List from '../../containers/List';
import I18n from '../../i18n';
import { useTheme } from '../../theme';
import sharedStyles from '../Styles';
import { MediaDownloadOption } from '../../lib/constants';

const styles = StyleSheet.create({
	title: { ...sharedStyles.textRegular, fontSize: 16 }
});

const OPTIONS: { label: string; value: MediaDownloadOption }[] = [
	{
		label: 'Wi_Fi_and_mobile_data',
		value: MediaDownloadOption.WIFI_MOBILE_DATA
	},
	{
		label: 'Wi_Fi',
		value: MediaDownloadOption.WIFI
	},
	{
		label: 'Never',
		value: MediaDownloadOption.NEVER
	}
];

interface IBaseParams {
	value: string;
	onChangeValue: (value: MediaDownloadOption) => void;
}

const ListPicker = ({
	value,
	title,
	testID,
	onChangeValue
}: {
	title: string;
	testID: string;
} & IBaseParams) => {
	const { showActionSheet, hideActionSheet } = useActionSheet();
	const { colors } = useTheme();
	const option = OPTIONS.find(option => option.value === value);

	const getOptions = (): TActionSheetOptionsItem[] =>
		OPTIONS.map(i => ({
			title: I18n.t(i.label, { defaultValue: i.label }),
			onPress: () => {
				hideActionSheet();
				onChangeValue(i.value);
			},
			right: option?.value === i.value ? () => <CustomIcon name={'check'} size={20} color={colors.tintActive} /> : undefined
		}));

	return (
		<List.Item
			title={title}
			testID={testID}
			onPress={() => showActionSheet({ options: getOptions() })}
			right={() => (
				<Text style={[styles.title, { color: colors.actionTintColor }]}>
					{option?.label ? I18n.t(option?.label, { defaultValue: option?.label }) : option?.label}
				</Text>
			)}
		/>
	);
};

export default ListPicker;
