package net.sf.jabref.gui.entryeditor;

import java.awt.Dimension;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Optional;

import javax.swing.DefaultListModel;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;

import net.sf.jabref.gui.BasePanel;
import net.sf.jabref.gui.JabRefFrame;
import net.sf.jabref.logic.pdf.PdfCommentImporter;
import net.sf.jabref.model.entry.BibEntry;
import net.sf.jabref.model.entry.FieldName;
import javax.swing.JTextField;

public class PdfCommentsTab extends JPanel {

    private final JPanel informationPanel = new JPanel();

    private final JList<String> commentList = new JList<>();

    private final JScrollPane commentScrollPane = new JScrollPane();

    private final JLabel authorLabel = new JLabel("author", JLabel.CENTER);

    private final JTextArea authorArea = new JTextArea("author", 2, 25);

    private final JScrollPane authorScrollPane = new JScrollPane();

    private final JLabel dateLabel = new JLabel("date", JLabel.CENTER);

    private final JTextArea dateArea = new JTextArea("date", 2, 25);

    private final JScrollPane dateScrollPane = new JScrollPane();

    private final JLabel pageLabel = new JLabel("page", JLabel.CENTER);

    private final JTextArea pageArea = new JTextArea("page", 2, 25);

    DefaultListModel<String> listModel;

    private final EntryEditor parent;

    private final String tabTitle;

    private final JabRefFrame frame;

    private final BasePanel basePanel;
    private JTextField txtCommentContent;

    public PdfCommentsTab(EntryEditor parent, JabRefFrame frame, BasePanel basePanel) {
        this.parent = parent;
        this.frame = frame;
        this.basePanel = basePanel;
        this.tabTitle = "PDF comments";
        this.setUpInformationPanel();
        listModel  = new DefaultListModel<>();
        try {
            this.setUpPdfCommentsTab();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    private void setUpPdfCommentsTab() throws IOException {
        Optional<String> field = parent.getEntry().getField(FieldName.FILE);
        if (field.isPresent()) {

            commentList.setModel(listModel);
            PdfCommentImporter commentImporter = new PdfCommentImporter();
            ArrayList<BibEntry> entries = new ArrayList<>();
            entries.add(parent.getEntry());
            commentImporter.importPdfFile(entries, basePanel.getBibDatabaseContext());

            HashMap<String, String> importedNotes = commentImporter.importNotes(commentImporter.importPdfFile(entries,
                    basePanel.getBibDatabaseContext()).get(0));
            updateShownComments(importedNotes);
        }
        setLayout(null);
        commentScrollPane.setBounds(26, 16, 450, 389);

        commentScrollPane.setPreferredSize(new Dimension(450,200));

        this.add(commentScrollPane);
        commentScrollPane.setViewportView(commentList);
        informationPanel.setBounds(583, 16, 650, 389);
        this.add(informationPanel);
    }

    private void updateShownComments(HashMap<String, String> importedNotes){
        importedNotes.values().stream().forEach((note) -> listModel.addElement(note));
    }



    private void setUpInformationPanel(){
        informationPanel.setLayout(null);
        authorLabel.setBounds(15, 25, 46, 20);

        informationPanel.add(authorLabel);
        authorScrollPane.setBounds(76, 25, 547, 42);
        informationPanel.add(authorScrollPane);
        authorScrollPane.setViewportView(authorArea);
        authorArea.setEditable(false);
        dateLabel.setBounds(15, 80, 30, 20);
        informationPanel.add(dateLabel);
        dateScrollPane.setBounds(76, 83, 547, 42);
        informationPanel.add(dateScrollPane);
        dateScrollPane.setViewportView(dateArea);
        dateArea.setEditable(false);
        pageLabel.setBounds(15, 128, 34, 20);
        informationPanel.add(pageLabel);

        JScrollPane pageScrollPane = new JScrollPane();
        pageScrollPane.setBounds(76, 141, 547, 42);
        informationPanel.add(pageScrollPane);
        pageScrollPane.setViewportView(pageArea);
        pageArea.setEditable(false);

        txtCommentContent = new JTextField();
        txtCommentContent.setText("comment content");
        txtCommentContent.setBounds(76, 200, 547, 173);
        informationPanel.add(txtCommentContent);
        txtCommentContent.setColumns(10);

        JLabel lblContent = new JLabel("content");
        lblContent.setBounds(15, 200, 69, 20);
        informationPanel.add(lblContent);
    }
}
@@ -565,39 +565,8 @@
 ame;
-%0Aimport javax.swing.JTextField;
 %0A%0Apu
@@ -1369,24 +1369,333 @@
 %22, 2, 25);%0A%0A
+    private final JScrollPane pageScrollPane = new JScrollPane();%0A%0A    private final JLabel commentTxtLabel = new JLabel(%22content%22,JLabel.CENTER);%0A%0A    private final JTextArea commentTxtArea = new JTextArea(%22comment content%22, 10, 25);%0A%0A    private final JScrollPane commentTxtScrollPane = new JScrollPane();%0A%0A
     DefaultL
@@ -1878,50 +1878,8 @@
 nel;
-%0A    private JTextField txtCommentContent;
 %0A%0A  
@@ -3636,32 +3636,33 @@
 etLayout(null);%0A
+%0A
         authorLa
@@ -3688,25 +3688,24 @@
 5, 46, 20);%0A
-%0A
         info
@@ -3921,32 +3921,33 @@
 ditable(false);%0A
+%0A
         dateLabe
@@ -4196,24 +4196,33 @@
 ble(false);%0A
+        %0A
         page
@@ -4299,65 +4299,8 @@
 el);
-%0A%0A        JScrollPane pageScrollPane = new JScrollPane();
 %0A   
@@ -4494,97 +4494,96 @@
     
-txtC
+c
 omment
-Content = new JTextField();%0A        txtCommentContent.setText(%22comment content%22
+TxtLabel.setBounds(15, 200, 69, 20);%0A        informationPanel.add(commentTxtLabel
 );%0A 
@@ -4589,33 +4589,36 @@
         
-txtC
+c
 omment
-Content
+TxtScrollPane
 .setBoun
@@ -4669,33 +4669,36 @@
 nel.add(
-txtC
+c
 omment
-Content
+TxtScrollPane
 );%0A     
@@ -4704,183 +4704,106 @@
     
-txtC
+c
 omment
-Content.setColumns(10);%0A%0A        JLabel lblContent = new JLabel(%22content%22);%0A        lblContent.setBounds(15, 200, 69, 20);%0A        informationPanel.add(lblContent
+TxtScrollPane.setViewportView(commentTxtArea);%0A        commentTxtArea.setEditable(false
 );%0A
+%0A
     

refactor PdfCommentsTab