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

import PostViewController from './post_view_controller.jsx';
import LoadingScreen from 'components/loading_screen.jsx';

import ChannelStore from 'stores/channel_store.jsx';
import UserStore from 'stores/user_store.jsx';
import * as AsyncClient from 'utils/async_client.jsx';

import React from 'react';

const MAXIMUM_CACHED_VIEWS = 5;

export default class PostViewCache extends React.Component {
    constructor(props) {
        super(props);

        this.onChannelChange = this.onChannelChange.bind(this);

        const currentChannelId = ChannelStore.getCurrentId();
        const channel = ChannelStore.getCurrent();

        this.state = {
            currentChannelId,
            channels: channel ? [channel] : []
        };
    }

    componentDidMount() {
        ChannelStore.addChangeListener(this.onChannelChange);
    }

    componentWillUnmount() {
        if (UserStore.getCurrentUser()) {
            AsyncClient.viewChannel('');
        }
        ChannelStore.removeChangeListener(this.onChannelChange);
    }

    onChannelChange() {
        const channels = Object.assign([], this.state.channels);
        const currentChannel = ChannelStore.getCurrent();

        if (!currentChannel) {
            return;
        }

        // make sure current channel really changed
        if (currentChannel.id === this.state.currentChannelId) {
            return;
        }

        if (channels.length > MAXIMUM_CACHED_VIEWS) {
            channels.shift();
        }

        const index = channels.map((c) => c.id).indexOf(currentChannel.id);
        if (index !== -1) {
            channels.splice(index, 1);
        }

        channels.push(currentChannel);

        this.setState({
            currentChannelId: currentChannel.id,
            channels
        });
    }

    shouldComponentUpdate(nextProps) {
        return Boolean(nextProps.channelId);
    }

    render() {
        const channels = this.state.channels;
        const currentChannelId = this.state.currentChannelId;
        const valid = this.props.channelId === this.state.currentChannelId;
        const postViews = [];
        let content;

        if (valid) {
            for (let i = 0; i < channels.length; i++) {
                postViews.push(
                    <PostViewController
                        key={'postviewcontroller_' + channels[i].id}
                        channel={channels[i]}
                        active={channels[i].id === currentChannelId}
                    />
                );
            }
            content = postViews;
        } else {
            content = (
                <LoadingScreen
                    position='absolute'
                    key='loading'
                />
            );
        }

        return (
            <div id='post-list'>
                {content}
            </div>
        );
    }
}

PostViewCache.propTypes = {
    channelId: React.PropTypes.string.isRequired
};
