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

import UserTypingStore from 'stores/user_typing_store.jsx';

import {FormattedMessage} from 'react-intl';

import React from 'react';

class MsgTyping extends React.Component {
    constructor(props) {
        super(props);

        this.onTypingChange = this.onTypingChange.bind(this);
        this.updateTypingText = this.updateTypingText.bind(this);
        this.componentWillReceiveProps = this.componentWillReceiveProps.bind(this);

        this.state = {
            text: ''
        };
    }

    componentWillMount() {
        UserTypingStore.addChangeListener(this.onTypingChange);
        this.onTypingChange();
    }

    componentWillUnmount() {
        UserTypingStore.removeChangeListener(this.onTypingChange);
    }

    componentWillReceiveProps(nextProps) {
        if (this.props.channelId !== nextProps.channelId) {
            this.updateTypingText(UserTypingStore.getUsersTyping(nextProps.channelId, nextProps.parentId));
        }
    }

    onTypingChange() {
        this.updateTypingText(UserTypingStore.getUsersTyping(this.props.channelId, this.props.parentId));
    }

    updateTypingText(typingUsers) {
        if (!typingUsers) {
            return;
        }

        const users = Object.keys(typingUsers);
        let text = '';
        switch (users.length) {
        case 0:
            text = '';
            break;
        case 1:
            text = (
                <FormattedMessage
                    id='msg_typing.isTyping'
                    defaultMessage='{user} is typing...'
                    values={{
                        user: users[0]
                    }}
                />
            );
            break;
        default: {
            const last = users.pop();
            text = (
                <FormattedMessage
                    id='msg_typing.areTyping'
                    defaultMessage='{users} and {last} are typing...'
                    values={{
                        users: (users.join(', ')),
                        last: (last)
                    }}
                />
            );
            break;
        }
        }

        this.setState({text});
    }

    render() {
        return (
            <span className='msg-typing'>{this.state.text}</span>
        );
    }
}

MsgTyping.propTypes = {
    channelId: React.PropTypes.string,
    parentId: React.PropTypes.string
};

export default MsgTyping;
