import React, { Component, PropTypes } from 'react';
import parsePath from 'history/lib/parsePath';
import Location from '../../core/Location';

function isLeftClickEvent(event) {
  return event.button === 0;
}

function 
(event) {
  return !!(event.metaKey || event.altKey || event.ctrlKey || event.shiftKey);
}

class Link extends Component {

  static propTypes = {
    to: PropTypes.string.isRequired,
    query: PropTypes.object,
    state: PropTypes.object,
    onClick: PropTypes.func,
  };

  static handleClick = (event) => {
    let allowTransition = true;
    let clickResult;

    if (this.props && this.props.onClick) {
      clickResult = this.props.onClick(event);
    }

    if (isModifiedEvent(event) || !isLeftClickEvent(event)) {
      return;
    }

    if (clickResult === false || event.defaultPrevented === true) {
      allowTransition = false;
    }
    // bootstrap navbar-collapse
    if ($('.navbar-collapse')) $('.navbar-collapse').collapse('hide');

    event.preventDefault();

    if (allowTransition) {
      const link = event.currentTarget;
      if (this.props && this.props.to) {
        Location.push({
          ...(parsePath(this.props.to)),
          state: this.props && this.props.state || null,
        });
      } else {
        Location.push({
          pathname: link.pathname,
          search: link.search,
          state: this.props && this.props.state || null,
        });
      }
    }
  };

  render() {
    const { to, query, ...props } = this.props;
    return <a href={Location.createHref(to, query)} onClick={Link.handleClick.bind(this)} {...props} />;
  }

}

export default Link;

isModifiedEvent