package org.ntlab.rdl;

import org.ntlab.rdl.applicationruntime.ApplicationEventReceiver;
import org.ntlab.rdl.applicationruntime.ApplicationRuntime;
import org.ntlab.rdl.applicationruntime.WidgetState;
import org.ntlab.rdl.swingadapter.SwingAdapter;

public class Main {
    public static void main(String[] args) {
        // Runtime core
        ApplicationRuntime runtime = new ApplicationRuntime();

        // Adapter for Swing
        SwingAdapter swingAdapter = new SwingAdapter(runtime.getUIState());

        // Connect the adapter to runtime core
        runtime.setScreenConfigurationSender(swingAdapter);
        runtime.setWidgetPropertySender(swingAdapter);

        // Create a window
        runtime.getUIState().createWindow("メモアプリ", 500, 300);

        // Create display screen
        runtime.getUIDefinition().createScreen("メモ表示画面");
        runtime.getUIDefinition().addWidget("メモ表示画面", "label", new WidgetState("label", "メモ:", 50, 50, 50, 30));
        runtime.getUIDefinition().addWidget("メモ表示画面", "memo", new WidgetState("label", "", 120, 50, 300, 30));
        runtime.getUIDefinition().addWidget("メモ表示画面", "editButton", new WidgetState("button", "編集", 330, 170, 60, 30));

        // Create edit screen
        runtime.getUIDefinition().createScreen("メモ編集画面");
        runtime.getUIDefinition().addWidget("メモ編集画面", "label", new WidgetState("label", "メモ:", 50, 50, 50, 30));
        runtime.getUIDefinition().addWidget("メモ編集画面", "memoInput", new WidgetState("textInput", "", 120, 50, 300, 30));
        runtime.getUIDefinition().addWidget("メモ編集画面", "okButton", new WidgetState("button", "OK", 250, 170, 60, 30));
        runtime.getUIDefinition().addWidget("メモ編集画面", "canselButton", new WidgetState("button", "キャンセル", 330, 170, 120, 30));

        // Setup Event Handler
        runtime.setApplicationEventReceiver(new ApplicationEventReceiver() {
            @Override
            public void onWidgetStateChanged(String screenId, String widgetId, String state) {
                if (screenId.equals("メモ表示画面") && widgetId.equals("editButton") && state.equals("clicked")) {
                    runtime.getUIState().setWidgetText("メモ編集画面", "memoInput", runtime.getUIState().getCurWidgetText("memo"));
                    runtime.getUIState().changeScreen("メモ編集画面");
                } else if (screenId.equals("メモ編集画面") && widgetId.equals("okButton") && state.equals("clicked")) {
                    runtime.getUIState().setWidgetText("メモ表示画面", "memo", runtime.getUIState().getCurWidgetText("memoInput"));
                    runtime.getUIState().changeScreen("メモ表示画面");
                } else if (screenId.equals("メモ編集画面") && widgetId.equals("canselButton") && state.equals("clicked")) {
                    runtime.getUIState().changeScreen("メモ表示画面");
                }
            }
        });

        runtime.getUIState().changeScreen("メモ表示画面");
    }
}
