<?xml version="1.0" encoding="UTF-8"?>

<?import java.lang.*?>
<?import java.util.*?>
<?import javafx.scene.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>

<?import javafx.geometry.Insets?>
<?import tornadofx.control.Form?>
<?import tornadofx.control.Fieldset?>
<?import tornadofx.control.Field?>
<?import com.sparrowwallet.sparrow.control.UnlabeledToggleSwitch?>
<?import org.controlsfx.glyphfont.Glyph?>
<?import org.controlsfx.control.SegmentedButton?>
<?import com.sparrowwallet.sparrow.net.ServerType?>
<?import com.sparrowwallet.sparrow.net.CoreAuthType?>
<?import com.sparrowwallet.sparrow.control.HelpLabel?>
<?import javafx.collections.FXCollections?>
<?import com.sparrowwallet.sparrow.net.PublicElectrumServer?>
<?import com.sparrowwallet.sparrow.control.CopyableLabel?>
<GridPane hgap="10.0" vgap="10.0" stylesheets="@preferences.css, @../general.css" xmlns="http://javafx.com/javafx/10.0.2-internal" xmlns:fx="http://javafx.com/fxml/1" fx:controller="com.sparrowwallet.sparrow.preferences.ServerPreferencesController">
    <padding>
        <Insets left="25.0" right="25.0" top="25.0" />
    </padding>
    <columnConstraints>
        <ColumnConstraints percentWidth="100" />
    </columnConstraints>
    <rowConstraints>
        <RowConstraints />
    </rowConstraints>

    <Form GridPane.columnIndex="0" GridPane.rowIndex="0">
        <Fieldset inputGrow="SOMETIMES" text="Server">
            <Field text="Type:">
                <SegmentedButton fx:id="serverTypeSegmentedButton">
                    <toggleGroup>
                        <ToggleGroup fx:id="serverTypeToggleGroup" />
                    </toggleGroup>
                    <buttons>
                        <ToggleButton fx:id="publicElectrumToggle" text="Public Server" toggleGroup="$serverTypeToggleGroup">
                            <graphic>
                                <Glyph fontFamily="Font Awesome 5 Free Solid" fontSize="12" icon="TOGGLE_ON" styleClass="public-electrum" />
                            </graphic>
                            <userData>
                                <ServerType fx:constant="PUBLIC_ELECTRUM_SERVER"/>
                            </userData>
                        </ToggleButton>
                        <ToggleButton text="Bitcoin Core" toggleGroup="$serverTypeToggleGroup">
                            <graphic>
                                <Glyph fontFamily="Font Awesome 5 Free Solid" fontSize="12" icon="TOGGLE_ON" styleClass="bitcoin-core" />
                            </graphic>
                            <userData>
                                <ServerType fx:constant="BITCOIN_CORE"/>
                            </userData>
                        </ToggleButton>
                        <ToggleButton text="Private Electrum" toggleGroup="$serverTypeToggleGroup">
                            <graphic>
                                <Glyph fontFamily="Font Awesome 5 Free Solid" fontSize="12" icon="TOGGLE_ON" styleClass="private-electrum" />
                            </graphic>
                            <userData>
                                <ServerType fx:constant="ELECTRUM_SERVER"/>
                            </userData>
                        </ToggleButton>
                    </buttons>
                </SegmentedButton>
            </Field>
        </Fieldset>
    </Form>

    <Form fx:id="publicElectrumForm" GridPane.columnIndex="0" GridPane.rowIndex="1">
        <Fieldset inputGrow="SOMETIMES" text="Public Server">
            <Field text="">
                <Label alignment="CENTER" translateY="-1">
                    <graphic>
                        <Glyph fontFamily="Font Awesome 5 Free Solid" fontSize="12" icon="EXCLAMATION_TRIANGLE" styleClass="public-warning" />
                    </graphic>
                    <padding>
                        <Insets right="5" />
                    </padding>
                </Label>
                <CopyableLabel text="Warning!" />
            </Field>
            <Field text="">
                <CopyableLabel text="Using a public server means it can see your transactions."/>
            </Field>
            <Field text="URL:">
                <ComboBox fx:id="publicElectrumServer">
                    <items>
                        <FXCollections fx:factory="observableArrayList">
                            <PublicElectrumServer fx:constant="BLOCKSTREAM_INFO" />
                            <PublicElectrumServer fx:constant="ELECTRUM_BLOCKSTREAM_INFO" />
                            <PublicElectrumServer fx:constant="LUKECHILDS_CO" />
                            <PublicElectrumServer fx:constant="EMZY_DE" />
                            <PublicElectrumServer fx:constant="BITAROO_NET" />
                        </FXCollections>
                    </items>
                </ComboBox>
            </Field>
            <Field text="Use Proxy:">
                <UnlabeledToggleSwitch fx:id="publicUseProxy"/>
            </Field>
            <Field text="Proxy URL:">
                <TextField fx:id="publicProxyHost" />
                <TextField fx:id="publicProxyPort" maxWidth="100" />
            </Field>
        </Fieldset>
    </Form>

    <Form fx:id="coreForm" GridPane.columnIndex="0" GridPane.rowIndex="1">
        <Fieldset inputGrow="SOMETIMES" text="Bitcoin Core RPC">
            <Field text="URL:">
                <TextField fx:id="coreHost" promptText="e.g. 127.0.0.1"/>
                <TextField fx:id="corePort" promptText="e.g. 8332" maxWidth="100" />
            </Field>
            <Field text="Authentication:">
                <SegmentedButton>
                    <toggleGroup>
                        <ToggleGroup fx:id="coreAuthToggleGroup" />
                    </toggleGroup>
                    <buttons>
                        <ToggleButton text="Default" toggleGroup="$coreAuthToggleGroup">
                            <userData>
                                <CoreAuthType fx:constant="COOKIE"/>
                            </userData>
                        </ToggleButton>
                        <ToggleButton text="User / Pass" toggleGroup="$coreAuthToggleGroup">
                            <userData>
                                <CoreAuthType fx:constant="USERPASS"/>
                            </userData>
                        </ToggleButton>
                    </buttons>
                </SegmentedButton>
            </Field>
            <Field fx:id="coreDataDirField" text="Data Folder:" styleClass="label-button">
                <TextField fx:id="coreDataDir"/>
                <Button fx:id="coreDataDirSelect" maxWidth="35" minWidth="-Infinity" prefWidth="35">
                    <graphic>
                        <Glyph fontFamily="FontAwesome" icon="EDIT" fontSize="13" />
                    </graphic>
                </Button>
            </Field>
            <Field fx:id="coreUserPassField" text="User / Pass:" styleClass="label-button">
                <TextField fx:id="coreUser"/>
                <PasswordField fx:id="corePass"/>
            </Field>
            <Field text="Multi-Wallet:">
                <UnlabeledToggleSwitch fx:id="coreMultiWallet"/> <HelpLabel helpText="Creates a new Bitcoin Core wallet with the following name (recommended to avoid conflicts)" />
            </Field>
            <Field text="Wallet Name:" styleClass="label-button">
                <TextField fx:id="coreWallet"/>
            </Field>
        </Fieldset>
    </Form>

    <Form fx:id="electrumForm" GridPane.columnIndex="0" GridPane.rowIndex="1">
        <Fieldset inputGrow="SOMETIMES" text="Private Electrum Server">
            <Field text="URL:">
                <TextField fx:id="electrumHost" promptText="e.g. 127.0.0.1 or Tor hostname (.onion)"/>
                <TextField fx:id="electrumPort" promptText="e.g. 50002" maxWidth="100" />
            </Field>
            <Field text="Use SSL:">
                <UnlabeledToggleSwitch fx:id="electrumUseSsl"/>
            </Field>
            <Field text="Certificate:" styleClass="label-button">
                <TextField fx:id="electrumCertificate" promptText="Optional server certificate (.crt)"/>
                <Button fx:id="electrumCertificateSelect" maxWidth="35" minWidth="-Infinity" prefWidth="35">
                    <graphic>
                        <Glyph fontFamily="FontAwesome" icon="EDIT" fontSize="13" />
                    </graphic>
                </Button>
            </Field>
            <Field text="Use Proxy:">
                <UnlabeledToggleSwitch fx:id="useProxy"/>
            </Field>
            <Field text="Proxy URL:">
                <TextField fx:id="proxyHost" />
                <TextField fx:id="proxyPort" maxWidth="100" />
            </Field>
        </Fieldset>
    </Form>

    <StackPane GridPane.columnIndex="0" GridPane.rowIndex="2">
        <Button fx:id="testConnection" graphicTextGap="5" text="Test Connection">
            <graphic>
                <Glyph fontFamily="FontAwesome" icon="QUESTION_CIRCLE" prefWidth="13" />
            </graphic>
        </Button>
        <Button fx:id="editConnection" graphicTextGap="5" text="Edit Existing Connection">
            <graphic>
                <Glyph fontFamily="FontAwesome" icon="EDIT" prefWidth="15" />
            </graphic>
        </Button>
    </StackPane>

    <StackPane GridPane.columnIndex="0" GridPane.rowIndex="3">
        <padding>
            <Insets top="10.0" bottom="20.0"/>
        </padding>
        <TextArea fx:id="testResults" editable="false" wrapText="true"/>
    </StackPane>

</GridPane>
