package org.jabref.gui.entryeditor.fileannotationtab;

import javafx.beans.property.SimpleStringProperty;
import javafx.beans.property.StringProperty;

import org.jabref.logic.l10n.Localization;
import org.jabref.model.pdf.FileAnnotation;
import org.jabref.model.pdf.FileAnnotationType;

public class FileAnnotationViewModel {

    private final FileAnnotation annotation;
    private StringProperty author = new SimpleStringProperty();
    private StringProperty page = new SimpleStringProperty();
    private StringProperty date = new SimpleStringProperty();
    private StringProperty content = new SimpleStringProperty();
    private StringProperty marking = new SimpleStringProperty();

    public FileAnnotationViewModel(FileAnnotation annotation) {
        this.annotation = annotation;
        author.set(annotation.getAuthor());
        page.set(Integer.toString(annotation.getPage()));
        date.set(annotation.getTimeModified().toString());
        content.set(getContentOrNA(annotation.getContent()));
        marking.set(getMarking(annotation));
    }

    private static String getMarking(FileAnnotation annotation) {
        if (annotation.hasLinkedAnnotation()) {
            return getContentOrNA(annotation.getLinkedFileAnnotation().getContent());
        }
        return "N/A";
    }

    private static String getContentOrNA(String content) {
        if (content.isEmpty()) {
            return "N/A";
        }
        return content;
    }

    public String getAuthor() {
        return author.get();
    }

    public String getPage() {
        return page.get();
    }

    public String getDate() {
        return date.get();
    }

    public String getContent() {
        return content.get();
    }

    public StringProperty pageProperty() {
        return page;
    }

    public StringProperty dateProperty() {
        return date;
    }

    public StringProperty contentProperty() {
        return content;
    }

    public StringProperty markingProperty() {
        return marking;
    }

    public StringProperty authorProperty() {
        return author;
    }

    @Override
    public String toString() {
        if (annotation.hasLinkedAnnotation() && this.getContent().isEmpty()) {
            if (FileAnnotationType.UNDERLINE.equals(annotation.getAnnotationType())) {
                return Localization.lang("Empty Underline");
            }
            if (FileAnnotationType.HIGHLIGHT.equals(annotation.getAnnotationType())) {
                return Localization.lang("Empty Highlight");
            }
            return Localization.lang("Empty Marking");
        }

        if (FileAnnotationType.UNDERLINE.equals(annotation.getAnnotationType())) {
            return Localization.lang("Underline") + ": " + this.getContent();
        }
        if (FileAnnotationType.HIGHLIGHT.equals(annotation.getAnnotationType())) {
            return Localization.lang("Highlight") + ": " + this.getContent();
        }

        return super.toString();
    }

    public String getDescription() {
        return marking.get();
    }
}

@@ -960,92 +960,30 @@
     
-content.set(getContentOrNA(annotation.getContent()));%0A        marking.set(getMarking
+setupContentProperties
 (ann
@@ -990,17 +990,16 @@
 otation)
-)
 ;%0A    %7D%0A
@@ -1015,32 +1015,35 @@
 ate 
-static String getMarking
+void setupContentProperties
 (Fil
@@ -1132,29 +1132,36 @@
     
-return getContentOrNA
+this.content.set(toNAifEmpty
 (ann
@@ -1207,16 +1207,17 @@
 ntent())
+)
 ;%0A      
@@ -1222,31 +1222,412 @@
     
-%7D%0A        return %22N/A%22;
+    String annotationContent = annotation.getContent();%0A            String illegibleTextMessage = Localization.lang(%22The marked area does not contain any legible text!%22);%0A            this.marking.set(annotationContent.isEmpty() ? illegibleTextMessage : annotationContent);%0A        %7D else %7B%0A            this.content.set(toNAifEmpty(annotation.getContent()));%0A            this.marking.set(%22N/A%22);%0A        %7D
 %0A   
@@ -1660,22 +1660,19 @@
 ing 
-getContentOrNA
+toNAifEmpty
 (Str

fix the filling of the content fields