import React, { Component } from 'react';

import { connect } from 'react-redux';
import { bindActionCreators } from 'redux';
import * as AuthActions from 'cem/actions/auth';
import { pushPath } from 'redux-simple-router';

import { Link } from 'react-router';
import cn from 'classnames';

import UI from 'cem/components/ui';
import styles from 'cem/styles/sidebar';

import Toastr from 'cem/components/toastr';

class RootContainer extends Component {
  componentWillReceiveProps(props) {
    if (!props.state.auth.token) {
      this.props.actions.pushPath(`/login`);
    }
  }

  render() {
    const { props } = this;

    return (
      <section className={styles.pushRight}>
        <nav className={styles.sidebar}>
          {/* <Link className={styles.link} activeClassName={styles.active} to="/">
            <UI.Icon className={styles.icon} icon="dashboard" />
          </Link> */}
          <Link className={styles.link} activeClassName={styles.active} to="/deals">
            <UI.Icon className={styles.icon} icon="deals" />
          </Link>
          <Link className={styles.link} activeClassName={styles.active} to="/properties">
            <UI.Icon className={cn(styles.icon, styles.properties)} icon="properties" />
          </Link>
          <Link className={styles.link} activeClassName={styles.active} to="/contacts">
            <UI.Icon className={styles.icon} icon="contacts" />
          </Link>
          <Link className={styles.link} activeClassName={styles.active} to="/staff">
            <UI.Icon className={styles.icon} icon="staff" />
          </Link>
          <Link className={styles.link} activeClassName={styles.active} to="/settings">
            <UI.Icon className={styles.icon} icon="settings" />
          </Link>
          <UI.Button className={cn(styles.link, styles.logOut)} onClick={props.actions.logout}>
            <UI.Icon className={styles.icon} icon="exit" />
          </UI.Button>
        </nav>

        {props.children}

        <Toastr />
      </section>
    );
  }
}

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

const pickActions = (dispatch) => ({
  actions: bindActionCreators({ ...AuthActions, pushPath }, dispatch),
});

export default connect(pickState, pickActions)(RootContainer);
