package info.jayharris.klondike;

import com.googlecode.blacken.colors.ColorNames;
import com.googlecode.blacken.colors.ColorPalette;
import com.googlecode.blacken.swing.SwingTerminal;
import com.googlecode.blacken.terminal.BlackenKeys;
import com.googlecode.blacken.terminal.CursesLikeAPI;
import com.googlecode.blacken.terminal.TerminalInterface;

public class TerminalUI implements KlondikeUI {

    private Klondike klondike;

    private boolean quit;
    private ColorPalette palette;
    private CursesLikeAPI term = null;

    public TerminalUI(Klondike klondike) {
        this.klondike = klondike;
    }

    protected boolean loop() {
        int ch = BlackenKeys.KEY_NO_KEY;
        if (palette.containsKey("Black")) {
            term.setCurBackground("Black");
        }
        if (palette.containsKey("White")) {
            term.setCurForeground("White");
        }
        term.puts("Terminal Interface\n");
        term.puts("Press F10 to quit.\n");
        while (!this.quit) {
            term.puts(">");
            // getch automatically does a refresh
            ch = term.getch();
            term.puts(BlackenKeys.toString(ch));
            if (ch == BlackenKeys.KEY_F10) {
                this.quit = true;
            }
            term.puts("\n");
        }
        term.refresh();
        return this.quit;
    }

    public void init(TerminalInterface term, ColorPalette palette) {
        if (term == null) {
            this.term = new CursesLikeAPI(new SwingTerminal());
            this.term.init("Klondike", 25, 80);
        }
        else {
            this.term = new CursesLikeAPI(term);
        }

        if (palette == null) {
            palette = new ColorPalette();
            palette.addAll(ColorNames.XTERM_256_COLORS, false);
            palette.putMapping(ColorNames.SVG_COLORS);
        }
        this.palette = palette;
        this.term.setPalette(palette);
    }

    public void quit() {
        this.quit = true;
        term.quit();
    }

    public static void main(String[] args) {
        TerminalUI ui= new TerminalUI(new Klondike());
        ui.init(null, null);
        ui.loop();
        ui.quit();
    }
}

@@ -713,21 +713,21 @@
 insKey(%22
-Black
+White
 %22)) %7B%0A  
@@ -759,21 +759,21 @@
 ground(%22
-Black
+White
 %22);%0A    
@@ -803,37 +803,37 @@
 te.containsKey(%22
-White
+Black
 %22)) %7B%0A          
@@ -849,37 +849,37 @@
 tCurForeground(%22
-White
+Black
 %22);%0A        %7D%0A  
@@ -958,24 +958,52 @@
  quit.%5Cn%22);%0A
+        this.term.clear();%0A%0A
         whil

write black text on white background