package gitto;

import java.io.File;
import java.net.URL;
import java.nio.file.Paths;
import java.util.ResourceBundle;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.*;
import javafx.scene.input.DragEvent;
import javafx.scene.input.Dragboard;
import javafx.scene.input.TransferMode;
import javafx.scene.layout.Pane;
import org.eclipse.jgit.api.Git;
import org.eclipse.jgit.lib.Constants;
import org.eclipse.jgit.lib.Repository;
import org.eclipse.jgit.storage.file.FileRepositoryBuilder;

public class ViewController implements Initializable {

    @FXML private TextField textField;
    @FXML private Pane      dropPane;

    private Git git;

    @Override public void initialize(URL location, ResourceBundle resources) {
        dropPane.addEventHandler(DragEvent.DRAG_OVER, (DragEvent event) -> {
            Dragboard db = event.getDragboard();
            if (db.hasFiles()) {
                event.acceptTransferModes(TransferMode.ANY);
            }
            event.consume();
        });

        dropPane.addEventHandler(DragEvent.DRAG_DROPPED, (DragEvent event) -> {
            Dragboard db = event.getDragboard();
            db.getFiles().stream().findFirst().ifPresent((File file) -> {
                String filePath = file.toString();
                if (this.git == null) {
                    loadGitFromPath(filePath);
                }
                addFile(file);
            });
            event.setDropCompleted(true);
        });
    }

    private void loadGitFromPath(String location) {
        File file = new File(location);
        File dir = file.isDirectory() ? file : file.getParentFile();

        FileRepositoryBuilder builder = new FileRepositoryBuilder();
        File gitPath = Paths.get(dir.getPath(), Constants.DOT_GIT).toFile();
        builder.findGitDir(gitPath);

        System.out.println(gitPath.getAbsolutePath());

        Repository repository;
        try {
            if (builder.getGitDir() == null) { // .gitがみつからない
                repository = FileRepositoryBuilder.create(gitPath);
            } else { // .gitがみつかった
                repository = builder.build();
            }
        } catch (Exception exception) {
            exception.printStackTrace();
            return;
        }

        this.textField.setText(repository.getDirectory().getParent());
        this.git = new Git(repository);
    }

    private void addFile(File file) {
        // TODO: implementation
    }

}

@@ -520,16 +520,68 @@
 Builder;
+%0Aimport org.eclipse.jgit.api.errors.GitAPIException;
 %0A%0Apublic
@@ -1460,16 +1460,53 @@
         
+addAllAndCommit();%0A                //
 addFile(
@@ -2162,24 +2162,174 @@
 e(gitPath);%0A
+                this.git = new Git(repository);%0A                // git init%0A                this.git.init().setDirectory(file).setBare(false).call();%0A
             
@@ -2393,24 +2393,72 @@
 er.build();%0A
+                this.git = new Git(repository);%0A
             
@@ -2566,25 +2566,24 @@
 ;%0A        %7D%0A
-%0A
         this
@@ -2645,48 +2645,8 @@
 ));%0A
-        this.git = new Git(repository);%0A
     
@@ -2682,24 +2682,24 @@
 ile file) %7B%0A
-
         // T
@@ -2725,10 +2725,438 @@
 %0A    %7D%0A%0A
+    private void addAllAndCommit() %7B%0A        try %7B%0A            //git add --all %7C%7C git commit%0A            this.git.add().addFilepattern(%22.%22).call();%0A            this.git.commit().setMessage(%22Commit all changes including additions%22).call();%0A            System.out.println(%22Committed all changes to repository%22);%0A        %7D catch (Exception exception) %7B%0A            exception.printStackTrace();%0A            return;%0A        %7D%0A    %7D%0A%0A
 %7D%0A

add -- all||commitの追加