package edu.stuy.starlorn.util;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.HashMap;
import java.awt.event.KeyEvent;

public class Preferences {

    private static HashMap<String, Integer> _data;

    public static void load(int width, int height) {
        _data = new HashMap<String, Integer>();
        try {
            String filename = "preferences.txt";
            BufferedReader br = new BufferedReader(new FileReader(filename));

            while (br.ready()) {
                String[] data = br.readLine().split(" : ");
                _data.put(data[0], Integer.parseInt(data[1]));
            }

            br.close();

        } catch (FileNotFoundException e) {
            System.out.println("You don't seem to have a preferences file. I'll make one for you, then.");
            loadDefault(width, height);
            save();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static void save() {
        String filename = "preferences.txt";
        try {
            BufferedWriter b = new BufferedWriter(new FileWriter(filename));
            for (String key : _data.keySet()) {
                b.write(key);
                b.write(" : ");
                b.write("" + _data.get(key));
                b.newLine();
            }
            b.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static int getValue(String key) {
        return _data.get(key);
    }

    private static void loadDefault(int width, int height) {
        _data.put("upKey", KeyEvent.VK_W);
        _data.put("downKey", KeyEvent.VK_S);
        _data.put("leftKey", KeyEvent.VK_A);
        _data.put("rightKey", KeyEvent.VK_D);
        _data.put("shootKey", KeyEvent.VK_SPACE);
        _data.put("pauseKey", KeyEvent.VK_P);

        _data.put("fancyGraphics", 1);
        _data.put("screenWidth", width);
        _data.put("screenHeight", height);
        _data.put("fullScreen", 0);
    }


}

@@ -784,16 +784,94 @@
 close();
+%0A            loadDefault(width, height, false); // load in any missing options
 %0A%0A      
@@ -1044,32 +1044,38 @@
 lt(width, height
+, true
 );%0A            s
@@ -1781,62 +1781,163 @@
 ight
-) %7B%0A        _data.put(%22upKey%22, KeyEvent.VK_W);%0A       
+, boolean force) %7B%0A        if (force %7C%7C !_data.containsKey(%22upKey%22)) _data.put(%22upKey%22, KeyEvent.VK_W);%0A        if (force %7C%7C !_data.containsKey(%22downKey%22))
  _da
@@ -1969,32 +1969,76 @@
 t.VK_S);%0A       
+ if (force %7C%7C !_data.containsKey(%22leftKey%22))
  _data.put(%22left
@@ -2058,32 +2058,77 @@
 t.VK_A);%0A       
+ if (force %7C%7C !_data.containsKey(%22rightKey%22))
  _data.put(%22righ
@@ -2149,32 +2149,77 @@
 t.VK_D);%0A       
+ if (force %7C%7C !_data.containsKey(%22shootKey%22))
  _data.put(%22shoo
@@ -2244,32 +2244,77 @@
 _SPACE);%0A       
+ if (force %7C%7C !_data.containsKey(%22pauseKey%22))
  _data.put(%22paus
@@ -2349,130 +2349,324 @@
     
-_data.put(%22fancyGraphics%22, 1);%0A        _data.put(%22screenWidth%22, width);%0A        _data.put(%22screenHeight%22, height);%0A       
+if (force %7C%7C !_data.containsKey(%22fancyGraphics%22)) _data.put(%22fancyGraphics%22, 1);%0A        if (force %7C%7C !_data.containsKey(%22screenWidth%22)) _data.put(%22screenWidth%22, width);%0A        if (force %7C%7C !_data.containsKey(%22screenHeight%22)) _data.put(%22screenHeight%22, height);%0A        if (force %7C%7C !_data.containsKey(%22fullscreen%22))
  _da

Make preferences add anything that's missing on load