import javafx.application.Platform;
import javafx.beans.property.SimpleStringProperty;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.embed.swing.JFXPanel;
import javafx.geometry.Insets;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.Tab;
import javafx.scene.control.TabPane;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.control.TextField;
import javafx.scene.control.cell.PropertyValueFactory;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.VBox;
import javax.swing.JFrame;
import javax.swing.SwingUtilities;
import java.awt.Dimension;

/** Disposable JavaFX bridge integration harness. */
public class FxTestApp {
    public static final class Row {
        private final SimpleStringProperty code;
        private final SimpleStringProperty amount;
        Row(String code, String amount) {
            this.code = new SimpleStringProperty(code);
            this.amount = new SimpleStringProperty(amount);
        }
        public String getCode() { return code.get(); }
        public String getAmount() { return amount.get(); }
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                JFrame frame = new JFrame("FxTestApp - disposable JavaFX bridge test harness");
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.setSize(650, 600);
                final JFXPanel fxPanel = new JFXPanel();
                fxPanel.setPreferredSize(new Dimension(630, 580));
                frame.getContentPane().add(fxPanel);
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
                System.out.println("[FxTestApp] Swing window shown, JFXPanel added, awaiting FX scene init...");
                Platform.runLater(new Runnable() {
                    public void run() { initFxScene(fxPanel); }
                });
            }
        });
    }

    private static void initFxScene(JFXPanel fxPanel) {
        final Label statusLabel = new Label("status: not clicked yet");
        statusLabel.setId("StatusLabel");

        final TextField textField = new TextField("initial-value");
        textField.setId("MyFxTextField");
        textField.textProperty().addListener((obs, oldV, newV) ->
            System.out.println("[FxTestApp] FX FIELD TEXT CHANGED to = '" + newV + "'"));

        final SimpleStringProperty boundValue = new SimpleStringProperty("bound-value");
        final TextField boundField = new TextField();
        boundField.setId("BoundFxTextField");
        boundField.textProperty().bind(boundValue);

        final Button clickButton = new Button("MyFxClickButton");
        clickButton.setId("MyFxClickButton");
        clickButton.setOnAction(e -> {
            System.out.println("[FxTestApp] FX BUTTON CLICKED. current field text = '" + textField.getText() + "'");
            statusLabel.setText("status: clicked!");
        });

        Tab tabOne = new Tab("TabOne");
        tabOne.setClosable(false);
        Tab tabTwo = new Tab("TabTwo");
        tabTwo.setClosable(false);
        final TabPane tabPane = new TabPane(tabOne, tabTwo);
        tabPane.setId("MyFxTabPane");

        final TableView<Row> table = new TableView<Row>();
        table.setId("AdjustmentTable");
        table.setEditable(true);
        TableColumn<Row, String> code = new TableColumn<Row, String>("Code");
        code.setCellValueFactory(new PropertyValueFactory<Row, String>("code"));
        TableColumn<Row, String> amount = new TableColumn<Row, String>("Amount");
        amount.setCellValueFactory(new PropertyValueFactory<Row, String>("amount"));
        table.getColumns().addAll(code, amount);
        ObservableList<Row> rows = FXCollections.observableArrayList(
            new Row("100", "-59.00"), new Row("200", "12.50"));
        table.setItems(rows);
        table.setOnMouseClicked((MouseEvent e) -> {
            if (e.getClickCount() == 2) {
                int index = table.getSelectionModel().getSelectedIndex();
                statusLabel.setText("status: table doubleclick row=" + index);
                System.out.println("[FxTestApp] TABLE DOUBLECLICK row=" + index);
            }
        });

        VBox root = new VBox(10);
        root.setPadding(new Insets(15));
        root.getChildren().addAll(clickButton, textField, boundField, statusLabel, tabPane, table);
        Scene scene = new Scene(root, 630, 580);
        fxPanel.setScene(scene);
        System.out.println("[FxTestApp] FX scene built and attached to JFXPanel. ready.");
    }
}
