import PropTypes from 'prop-types';
import React, { Component } from 'react';
import Card from 'Components/Card';
import FieldSet from 'Components/FieldSet';
import Icon from 'Components/Icon';
import PageSectionContent from 'Components/Page/PageSectionContent';
import { icons } from 'Helpers/Props';
import translate from 'Utilities/String/translate';
import AddNotificationModal from './AddNotificationModal';
import EditNotificationModalConnector from './EditNotificationModalConnector';
import Notification from './Notification';
import styles from './Notifications.css';

class Notifications extends Component {

  //
  // Lifecycle

  constructor(props, context) {
    super(props, context);

    this.state = {
      isAddNotificationModalOpen: false,
      isEditNotificationModalOpen: false
    };
  }

  //
  // Listeners

  onAddNotificationPress = () => {
    this.setState({ isAddNotificationModalOpen: true });
  };

  onAddNotificationModalClose = ({ notificationSelected = false } = {}) => {
    this.setState({
      isAddNotificationModalOpen: false,
      isEditNotificationModalOpen: notificationSelected
    });
  };

  onEditNotificationModalClose = () => {
    this.setState({ isEditNotificationModalOpen: false });
  };

  //
  // Render

  render() {
    const {
      items,
      tagList,
      onConfirmDeleteNotification,
      ...otherProps
    } = this.props;

    const {
      isAddNotificationModalOpen,
      isEditNotificationModalOpen
    } = this.state;

    return (
      <FieldSet legend={translate('Connections')}>
        <PageSectionContent
          errorMessage={translate('UnableToLoadNotifications')}
          {...otherProps}
        >
          <div className={styles.notifications}>
            {
              items.map((item) => {
                return (
                  <Notification
                    key={item.id}
                    {...item}
                    tagList={tagList}
                    onConfirmDeleteNotification={onConfirmDeleteNotification}
                  />
                );
              })
            }

            <Card
              className={styles.addNotification}
              onPress={this.onAddNotificationPress}
            >
              <div className={styles.center}>
                <Icon
                  name={icons.ADD}
                  size={45}
                />
              </div>
            </Card>
          </div>

          <AddNotificationModal
            isOpen={isAddNotificationModalOpen}
            onModalClose={this.onAddNotificationModalClose}
          />

          <EditNotificationModalConnector
            isOpen={isEditNotificationModalOpen}
            onModalClose={this.onEditNotificationModalClose}
          />
        </PageSectionContent>
      </FieldSet>
    );
  }
}

Notifications.propTypes = {
  isFetching: PropTypes.bool.isRequired,
  error: PropTypes.object,
  items: PropTypes.arrayOf(PropTypes.object).isRequired,
  tagList: PropTypes.arrayOf(PropTypes.object).isRequired,
  onConfirmDeleteNotification: PropTypes.func.isRequired
};

export default Notifications;
