// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
// See LICENSE.txt for license information.

import React from 'react';
import {useIntl} from 'react-intl';
import {Platform} from 'react-native';

import OptionItem from '@components/option_item';
import {NotificationLevel, Screens} from '@constants';
import {useTheme} from '@context/theme';
import {t} from '@i18n';
import {goToScreen} from '@screens/navigation';
import {preventDoubleTap} from '@utils/tap';
import {changeOpacity} from '@utils/theme';

type Props = {
    channelId: string;
    displayName: string;
    notifyLevel: NotificationLevel;
}

const NotificationPreference = ({channelId, displayName, notifyLevel}: Props) => {
    const {formatMessage} = useIntl();
    const theme = useTheme();

    const title = formatMessage({id: 'channel_info.mobile_notifications', defaultMessage: 'Mobile Notifications'});

    const goToChannelNotificationPreference = preventDoubleTap(() => {
        const options = {
            topBar: {
                title: {
                    text: title,
                },
                subtitle: {
                    color: changeOpacity(theme.sidebarHeaderTextColor, 0.72),
                    text: displayName,
                },
            },
        };
        goToScreen(Screens.CHANNEL_NOTIFICATION_PREFERENCE, title, {channelId}, options);
    });

    const notificationLevelToText = () => {
        let id = '';
        let defaultMessage = '';
        switch (notifyLevel) {
            case NotificationLevel.ALL: {
                id = t('channel_info.notification.all');
                defaultMessage = 'All';
                break;
            }
            case NotificationLevel.MENTION: {
                id = t('channel_info.notification.mention');
                defaultMessage = 'Mentions';
                break;
            }
            case NotificationLevel.NONE: {
                id = t('channel_info.notification.none');
                defaultMessage = 'Never';
                break;
            }
            default:
                id = t('channel_info.notification.default');
                defaultMessage = 'Default';
                break;
        }

        return formatMessage({id, defaultMessage});
    };

    return (
        <OptionItem
            action={goToChannelNotificationPreference}
            label={title}
            icon='cellphone'
            type={Platform.select({ios: 'arrow', default: 'default'})}
            info={notificationLevelToText()}
            testID='channel_info.options.notification_preference.option'
        />
    );
};

export default NotificationPreference;
