var React = require("react"),
    Router = require("react-router"),
    Link = Router.Link;

var UserStore = require('../../stores/userStore');
var UserActions = require('../../actions/userActions');

var LoginStatus = React.createClass({
    getInitialState: function() {
        return UserStore.getLoginState();
    },
    componentDidMount: function() {
        UserStore.addChangeListener(this.onUserStateChange);
        UserActions.checkLogin();
    },
    componentWillReceiveProps: function() {
        this.update();
    },
    componentWillUnmount: function() {
        UserStore.removeChangeListener(this.onUserStateChange);
    },
    onUserStateChange: function() {
        this.update();
    },
    update: function() {
        if(this.isMounted()) {
            this.setState(UserStore.getLoginState());
        }
    },
    logout: function(e){
        e.preventDefault();
        UserActions.userLogout();
    },
  render : function() {


      var content;

      if(this.state.loggedIn){
          content = (<div style={{display: 'inline-block', paddingTop: '18px', paddingRight: '10px'}}>{this.state.user.first_name} {this.state.user.last_name}  <a onClick={this.logout} style={{ paddingLeft: '10px' }}>Odjavite se</a></div>)
      } else {
          content = (<div style={{display: 'inline-block', paddingTop: '18px', paddingRight: '10px'}}><Link to="registracija" style={{ paddingRight: '10px' }}>Registracija</Link>  <Link to="login">Prijava</Link></div>)
      }

      return content;
  }
})
module.exports = LoginStatus;
