import React, { Component } from 'react';
import cn from 'classnames';

import { connect } from 'react-redux';
// import { bindActionCreators } from 'redux';
// import * as ToastrActions from 'cem/actions/toastr';

import styles from 'cem/styles/toastr';

class Toastr extends Component {
  constructor() {
    super();

    this.state = {
      active: false,
    };
  }

  componentWillReceiveProps(props) {
    if (props.state.toastr.timestamp !== this.props.state.toastr.timestamp) {
      this.setState({
        active: true,
      }, () => {
        setTimeout(() => {
          this.setState({
            active: false,
          });
        }, 3000);
      });
    }
  }

  render() {
    const { title, body, kind } = this.props.state.toastr;

    if (this.state.active) {
      return (
        <div className={cn(styles.container, styles[kind])}>
          {title &&
            <h4 className={styles.title}>{title}</h4>
          }

          {body &&
            <p className={styles.body}>{body}</p>
          }
        </div>
      );
    }

    return null;
  }
}

const pickState = ({ toastr }) => ({
  state: { toastr },
});

export default connect(pickState)(Toastr);
