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

import React, {PureComponent} from 'react';
import PropTypes from 'prop-types';
import {
    StyleSheet,
    Text,
    TouchableOpacity,
    View
} from 'react-native';

import FormattedText from 'app/components/formatted_text';
import FormattedTime from 'app/components/formatted_time';
import ReplyIcon from 'app/components/reply_icon';
import {emptyFunction} from 'app/utils/general';
import {changeOpacity, makeStyleSheetFromTheme} from 'app/utils/theme';

const BOT_NAME = 'BOT';

export default class PostHeader extends PureComponent {
    static propTypes = {
        commentCount: PropTypes.number,
        commentedOnDisplayName: PropTypes.string,
        createAt: PropTypes.number.isRequired,
        displayName: PropTypes.string.isRequired,
        enablePostUsernameOverride: PropTypes.bool,
        fromWebHook: PropTypes.bool,
        isPendingOrFailedPost: PropTypes.bool,
        isSearchResult: PropTypes.bool,
        shouldRenderReplyButton: PropTypes.bool,
        isSystemMessage: PropTypes.bool,
        onPress: PropTypes.func,
        onViewUserProfile: PropTypes.func,
        overrideUsername: PropTypes.string,
        renderReplies: PropTypes.bool,
        theme: PropTypes.object.isRequired
    };

    static defaultProps = {
        commentCount: 0,
        onPress: emptyFunction,
        onViewUserProfile: emptyFunction
    };

    getDisplayName = (style) => {
        const {
            enablePostUsernameOverride,
            fromWebHook,
            isSystemMessage,
            onViewUserProfile,
            overrideUsername
        } = this.props;

        if (isSystemMessage) {
            return (
                <FormattedText
                    id='post_info.system'
                    defaultMessage='System'
                    style={style.displayName}
                />
            );
        } else if (fromWebHook) {
            let name = this.props.displayName;
            if (overrideUsername && enablePostUsernameOverride) {
                name = overrideUsername;
            }

            return (
                <View style={style.botContainer}>
                    <Text style={style.displayName}>
                        {name}
                    </Text>
                    <Text style={style.bot}>
                        {BOT_NAME}
                    </Text>
                </View>
            );
        } else if (this.props.displayName) {
            return (
                <TouchableOpacity onPress={onViewUserProfile}>
                    <Text style={style.displayName}>
                        {this.props.displayName}
                    </Text>
                </TouchableOpacity>
            );
        }

        return (
            <FormattedText
                id='channel_loader.someone'
                defaultMessage='Someone'
                style={style.displayName}
            />
        );
    };

    renderCommentedOnMessage = (style) => {
        if (!this.props.renderReplies || !this.props.commentedOnDisplayName) {
            return null;
        }

        const displayName = this.props.commentedOnDisplayName;

        let name;
        if (displayName) {
            name = displayName;
        } else {
            name = (
                <FormattedText
                    id='channel_loader.someone'
                    defaultMessage='Someone'
                />
            );
        }

        let apostrophe;
        if (displayName && displayName.slice(-1) === 's') {
            apostrophe = '\'';
        } else {
            apostrophe = '\'s';
        }

        return (
            <FormattedText
                id='post_body.commentedOn'
                defaultMessage='Commented on {name}{apostrophe} message: '
                values={{
                    name,
                    apostrophe
                }}
                style={style.commentedOn}
            />
        );
    };

    render() {
        const {
            commentedOnDisplayName,
            commentCount,
            createAt,
            isPendingOrFailedPost,
            isSearchResult,
            onPress,
            renderReplies,
            shouldRenderReplyButton,
            theme
        } = this.props;
        const style = getStyleSheet(theme);
        const showReply = shouldRenderReplyButton || (!commentedOnDisplayName && commentCount > 0 && renderReplies);

        return (
            <View>
                <View style={[style.postInfoContainer, (isPendingOrFailedPost && style.pendingPost)]}>
                    <View style={{flexDirection: 'row', flex: 1}}>
                        {this.getDisplayName(style)}
                        <View style={style.timeContainer}>
                            <Text style={style.time}>
                                <FormattedTime value={createAt}/>
                            </Text>
                        </View>
                    </View>
                    {showReply &&
                    <TouchableOpacity
                        onPress={onPress}
                        style={style.replyIconContainer}
                    >
                        <ReplyIcon
                            height={15}
                            width={15}
                            color={theme.linkColor}
                        />
                        {!isSearchResult &&
                        <Text style={style.replyText}>{commentCount}</Text>
                        }
                    </TouchableOpacity>
                    }
                </View>
                {commentedOnDisplayName !== '' &&
                <View>
                    {this.renderCommentedOnMessage(style)}
                </View>
                }
            </View>
        );
    }
}

const getStyleSheet = makeStyleSheetFromTheme((theme) => {
    return StyleSheet.create({
        commentedOn: {
            color: changeOpacity(theme.centerChannelColor, 0.65),
            marginBottom: 3,
            lineHeight: 21
        },
        postInfoContainer: {
            alignItems: 'center',
            flexDirection: 'row',
            marginTop: 10
        },
        pendingPost: {
            opacity: 0.5
        },
        timeContainer: {
            justifyContent: 'center'
        },
        time: {
            color: theme.centerChannelColor,
            fontSize: 13,
            marginLeft: 5,
            marginBottom: 1,
            opacity: 0.5
        },
        replyIconContainer: {
            flexDirection: 'row',
            alignItems: 'center',
            justifyContent: 'center'
        },
        replyText: {
            fontSize: 15,
            marginLeft: 3,
            color: theme.linkColor
        },
        botContainer: {
            flexDirection: 'row'
        },
        bot: {
            alignSelf: 'center',
            backgroundColor: changeOpacity(theme.centerChannelColor, 0.15),
            borderRadius: 2,
            color: theme.centerChannelColor,
            fontSize: 10,
            fontWeight: '600',
            marginRight: 5,
            paddingVertical: 2,
            paddingHorizontal: 4
        },
        displayName: {
            color: theme.centerChannelColor,
            fontSize: 15,
            fontWeight: '600',
            marginRight: 5,
            marginBottom: 3
        }
    });
});
