package utils;

import gui.MainFrame;
import gui.UserInfoDisplay;
import java.awt.GridLayout;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import javax.swing.JFileChooser;

public class FileSaverAndLoader { // TODO: limit file save/load to .save files

    private final MainFrame mainFrame;

    public FileSaverAndLoader(MainFrame mainFrame) {
        this.mainFrame = mainFrame;
    }

    public void saveToFile() throws IOException {
        JFileChooser fileChooser = new JFileChooser();
        if (fileChooser.showSaveDialog(mainFrame) == JFileChooser.APPROVE_OPTION) {
            File file = fileChooser.getSelectedFile();
            FileOutputStream fs = new FileOutputStream(file);
            try (ObjectOutputStream os = new ObjectOutputStream(fs)) {
                mainFrame.getEnemyMap().getSaveOrLoad().saveMap(os);
                mainFrame.getUserMap().getSaveOrLoad().saveMap(os);
            }
        }
    }

    public void loadFromFile() throws IOException, ClassNotFoundException {
        JFileChooser fileChooser = new JFileChooser();
        if (fileChooser.showOpenDialog(mainFrame) == JFileChooser.APPROVE_OPTION) {
            mainFrame.getContentPane().removeAll(); // could call New Game, but need to move instruction window first
            GridLayout layout = new GridLayout(2, 2);
            mainFrame.setLayout(layout);
            mainFrame.addPanels();
            ((UserInfoDisplay) mainFrame.getUserInfo()).removeButtons();

            mainFrame.getEnemyMap().setShipsPlaced(true);
            mainFrame.getUserMap().setShipsPlaced(true);

            File file = fileChooser.getSelectedFile();
            FileInputStream fis = new FileInputStream(file);
            try (ObjectInputStream ois = new ObjectInputStream(fis)) {
                mainFrame.getEnemyMap().getSaveOrLoad().loadMap(ois);
                mainFrame.getUserMap().getSaveOrLoad().loadMap(ois);
            }
        }
        mainFrame.revalidate();
        mainFrame.repaint();
    }

}

@@ -9,16 +9,45 @@
 utils;%0A%0A
+import gui.EnemyInfoDisplay;%0A
 import g
@@ -1301,32 +1301,33 @@
 PROVE_OPTION) %7B%0A
+%0A
             main
@@ -1562,78 +1562,414 @@
 ();%0A
-            ((UserInfoDisplay) mainFrame.getUserInfo()).removeButtons(
+%0A            ((EnemyInfoDisplay) mainFrame.getEnemyInfo()).showEnemyInfo();%0A            ((UserInfoDisplay) mainFrame.getUserInfo()).removeButtons();%0A            ((UserInfoDisplay) mainFrame.getUserInfo()).addRandomFireButton();%0A%0A            mainFrame.getEnemyInfo().setInfoLabelText(%22Number of enemy ships remaining:%22);%0A            mainFrame.getUserInfo().setInfoLabelText(%22Number of your ships remaining:%22
 );%0A%0A

Fix buttons and enemy info not showing after load