package model.database;

import java.util.HashMap;
import java.util.Map;

public final class Database {

    private static Database instance;
    private Map<String, String[]> varsMap;
    private Map<String, String[][]> cmdsMap;

    private Database () {
    }

    public static synchronized Database getInstance () {
        if (instance == null)
            instance = new Database();
        return instance;
    }

    public void addVariable (String name, String[] value) {
        varsMap.put(name, value);
    }

    public void addCommand (String name, String[] args, String[] value) {
        String[][] mapValue = { args, value };
        cmdsMap.put(name, mapValue);
    }

    public String[] getVariable (String name) {
        return varsMap.get(name);
    }

    public String[][] getCommand (String name) {
        return cmdsMap.get(name);
    }
    
    public Map<String, String> getViewerVarsMap(){
        Map<String, String> result = new HashMap<String,String>();
        for (String key: varsMap.keySet()){
            result.put(key, join(varsMap.get(key), " "));
        }
        return result;
    }
    
//    public Map<String, String> getViewerCmdsMap(){
//        Map<String, String> result = new HashMap<String,String>();
//        for (String key: cmdsMap.keySet()){
//            result.put(key, join(cmdsMap.get(key), " "));
//        }
//        return result;
//    }
    
    private String join(String[] parts, String delim){
        StringBuilder result = new StringBuilder();
        int counter=0;
        
        for (String part: parts){
            counter++;
            result.append(part);
            if (delim != null && counter < parts.length) {
                result.append(delim);
            }        
        }
        return result.toString();
    }
}

@@ -18,16 +18,44 @@
 abase;%0A%0A
+import java.util.ArrayList;%0A
 import j
@@ -93,16 +93,57 @@
 til.Map;
+%0Aimport javafx.collections.FXCollections;
 %0A%0Apublic
@@ -209,32 +209,39 @@
 ce;%0A    private 
+static 
 Map%3CString, Stri
@@ -253,16 +253,50 @@
  varsMap
+ = new HashMap%3CString, String%5B%5D%3E()
 ;%0A    pr
@@ -301,16 +301,23 @@
 private 
+static 
 Map%3CStri
@@ -339,17 +339,363 @@
  cmdsMap
-;
+ = new HashMap%3CString, String%5B%5D%5B%5D%3E();%0A    private static List%3CTurtle%3E turtleList = new ArrayList%3CTurtle%3E();%0A    %0A    private static Map%3CString, String%3E viewerVarsMap = FXCollections.observableMap(new HashMap%3CString, String%3E());%0A    private static Map%3CString, String%3E viewerCmdsMap = FXCollections.observableMap(new HashMap%3CString, String%3E());%0A    
 %0A%0A    pr
@@ -969,24 +969,75 @@
 me, value);%0A
+        viewerVarsMap.put(name, join(value, %22 %22));%0A
     %7D%0A%0A    p
@@ -1185,24 +1185,51 @@
  mapValue);%0A
+        // add to cmdsMap;%0A
     %7D%0A%0A    p
@@ -1421,263 +1421,80 @@
 lic 
-Map%3CString, String%3E getViewerVarsMap()%7B%0A        Map%3CString, String%3E result = new HashMap%3CString,String%3E();%0A        for (String key: varsMap.keySet())%7B%0A            result.put(key, join(varsMap.get(key), %22 %22));%0A   
+Turtle getTurtle(int id)%7B%0A        return TurtleList.get(id);%0A
     
- 
 %7D%0A    
-  
-  return result;%0A    %7D%0A    %0A//
+%0A
     
@@ -1533,19 +1533,19 @@
 ewer
-Cmd
+Var
 sMap()%7B%0A
 //  
@@ -1544,55 +1544,58 @@
 ()%7B%0A
-//
       
-  Map%3CString, String%3E result = new Hash
+return viewerVarsMap;%0A    %7D%0A    %0A    public 
 Map%3C
@@ -1605,169 +1605,73 @@
 ing,
+ 
 String%3E
-();%0A//        for (String key: cmdsMap.keySet())%7B%0A//            result.put(key, join(cmdsMap.get(key), %22 %22));%0A//        %7D%0A//        return result;%0A//    %7D
+ getViewerCmdsMap()%7B%0A        return viewerCmdsMap;%0A    %7D%0A
 %0A   

make observable maps for front-end