package com.redspr.redquerybuilder.core.shared.meta;

import java.io.Serializable;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import com.google.gwt.user.client.rpc.IsSerializable;

// XXX custom editors in Config?
public class Editor implements HasStyleName, Serializable, IsSerializable {
    public static class TextEditor extends Editor {
        @Override
        public Object getDefault() {
            return "";
        }
    };
    public static class DateEditor extends Editor {
        public static final String FORMAT = "format";
        @Override
        public Object getDefault() {
            return new Date();
        }
    };
    public static class BooleanEditor extends Editor {
        @Override
        public Object getDefault() {
            return Boolean.FALSE;
        }
    };

    public static class SelectEditor extends Editor {

    }

    public static class NumberEditor extends Editor {
    }


    // XXX - rubbish, only used by JSON?
    private static final Map<String, Editor> editorByName = new HashMap<String, Editor>();

    private static Editor valueOf2(String name) {
        if ("STRING".equals(name) || "TEXT".equals(name)) {
            return new TextEditor();
        } else if ("DATE".equals(name)) {
            return new DateEditor();
        } else if ("SUGGEST".equals(name)) {
            return new SuggestEditor();
        } else if ("SELECT".equals(name)) {
            return new SelectEditor();
        } else if ("NUMBER".equals(name)) {
            return new NumberEditor();
        } else {
            throw new RuntimeException("No editor for " + name);
        }
    }

    public static Editor valueOf(String name) {
        Editor e = editorByName.get(name);
        if (e == null) {
            e = valueOf2(name);
            editorByName.put(name, e);
        }
        return e;
    }

    // XXX map mush
    private final Map<String, Object> attributes = new HashMap<String, Object>();

    private String styleName;

    public Object getDefault() {
        return null;
    }

    public void setAttribute(String name, Object value) {
        attributes.put(name, value);
    }

    public Object getAttribute(String name) {
        return attributes.get(name);
    }

    public String getStyleName() {
        return styleName;
    }

    @Override
    public void setStyleName(String p) {
        this.styleName = p;
    }
}

@@ -80,31 +80,8 @@
 le;%0A
-import java.util.Date;%0A
 impo
@@ -102,16 +102,16 @@
 ashMap;%0A
+
 import j
@@ -426,33 +426,33 @@
 %0A        %7D%0A    %7D
-;
+%0A
 %0A    public stat
@@ -550,106 +550,10 @@
     
-    @Override%0A        public Object getDefault() %7B%0A            return new Date();%0A        %7D%0A    %7D;
+%7D%0A
 %0A   
@@ -707,14 +707,13 @@
   %7D%0A
+
     %7D
-;
 %0A%0A  

Remove default value for date.