package mainWindow;

import java.io.File;
import java.io.IOException;

import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.pdmodel.encryption.InvalidPasswordException;
import org.apache.pdfbox.rendering.PDFRenderer;

import javafx.embed.swing.SwingFXUtils;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.fxml.FXML;
import javafx.stage.FileChooser;
import javafx.stage.FileChooser.ExtensionFilter;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.layout.AnchorPane;
import javafx.scene.layout.HBox;
import javafx.scene.control.Button;
import javafx.scene.control.TextField;

public class MainWindowController {
	
	@FXML AnchorPane anchorPane;
	@FXML ImageView pdfContainer;
	@FXML Button prevButton;
	@FXML Button nextButton;
	@FXML TextField pageNumber;
	
	private int pageNo = 0;
	private PDDocument pdfFile;

	@FXML
	private void clickOpen() throws IOException {
		File selectedFile = openFileChoser();
		openPDFFile(selectedFile);
	}

	private void openPDFFile(File selectedFile) {
		
		
		if (selectedFile == null){
			return;
		}
		try {
			pdfFile = PDDocument.load(selectedFile);
		} catch (InvalidPasswordException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		openPDFPage(pageNo);
		
		anchorPaneListeners();
		navButtonListeners();
		
	}

	private void navButtonListeners() {
		prevButton.setOnAction(new EventHandler<ActionEvent>() {
		    @Override public void handle(ActionEvent e) {
		        pageNo--;
		        openPDFPage(pageNo);
		    }
		});
		
		nextButton.setOnAction(new EventHandler<ActionEvent>() {
		    @Override public void handle(ActionEvent e) {
		        pageNo++;
		        openPDFPage(pageNo);
		    }
		});
		
		pageNumber.setOnAction(new EventHandler<ActionEvent>(){
			@Override public void handle(ActionEvent e){
				pageNo = Integer.parseInt(pageNumber.getText());
				openPDFPage(pageNo);
			}
		});
	}
	
	private void openPDFPage(int pageNo){
		PDFRenderer renderer = new PDFRenderer(pdfFile);
        Image image = null;
        try {
			image = SwingFXUtils.toFXImage(renderer.renderImage(pageNo), null);
		} catch (IOException e1) {
			e1.printStackTrace();
		}
        pdfContainer.setImage(image);
	}

	private void anchorPaneListeners() {
		anchorPane.widthProperty().addListener((obs, oldVal, newVal) -> {			
    		pdfContainer.setX(pdfContainer.getX() + newVal.doubleValue() - oldVal.doubleValue());	
    		pdfContainer.setFitWidth(pdfContainer.getFitWidth() + newVal.doubleValue() - oldVal.doubleValue());
			pdfContainer.preserveRatioProperty();
		});
		
		anchorPane.heightProperty().addListener((obs, oldVal, newVal) -> {
			pdfContainer.setFitHeight(pdfContainer.getFitHeight() + newVal.doubleValue() - oldVal.doubleValue());
			pdfContainer.preserveRatioProperty();
		});
	}

	private File openFileChoser() {
		FileChooser fileChooser = new FileChooser();
		fileChooser.setTitle("Open PDF File");
		fileChooser.getExtensionFilters().add(new ExtensionFilter("PDF Files", "*.pdf"));
		File selectedFile = fileChooser.showOpenDialog(anchorPane.getScene().getWindow());
		return selectedFile;
	}
}

@@ -555,41 +555,8 @@
 ne;%0A
-import javafx.scene.layout.HBox;%0A
 impo
@@ -1041,19 +1041,16 @@
 e) %7B%0A%09%09%0A
-%09%09%0A
 %09%09if (se
@@ -1490,28 +1490,51 @@
  e) %7B%0A%09%09    
-    
+%09if (pageNo != 0)%7B%0A%09%09    %09%09
 pageNo--;%0A%09%09
@@ -1529,36 +1529,34 @@
 pageNo--;%0A%09%09    
-    
+%09%09
 openPDFPage(page
@@ -1552,32 +1552,41 @@
 DFPage(pageNo);%0A
+%09%09    %09%7D%0A
 %09%09    %7D%0A%09%09%7D);%0A%09%09
@@ -1703,20 +1703,68 @@
 %7B%0A%09%09    
-    
+%09if (pageNo != pdfFile.getNumberOfPages())%7B%0A%09%09    %09%09
 pageNo++
@@ -1775,16 +1775,17 @@
         
+%09
 openPDFP
@@ -1793,24 +1793,33 @@
 ge(pageNo);%0A
+%09%09    %09%7D%0A
 %09%09    %7D%0A%09%09%7D)
@@ -1929,16 +1929,27 @@
 ent e)%7B%0A
+%09%09%09%09try %7B%0A%09
 %09%09%09%09page
@@ -1993,16 +1993,75 @@
 ext());%0A
+%09%09%09%09%7D catch (NumberFormatException exception)%7B%0A%09%09%09%09%09%0A%09%09%09%09%7D%0A
 %09%09%09%09open
@@ -2299,34 +2299,32 @@
 ll);%0A%09%09%7D catch (
-IO
 Exception e1) %7B%0A
@@ -2322,17 +2322,16 @@
 on e
-1
 ) %7B%0A%09%09%09e
 1.pr
@@ -2330,9 +2330,8 @@
 %09%09%09e
-1
 .pri
@@ -2384,24 +2384,65 @@
 age(image);%0A
+        pageNumber.setText(%22%22 + pageNo);%0A
 %09%7D%0A%0A%09private

Fix bugs pressing previous and next buttons on first and last pages.