package simulator.ui;

import common.annotations.InstantiateOnEDT;
import common.annotations.InvokeLaterNotNecessary;
import common.instruction.Instruction;
import lombok.EqualsAndHashCode;
import lombok.Value;
import lombok.experimental.NonFinal;
import lombok.val;
import simulator.program.Program;

import javax.swing.*;
import javax.swing.text.BadLocationException;
import javax.swing.text.StyledDocument;
import java.awt.*;
import java.util.ArrayList;
import java.util.List;

@InstantiateOnEDT
@Value
@EqualsAndHashCode(callSuper = true)
class ProgramView extends JPanel {
  // Displays the entire program
  JTextPane programFrontend = new JTextPane();
  StyledDocument programDocument = programFrontend.getStyledDocument();

  // Contains all the instructions shown in the programFrontend
  List<Instruction> instructionsInDisplayedProgram = new ArrayList<>();

  public ProgramView() {
    // The BorderLayout is what allows us to fit the text pane to the panel
    super(new BorderLayout());
    JScrollPane scrollPane = new JScrollPane(programFrontend);
    scrollPane.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
    this.add(scrollPane, BorderLayout.CENTER);
    programFrontend.setEditable(false);
  }

  @InvokeLaterNotNecessary
  public void display(Program p) {
    SwingUtilities.invokeLater(() -> {
      // Clear the old instructions - if any
      programFrontend.setText("");

      // Adding the elements has to happen on the EDT
      p.getInstructions().forEach(this::append);
    });
  }

  @NonFinal
  boolean empty = true;

  private void append(Instruction i) {
    try {
      if (empty) {
        programDocument.insertString(
              programDocument.getLength(),
              i.getMnemonicRepresentation(), null);
        empty = false;
      } else {
        programDocument.insertString(
              programDocument.getLength(),
              "\n" + i.getMnemonicRepresentation(), null);
      }
      instructionsInDisplayedProgram.add(i);
    } catch (BadLocationException e) {
      e.printStackTrace();
    }
  }
}

@@ -245,27 +245,8 @@
 al;%0A
-import lombok.val;%0A
 impo
@@ -1214,16 +1214,204 @@
 false);%0A
+    append(%22No MIPS32 program has been loaded into memory.%22);%0A    append(%22Try ALT+F by CTRL+L to open the file browser, or use the%22);%0A    append(%22%5C%22File%5C%22-menu in the top-left corner.%22);%0A%0A
   %7D%0A%0A  @
@@ -1731,19 +1731,32 @@
 ean 
-empty = tru
+displayingProgram = fals
 e;%0A%0A
@@ -1781,21 +1781,16 @@
 end(
-Instruction i
+String s
 ) %7B%0A
@@ -1803,29 +1803,8 @@
 y %7B%0A
-      if (empty) %7B%0A  
     
@@ -1830,39 +1830,24 @@
 nsertString(
-%0A              
 programDocum
@@ -1866,201 +1866,222 @@
 h(),
-%0A              i.getMnemonicRepresentation(), null);%0A        empty = false;%0A      %7D else %7B%0A        programDocument.insertString(%0A              programDocument.getLength(),%0A              %22%5Cn%22 + 
+ s + %22%5Cn%22, null);%0A    %7D catch (BadLocationException e) %7B%0A      e.printStackTrace();%0A    %7D%0A  %7D%0A%0A  private void append(Instruction i) %7B%0A    if (!displayingProgram) %7B%0A      displayingProgram = true;%0A    %7D%0A%0A    append(
 i.ge
@@ -2109,27 +2109,11 @@
 on()
-, null);%0A      %7D%0A  
+);%0A
     
@@ -2155,80 +2155,8 @@
 i);%0A
-    %7D catch (BadLocationException e) %7B%0A      e.printStackTrace();%0A    %7D%0A
   %7D%0A

Clearify the textpane