import React, { Component, PropTypes } from 'react';
import Pager from '@dtrussia/react-pager';
import 'jqestate.ru/styles/app/pagination';

class Pagination extends Component {
  static propTypes = {
    total: PropTypes.number.isRequired,
    offset: PropTypes.number.isRequired,
    limit: PropTypes.number.isRequired,
    update: PropTypes.func.isRequired,
  }

  constructor(props) {
    super(props);

    this.state = {
      visiblePages: 5,
    };
  }

  handlePageChanged(newPage) {
    const offset = newPage * this.props.limit;
    this.props.update(offset);
  }

  render() {
    const { total, offset, limit } = this.props;
    const current = Math.max(Math.ceil(offset / limit));
    const totalPages = Math.max(Math.ceil(total / limit));

    if (total) {
      return <Pager total={totalPages} current={current} visiblePages={this.state.visiblePages} onPageChanged={this.handlePageChanged.bind(this)} />;
    } else {
      return null;
    }
  }
}

export default Pagination;
