// Copyright (c) 2017 PlanGrid, Inc.

import React from 'react';
import { Document, Page } from 'react-pdf';

export default class PDFDriver extends React.Component {
  constructor(props) {
    super(props);
    this.state = {
      numPages: null
    };

    this.onDocumentLoadSuccess = this.onDocumentLoadSuccess.bind(this);
  }

  onDocumentLoadSuccess(document) {
    const { numPages } = document;
    this.setState({
      numPages
    });
  }

  render() {
    const { numPages } = this.state;
    return (
      <div>
        <Document
          file={this.props.filePath}
          renderMode="svg"
          width={800}
          onLoadSuccess={this.onDocumentLoadSuccess}
          onLoadError={e => console.log(e)}
        >
          {Array.from(new Array(numPages), (el, index) => (
            <Page key={`page_${index + 1}`} pageNumber={index + 1} renderTextLayer={false} />
          ))}
        </Document>
      </div>
    );
  }
}

PDFDriver.defaultProps = {
  disableVisibilityCheck: false
};
