import React, { Component, PropTypes } from 'react';
import { NewsFeed } from 'catalyst-ui';

/* global __PAGELET_BUILDER_URL__ */

const noNews = {
  'news-publication-result': {
    'id': '',
    'portal': '',
    'news-publication-top-story': '',
    'news-required-section-data': '',
    'news-section-data': '',
    'submit-links': '',
    'section-links': {
      'link': [{
      }, {
      }],
    },
    'empty_news_message': 'No articles currently available',
    'show_ts_images': 'Y',
    'show_section_links': 'Y',
    'show_feed_links': 'Y',
    'num_articles': '3',
    'bullet_img': '/cs/ps/cache/BULLET_1.JPG',
    'new_img': '/cs/ps/cache/PS_EO_PE_NEW_1.gif',
    'pagelet_width': 'Narrow',
  },
};


export default class NewsFeedProv extends Component {
  constructor() {
    super(...arguments);

    this.fetch = this.fetch.bind(this);
  }

  componentDidMount() {
    const { newsFeed, pageletName } = this.props;
    if (!newsFeed || (newsFeed.lastAction && newsFeed.lastAction.status === 'error') || !newsFeed.data || !newsFeed.data[pageletName]) {
      this.fetch();
    }
  }

  fetch() {
    const requestParams = {
      url: __PAGELET_BUILDER_URL__,
      auth: [__USERNAME__, __PASSWORD__],
    };

    this.props.fetch(requestParams, this.props.pageletName);
  }

  render() {
    let newsFeedElement = null;
    const { newsFeed, pageletName } = this.props;

    if (newsFeed.data[pageletName]) {
      newsFeedElement = <NewsFeed isFetching={newsFeed.isFetching} ncd={newsFeed.data[pageletName]} />;
    } else {
      newsFeedElement = <NewsFeed isFetching={newsFeed.isFetching} ncd={noNews} />;
    }

    return (
      <div className="piglet">
        {newsFeedElement}
      </div>
    );
  }
}

NewsFeedProv.propTypes = {
  newsFeed: PropTypes.object.isRequired,
  fetch: PropTypes.func.isRequired,
  invalidate: PropTypes.func.isRequired,
  pageletName: PropTypes.string.isRequired,
};

export { NewsFeedProv as NewsFeedProvDumb };
