import React, { Component } from 'react';


export default class PageSwitcher extends Component {

	constructor(props) {
		super(props);

		this.handlePrevPage = this.handlePrevPage.bind(this);
		this.handleNextPage = this.handleNextPage.bind(this);
		this.switchPage = this.switchPage.bind(this);
	}


	handlePrevPage(e) {
		e.preventDefault();
		this.switchPage(this.props.paging.page - 1);
	}


	handleNextPage(e) {
		e.preventDefault();
		this.switchPage(this.props.paging.page + 1);
	}


	switchPage(page) {
		let paging = {  ...this.props.paging, page };
		if (typeof this.props.onPageChanged !== "function") {
			return;
		}

		this.props.onPageChanged(paging);
	}


	render() {
		let { paging, pending, disabled, displayNoPages } = this.props;
		if (!paging) {
			return null;
		}

		//TODO
		if (paging.pagesCount < 2 && !displayNoPages) {
			return null;
		}


		let buttons = [];

		buttons.push(<button key="prev" className="btn" onClick={ this.handlePrevPage } type="button" disabled={ pending || disabled || paging.page < 1 } >&lt;</button> )
		buttons.push(<button key="next" className="btn" onClick={ this.handleNextPage } type="button" disabled={ pending || disabled || paging.page > paging.pagesCount - 2 } >&gt;</button> )

		return (
			<div className="right">
				{ buttons }
			</div>
		);
	}

}