import java.util.*;

public class Widgets {
    private Map<String, Widget> value = new HashMap<>();
    ScreenTemplates screenTemplates;
    CurScreen curScreen;
    SwingPresenter presenter;
    public Widgets(ScreenTemplates screenTemplates, SwingPresenter presenter) {
        this.screenTemplates = screenTemplates;
        this.presenter = presenter;
    }
    public Map<String, Widget> getValue() {
        return new HashMap<>(this.value);
    }
    public void setValue(Map<String, Object> value) {
        for (String key: value.keySet()) {
            String type = "";
            if (((Map<String, Object>) value.get(key)) != null) type = (String) ((Map<String, Object>) value.get(key)).get("type");
            String text = "";
            if (((Map<String, Object>) value.get(key)) != null) text = (String) ((Map<String, Object>) value.get(key)).get("text");
            boolean visible = true;
            if (((Map<String, Object>) value.get(key)) != null) visible = (boolean) ((Map<String, Object>) value.get(key)).get("visible");
            int x = 0;
            if (((Map<String, Object>) value.get(key)) != null) x = (int) ((Map<String, Object>) value.get(key)).get("x");
            int y = 0;
            if (((Map<String, Object>) value.get(key)) != null) y = (int) ((Map<String, Object>) value.get(key)).get("y");
            int width = 0;
            if (((Map<String, Object>) value.get(key)) != null) width = (int) ((Map<String, Object>) value.get(key)).get("width");
            int height = 0;
            if (((Map<String, Object>) value.get(key)) != null) height = (int) ((Map<String, Object>) value.get(key)).get("height");
            int state = 0;
            if (((Map<String, Object>) value.get(key)) != null) state = (int) ((Map<String, Object>) value.get(key)).get("state");
            Widget widget = new Widget(type, text, visible, x, y, width, height, state, null, screenTemplates, presenter);
            widget.setCurScreen(curScreen);
            this.value.put(key, widget);
        }
    }
    public Widget getWidget(String wid) {
        return this.value.get(wid);
    }
    public void addMovableButton(String text, int x, int y, int width, int height, String wid) {
        Widget widget = new Widget("button", text, true, x, y, width, height, 0, null, screenTemplates, presenter);
        widget.setCurScreen(curScreen);
        this.value.put(wid, widget);
    }
    public void addMovableTextInput(int x, int y, int width, int height, String wid) {
        Widget widget = new Widget("textInput", "", true, x, y, width, height, 0, null, screenTemplates, presenter);
        widget.setCurScreen(curScreen);
        this.value.put(wid, widget);
    }
    public void addMovableLabel(String text, int x, int y, int width, int height, String wid) {
        Widget widget = new Widget("label", text, true, x, y, width, height, 0, null, screenTemplates, presenter);
        widget.setCurScreen(curScreen);
        this.value.put(wid, widget);
    }
    public void addButton(String text, String wid) {
        Widget widget = new Widget("button", text, true, 0, 0, 0, 0, 0, null, screenTemplates, presenter);
        widget.setCurScreen(curScreen);
        this.value.put(wid, widget);
    }
    public void addTextInput(String wid) {
        Widget widget = new Widget("textInput", "", true, 0, 0, 0, 0, 0, null, screenTemplates, presenter);
        widget.setCurScreen(curScreen);
        this.value.put(wid, widget);
    }
    public void addLabel(String text, String wid) {
        Widget widget = new Widget("label", text, true, 0, 0, 0, 0, 0, null, screenTemplates, presenter);
        widget.setCurScreen(curScreen);
        this.value.put(wid, widget);
    }
    public void setCurScreen(CurScreen curScreen) {
        this.curScreen = curScreen;
        for (Widget widget: value.values()) {
            widget.setCurScreen(curScreen);
        }
    }
}