package market.guess.ui.desktop.controllers; import java.math.BigDecimal; import java.util.List; import java.util.function.BiConsumer; import java.util.function.Consumer; import javafx.fxml.FXML; import javafx.scene.control.Button; import javafx.scene.control.CheckBox; import javafx.scene.control.ComboBox; import javafx.scene.control.Label; import javafx.scene.control.TextArea; import javafx.scene.control.TextField; import market.guess.model.event.CommissionTiming; import market.guess.model.event.CreateEventRequest; import market.guess.model.event.MechanismType; import market.guess.ui.desktop.util.Format; import market.guess.ui.desktop.util.Views; public class CreateEventDialogController { private Runnable onCancel; private Consumer onCreateRequest; private BiConsumer onCreate; private MechanismType selectedType = MechanismType.LMSR; @FXML private TextField nameField; @FXML private TextArea descArea; @FXML private Button lmsrBtn; @FXML private Button orderBookBtn; @FXML private ComboBox feeCollectionCombo; @FXML private TextField feePercentField; @FXML private Label paramLabel; @FXML private TextField paramField; @FXML private TextField opt1Field; @FXML private TextField opt2Field; @FXML private CheckBox mintingCheckBox; @FXML private Label infoNoteLabel; @FXML private void initialize() { feeCollectionCombo.getItems().setAll("On purchase", "On close"); feeCollectionCombo.setValue("On purchase"); updateTypeSelection(); } public void setOnCancel(Runnable onCancel) { this.onCancel = onCancel; } public void setOnCreate(Consumer onCreateRequest) { this.onCreateRequest = onCreateRequest; } public void setOnCreate(BiConsumer onCreate) { this.onCreate = onCreate; } public void reset() { nameField.setText("Will the summit happen before Q4?"); descArea.setText( "Resolves YES if a joint summit is publicly confirmed and held before October 1st."); feeCollectionCombo.setValue("On purchase"); feePercentField.setText("5"); opt1Field.setText("YES"); opt2Field.setText("NO"); selectedType = MechanismType.LMSR; updateTypeSelection(); } private void updateTypeSelection() { Views.setSelected(lmsrBtn, selectedType == MechanismType.LMSR); Views.setSelected(orderBookBtn, selectedType == MechanismType.ORDER_BOOK); boolean isLmsr = selectedType == MechanismType.LMSR; paramLabel.setText(isLmsr ? "Liquidity b (integer)" : "Base value d ($)"); paramField.setText(isLmsr ? "100" : "1.00"); mintingCheckBox.setVisible(!isLmsr); String infoText = isLmsr ? "Opening this event will move " + Format.money(BigDecimal.valueOf(100 * Math.log(2))) + " of subsidy from your account into the contract account (b  ln 2)." : "Opening this event will buy the initial share inventory from your account into the" + " contract account."; infoNoteLabel.setText(infoText); } @FXML private void handleSelectLmsr() { selectedType = MechanismType.LMSR; updateTypeSelection(); } @FXML private void handleSelectOrderBook() { selectedType = MechanismType.ORDER_BOOK; updateTypeSelection(); } @FXML private void handleCancel() { if (onCancel != null) { onCancel.run(); } } @FXML private void handleCreate() { String name = nameField.getText() != null ? nameField.getText().trim() : ""; String desc = descArea.getText() != null ? descArea.getText().trim() : ""; MechanismType type = selectedType; CommissionTiming timing = "On close".equalsIgnoreCase(feeCollectionCombo.getValue()) ? CommissionTiming.ON_CLOSE : CommissionTiming.ON_PURCHASE; int fee = 5; try { fee = Integer.parseInt(feePercentField.getText().trim()); if (fee < 0) fee = 0; if (fee > 90) fee = 90; } catch (Exception ignored) { } Integer b = null; BigDecimal d = null; if (type == MechanismType.LMSR) { try { b = Integer.parseInt(paramField.getText().trim()); } catch (Exception ignored) { b = 100; } } else { try { d = new BigDecimal(paramField.getText().trim()); } catch (Exception ignored) { d = BigDecimal.ONE; } } String opt1 = opt1Field.getText() != null && !opt1Field.getText().isBlank() ? opt1Field.getText().trim() : "YES"; String opt2 = opt2Field.getText() != null && !opt2Field.getText().isBlank() ? opt2Field.getText().trim() : "NO"; boolean minting = mintingCheckBox.isSelected(); var request = new CreateEventRequest( name, desc, type, fee, timing, null, b, d, List.of(opt1, opt2), minting); if (onCreateRequest != null) { onCreateRequest.accept(request); } else if (onCreate != null) { onCreate.accept(name, type); } } }