<Page
    x:Class="MQTTnet.TestApp.UniversalWindows.MainPage"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    xmlns:server="using:MQTTnet.Server"
    xmlns:status="using:MQTTnet.Server.Status"
    d:DesignHeight="800"
    d:DesignWidth="800"
    mc:Ignorable="d">

    <Grid Margin="10">
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto" />
            <RowDefinition Height="*" />
        </Grid.RowDefinitions>

        <Pivot Grid.Row="0">
            <PivotItem Header="Connection">
                <StackPanel Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
                    <Grid>
                        <Grid.Resources>
                            <Style TargetType="TextBlock">
                                <Setter Property="VerticalAlignment" Value="Center"></Setter>
                                <Setter Property="Margin" Value="0, 0, 10, 0"></Setter>
                            </Style>
                        </Grid.Resources>
                        <Grid.ColumnDefinitions>
                            <ColumnDefinition Width="Auto"></ColumnDefinition>
                            <ColumnDefinition Width="*"></ColumnDefinition>
                        </Grid.ColumnDefinitions>
                        <Grid.RowDefinitions>
                            <RowDefinition></RowDefinition>
                            <RowDefinition></RowDefinition>
                            <RowDefinition></RowDefinition>
                            <RowDefinition></RowDefinition>
                            <RowDefinition></RowDefinition>
                            <RowDefinition></RowDefinition>
                            <RowDefinition></RowDefinition>
                            <RowDefinition></RowDefinition>
                        </Grid.RowDefinitions>

                        <TextBlock Grid.Column="0" Grid.Row="0">Server:</TextBlock>
                        <TextBox Grid.Column="1" Grid.Row="0" x:Name="Server" Text="localhost"></TextBox>

                        <TextBlock Grid.Column="0" Grid.Row="1">Port:</TextBlock>
                        <TextBox Grid.Column="1" Grid.Row="1" x:Name="Port" Text="1883"></TextBox>

                        <TextBlock Grid.Column="0" Grid.Row="2">Username:</TextBlock>
                        <TextBox Grid.Column="1" Grid.Row="2" x:Name="User"></TextBox>

                        <TextBlock Grid.Column="0" Grid.Row="3">Password:</TextBlock>
                        <TextBox Grid.Column="1" Grid.Row="3" x:Name="Password"></TextBox>

                        <TextBlock Grid.Column="0" Grid.Row="4">Client ID:</TextBlock>
                        <TextBox Grid.Column="1" Grid.Row="4" x:Name="ClientId"></TextBox>

                        <TextBlock Grid.Column="0" Grid.Row="5">Keep Alive interval:</TextBlock>
                        <TextBox Grid.Column="1" Grid.Row="5" x:Name="KeepAliveInterval" Text="5"></TextBox>

                        <TextBlock Grid.Column="0" Grid.Row="6">Transport:</TextBlock>
                        <StackPanel Grid.Column="1" Grid.Row="6" Orientation="Horizontal">
                            <RadioButton x:Name="UseTcp" IsChecked="True" GroupName="transport">TCP</RadioButton>
                            <RadioButton x:Name="UseWs" GroupName="transport">WS</RadioButton>
                            <CheckBox x:Name="UseTls">Use TLS</CheckBox>
                        </StackPanel>

                        <TextBlock Grid.Column="0" Grid.Row="7">Protocol:</TextBlock>
                        <StackPanel Grid.Column="1" Grid.Row="7" Orientation="Horizontal">
                            <RadioButton x:Name="UseMqtt500" GroupName="protocol">5.0.0</RadioButton>
                            <RadioButton x:Name="UseMqtt311" GroupName="protocol" IsChecked="True">3.1.1</RadioButton>
                            <RadioButton x:Name="UseMqtt310" GroupName="protocol">3.1.0</RadioButton>
                        </StackPanel>
                    </Grid>
                   
                    <CheckBox x:Name="CleanSession" IsChecked="True">Clean session</CheckBox>
                    <CheckBox x:Name="UseManagedClient" IsChecked="False">Use managed client</CheckBox>

                    <StackPanel Orientation="Horizontal">
                        <Button Click="Connect" Width="120" Margin="0,0,10,0">Connect</Button>
                        <Button Click="Disconnect" Width="120">Disconnect</Button>
                    </StackPanel>
                </StackPanel>
            </PivotItem>

            <PivotItem Header="Publish">
                <StackPanel Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
                    <Grid>
                        <Grid.Resources>
                            <Style TargetType="TextBlock">
                                <Setter Property="VerticalAlignment" Value="Center"></Setter>
                                <Setter Property="Margin" Value="0, 0, 10, 0"></Setter>
                            </Style>
                        </Grid.Resources>
                        <Grid.ColumnDefinitions>
                            <ColumnDefinition Width="Auto"></ColumnDefinition>
                            <ColumnDefinition Width="*"></ColumnDefinition>
                        </Grid.ColumnDefinitions>
                        <Grid.RowDefinitions>
                            <RowDefinition></RowDefinition>
                            <RowDefinition></RowDefinition>
                            <RowDefinition></RowDefinition>
                            <RowDefinition></RowDefinition>
                            <RowDefinition></RowDefinition>
                            <RowDefinition></RowDefinition>
                            <RowDefinition></RowDefinition>
                        </Grid.RowDefinitions>

                        <TextBlock Grid.Column="0" Grid.Row="0">Topic:</TextBlock>
                        <TextBox Grid.Column="1" Grid.Row="0" x:Name="Topic"></TextBox>

                        <TextBlock Grid.Column="0" Grid.Row="1">Content type:</TextBlock>
                        <TextBox Grid.Column="1" Grid.Row="1" x:Name="ContentType"></TextBox>

                        <TextBlock Grid.Column="0" Grid.Row="2">Payload:</TextBlock>
                        <TextBox Grid.Column="1" Grid.Row="2" x:Name="Payload"></TextBox>

                        <TextBlock Grid.Column="0" Grid.Row="3">Payload format:</TextBlock>
                        <StackPanel Grid.Column="1" Grid.Row="3" Orientation="Horizontal">
                            <RadioButton Margin="0,0,10,0"  x:Name="PlainText" IsChecked="True" GroupName="payload">Plain text</RadioButton>
                            <RadioButton Margin="0,0,10,0"  x:Name="Base64" GroupName="payload">Base64 string</RadioButton>
                        </StackPanel>

                        <TextBlock Grid.Column="0" Grid.Row="4">QoS level:</TextBlock>
                        <StackPanel Grid.Column="1" Grid.Row="4" Orientation="Horizontal">
                            <RadioButton Margin="0,0,10,0" IsChecked="True" GroupName="qos">0 (At most once)</RadioButton>
                            <RadioButton Margin="0,0,10,0" x:Name="QoS1" GroupName="qos">1 (At least once)</RadioButton>
                            <RadioButton Margin="0,0,10,0" x:Name="QoS2" GroupName="qos">2 (Exactly once)</RadioButton>
                        </StackPanel>

                        <TextBlock Grid.Column="0" Grid.Row="5">Response topic:</TextBlock>
                        <TextBox Grid.Column="1" Grid.Row="5" x:Name="ResponseTopic"></TextBox>
                    </Grid>

                    <CheckBox x:Name="Retain">Retain</CheckBox>

                    <Button Click="Publish" Width="120">Publish</Button>
                </StackPanel>
            </PivotItem>

            <PivotItem Header="Execute RPC">
                <StackPanel Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
                    <TextBlock>Method:</TextBlock>
                    <TextBox x:Name="RpcMethod"></TextBox>

                    <TextBlock>Payload:</TextBlock>
                    <TextBox x:Name="RpcPayload"></TextBox>
                    <StackPanel Orientation="Horizontal">
                        <RadioButton x:Name="RpcText" IsChecked="True" GroupName="payload">Text</RadioButton>
                        <RadioButton x:Name="RpcBase64" GroupName="payload">Base64</RadioButton>
                    </StackPanel>

                    <TextBlock>QoS:</TextBlock>
                    <StackPanel Orientation="Horizontal">
                        <RadioButton Margin="0,0,10,0" x:Name="RpcQoS0" GroupName="qos" IsChecked="True">0 (At most once)</RadioButton>
                        <RadioButton Margin="0,0,10,0" x:Name="RpcQoS1" GroupName="qos">1 (At least once)</RadioButton>
                        <RadioButton Margin="0,0,10,0" x:Name="RpcQoS2" GroupName="qos">2 (Exactly once)</RadioButton>
                    </StackPanel>

                    <TextBlock>Responses:</TextBlock>
                    <ListBox MinHeight="50" MaxHeight="250" x:Name="RpcResponses" Margin="0,0,0,10">
                        <ListBox.ItemTemplate>
                            <DataTemplate>
                                <ContentPresenter Content="{Binding}" FontFamily="Consolas" FontSize="12"></ContentPresenter>
                            </DataTemplate>
                        </ListBox.ItemTemplate>
                    </ListBox>

                    <StackPanel Orientation="Horizontal">
                        <Button Click="ExecuteRpc" Width="120" Margin="0,0,10,0">Execute</Button>
                        <Button Click="ClearRpcResponses" Width="200">Clear responses</Button>
                    </StackPanel>
                </StackPanel>
            </PivotItem>

            <PivotItem Header="Subscribe">
                <StackPanel Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
                    <TextBlock>Topic:</TextBlock>
                    <TextBox x:Name="SubscribeTopic"></TextBox>

                    <TextBlock>QoS:</TextBlock>
                    <StackPanel Orientation="Horizontal">
                        <RadioButton Margin="0,0,10,0" IsChecked="True" GroupName="sqos">0 (At most once)</RadioButton>
                        <RadioButton Margin="0,0,10,0" x:Name="SubscribeQoS1" GroupName="sqos">1 (At least once)</RadioButton>
                        <RadioButton Margin="0,0,10,0" x:Name="SubscribeQoS2" GroupName="sqos">2 (Exactly once)</RadioButton>
                    </StackPanel>

                    <TextBlock>Received messages:</TextBlock>
                    <CheckBox x:Name="AddReceivedMessagesToList" IsChecked="True">Add received messages to list</CheckBox>
                    <ListBox MinHeight="50" MaxHeight="250" x:Name="ReceivedMessages" Margin="0,0,0,10">
                        <ListBox.ItemTemplate>
                            <DataTemplate>
                                <ContentPresenter Content="{Binding}" FontFamily="Consolas" FontSize="12"></ContentPresenter>
                            </DataTemplate>
                        </ListBox.ItemTemplate>
                    </ListBox>

                    <StackPanel Orientation="Horizontal">
                        <Button Click="Subscribe" Width="120" Margin="0,0,10,0">Subscribe</Button>
                        <Button Click="Unsubscribe" Width="120" Margin="0,0,10,0">Unsubscribe</Button>
                        <Button Click="ClearReceivedMessages" Width="200">Clear received messages</Button>
                    </StackPanel>
                </StackPanel>
            </PivotItem>

            <PivotItem Header="Server">
                <StackPanel Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
                    <TextBlock>Port:</TextBlock>
                    <TextBox x:Name="ServerPort" Text="1883"></TextBox>

                    <CheckBox x:Name="ServerPersistRetainedMessages" IsChecked="True">Persist retained messages in JSON format</CheckBox>
                    <CheckBox x:Name="ServerClearRetainedMessages">Clear previously retained messages on startup</CheckBox>
                    <CheckBox x:Name="ServerAllowPersistentSessions">Allow persistent sessions</CheckBox>

                    <StackPanel Orientation="Horizontal">
                        <Button Width="120" Margin="0,0,10,0" Click="StartServer">Start</Button>
                        <Button Width="120" Margin="0,0,10,0" Click="StopServer">Stop</Button>
                    </StackPanel>
                </StackPanel>
            </PivotItem>

            <PivotItem Header="Sessions">
                <StackPanel Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
                    <StackPanel Orientation="Horizontal">
                        <Button Width="120" Margin="0,0,10,0" Click="RefreshSessions">Refresh</Button>
                        <Button Width="120" Margin="0,0,10,0" Click="ClearSessions">Clear</Button>
                    </StackPanel>

                    <ListView ItemsSource="{Binding}" x:Name="ListViewSessions">
                        <ListView.ItemTemplate>
                            <DataTemplate x:DataType="status:IMqttClientStatus">
                                <StackPanel Orientation="Horizontal">
                                    <StackPanel.Resources>
                                        <Style TargetType="TextBlock">
                                            <Setter Property="Margin" Value="0,0,10,0"></Setter>
                                        </Style>
                                    </StackPanel.Resources>
                                    <TextBlock Text="{Binding ClientId}"></TextBlock>
                                    <TextBlock Text="{Binding Endpoint}"></TextBlock>
                                    <TextBlock Text="{Binding LastPacketReceived}"></TextBlock>
                                    <TextBlock Text="{Binding LastNonKeepAlivePacketReceived}"></TextBlock>
                                    <TextBlock Text="{Binding ProtocolVersion}"></TextBlock>
                                    <TextBlock Text="{Binding Session}"></TextBlock>
                                </StackPanel>
                            </DataTemplate>
                        </ListView.ItemTemplate>
                    </ListView>

                </StackPanel>
            </PivotItem>
            <PivotItem Header="Log">
                <StackPanel Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
                    <Button Click="ClearLog" Width="120">Clear</Button>
                </StackPanel>
            </PivotItem>
        </Pivot>

        <ScrollViewer Grid.Row="1"  Margin="12,10,12,0"  Background="Black" Foreground="WhiteSmoke" FontFamily="Consolas" HorizontalScrollMode="Enabled" VerticalScrollMode="Enabled" HorizontalScrollBarVisibility="Visible" VerticalScrollBarVisibility="Visible">
            <TextBlock x:Name="Trace" FontSize="11"></TextBlock>
        </ScrollViewer>
    </Grid>

</Page>

