package controllers;

import javafx.fxml.FXML;
import javafx.geometry.Pos;
import javafx.scene.Node;
import javafx.scene.control.Button;
import javafx.scene.control.TextField;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.layout.FlowPane;
import javafx.scene.layout.HBox;
import javafx.scene.text.Text;
import javafx.stage.Stage;
import javafx.collections.ObservableList;
import javafx.event.ActionEvent;
import model.PhotoManager;
import model.Photo;

import java.awt.Desktop;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

public class ViewPhotoScreenController extends Controller {

    @FXML private ImageView preview;
    @FXML private FlowPane tags;
    @FXML private Button addTagButton;
    @FXML private Button editTagsButton;
    @FXML private Button undoButton;
    @FXML private Button confirmChangesButton;
    @FXML private HBox buttonBox;
    private boolean editingTags;
    
    private void flipButtons() {
        if (editingTags) {
            editingTags = false;
            buttonBox.getChildren().remove(0);
            buttonBox.getChildren().remove(0);
            buttonBox.getChildren().remove(0);
            buttonBox.getChildren().add(0, editTagsButton);
            /*
            addTagButton.setVisible(false);
            undoButton.setVisible(false);
            confirmChangesButton.setVisible(false);
            editTagsButton.setVisible(true);*/
        } else {
            editingTags = true;
            buttonBox.getChildren().remove(0);
            buttonBox.getChildren().add(0, undoButton);
            buttonBox.getChildren().add(0, addTagButton);
            buttonBox.getChildren().add(0, confirmChangesButton);
            /*
            addTagButton.setVisible(true);
            undoButton.setVisible(true);
            confirmChangesButton.setVisible(true);
            editTagsButton.setVisible(false);*/
        }
    }
    
    @FXML
    private void initialize() {
        preview.setImage(PhotoManager.getInstance().getCurrentPhoto().getPreviewImg());
        updateTagView();
        tags.setAlignment(Pos.TOP_LEFT);
        buttonBox.getChildren().remove(0);
        buttonBox.getChildren().remove(0);
        buttonBox.getChildren().remove(0);
    }

    public void setStage(Stage stage) {
        this.stage = stage;
    }

    @FXML protected void onBackPress(ActionEvent event) {
        stage.close();
    }

    @FXML protected void onViewInFolderPress(ActionEvent event) {
        try {
            Desktop.getDesktop().open(PhotoManager.getInstance().getCurrentPhoto().getFile().getParentFile());
        } catch (IOException io) {
            System.out.println("File Not Found");
        };
    }

    @FXML protected void onEditTagsPress(ActionEvent event) {
        flipButtons();
        updateTagView();
    }
    
    @FXML protected void onAddTagPress(ActionEvent event) {
        addTagToView("        ");
    }
    
    @FXML protected void onUndoPress(ActionEvent event) {
        flipButtons();
        updateTagView();
    }
    
    @FXML protected void onConfirmChangesPress(ActionEvent event) {
        updateTagModel();
        flipButtons();
        updateTagView();
    }
    
    private void updateTagModel() {
        List<String> newTags = new ArrayList<>();
        for (Node n : tags.getChildren()) {
            if (n instanceof HBox) {
                HBox h = (HBox) n;
                Node text = h.getChildren().get(0);
                if (text instanceof Text) {
                    newTags.add(((Text)text).getText().trim());
                }
                if (text instanceof TextField) {
                    newTags.add(((TextField)text).getText().trim());
                }
            }
        }
        while (newTags.contains("")) {
            newTags.remove("");
        }
        PhotoManager.getInstance().getCurrentPhoto().setTags(newTags);
        PhotoManager.getInstance().getCurrentPhoto().commitTagChanges();
    }

    private void updateTagView() {
        tags.getChildren().clear();
        for (String tag : PhotoManager.getInstance().getCurrentPhoto().getTags()) {
            addTagToView(tag);
        }
    }
    
    private void addTagToView(String tag) {
        HBox tagView = new HBox();
        tagView.setStyle("-fx-background-color: #E0FFFF; "
                + "-fx-border-radius: 3;"
                + "-fx-border-color: #87CEFA;");
        ObservableList<Node> children = tagView.getChildren();
        children.add(new Text(tag));
        if (editingTags) {
            ImageView x = new ImageView(new Image("/icons/cancel.png", 10, 10, true, true));
            x.setOnMouseClicked((e) -> {
                tags.getChildren().remove(tagView);
            });
            tagView.setOnMouseClicked((e) -> {
                tagView.getChildren().remove(0);
                tagView.getChildren().add(0, new TextField(tag.trim()));
            });
            children.add(x);
        }
        tags.getChildren().add(tagView);
    }
}
@@ -363,24 +363,67 @@
 tage.Stage;%0A
+import javafx.beans.value.ObservableValue;%0A
 import javaf
@@ -448,24 +448,24 @@
 rvableList;%0A
-
 import javaf
@@ -1000,21 +1000,17 @@
 ngTags;%0A
-    
 %0A
+
     priv
@@ -1299,208 +1299,8 @@
 n);%0A
-            /*%0A            addTagButton.setVisible(false);%0A            undoButton.setVisible(false);%0A            confirmChangesButton.setVisible(false);%0A            editTagsButton.setVisible(true);*/%0A
     
@@ -1575,206 +1575,8 @@
 n);%0A
-            /*%0A            addTagButton.setVisible(true);%0A            undoButton.setVisible(true);%0A            confirmChangesButton.setVisible(true);%0A            editTagsButton.setVisible(false);*/%0A
     
@@ -1583,29 +1583,25 @@
     %7D%0A    %7D%0A
-    
 %0A
+
     @FXML%0A  
@@ -2485,37 +2485,33 @@
 agView();%0A    %7D%0A
-    
 %0A
+
     @FXML protec
@@ -2586,37 +2586,33 @@
       %22);%0A    %7D%0A
-    
 %0A
+
     @FXML protec
@@ -2703,29 +2703,25 @@
 ew();%0A    %7D%0A
-    
 %0A
+
     @FXML pr
@@ -2852,29 +2852,25 @@
 ew();%0A    %7D%0A
-    
 %0A
+
     private 
@@ -3835,13 +3835,9 @@
   %7D%0A
-    
 %0A
+
     
@@ -4457,113 +4457,673 @@
     
-tagView.getChildren().remove(0);%0A                tagView.getChildren().add(0, new TextField(tag.trim()));
+if (tagView.getChildren().get(0) instanceof Text) %7B%0A                    TextField tf = new TextField(((Text)tagView.getChildren().get(0)).getText().trim());%0A                    tagView.getChildren().remove(0);%0A                    tf.focusedProperty().addListener((ObservableValue%3C? extends Boolean%3E observable, Boolean oldValue, Boolean newValue) -%3E %7B%0A                        if (newValue == false) %7B%0A                            tagView.getChildren().remove(0);%0A                            tagView.getChildren().add(0, new Text(tf.getText()));%0A                        %7D%0A                    %7D);%0A                    tagView.getChildren().add(0, tf);%0A                %7D
 %0A   

tag editing is now more intuitive