<?xml version="1.0" encoding="utf-8" ?>
<Page
    x:Class="ServerMonitorManager_Desktop.LinksPage"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="using:ServerMonitorManager_Desktop">

    <Grid RowSpacing="16">
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto" />
            <RowDefinition Height="Auto" />
            <RowDefinition Height="*" />
        </Grid.RowDefinitions>
        <Grid ColumnSpacing="12">
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="*" />
                <ColumnDefinition Width="Auto" />
            </Grid.ColumnDefinitions>
            <StackPanel Spacing="3">
                <TextBlock Style="{StaticResource TitleTextBlockStyle}" Text="Связи серверов" />
                <TextBlock Foreground="{ThemeResource TextFillColorSecondaryBrush}" Text="Направленные разрешения source → destination с ручным kill switch" />
            </StackPanel>
            <CommandBar Grid.Column="1" Background="Transparent" DefaultLabelPosition="Right">
                <AppBarButton AutomationProperties.Name="Обновить узлы и связи" Click="RefreshButton_Click" Icon="Refresh" Label="Обновить" />
                <AppBarButton AutomationProperties.Name="Перерегистрировать выбранный исходный Node" Click="ReenrollButton_Click" Label="Перерегистрировать">
                    <AppBarButton.Icon><FontIcon Glyph="&#xE8D7;" /></AppBarButton.Icon>
                </AppBarButton>
            </CommandBar>
        </Grid>

        <InfoBar
            x:Name="FirewallUnavailableInfo"
            Grid.Row="1"
            IsClosable="False"
            IsOpen="False"
            Message="Mesh firewall не загружен"
            Severity="Error" />

        <Grid x:Name="LinksWorkspace" Grid.Row="2" ColumnSpacing="20" RowSpacing="20">
            <Grid.ColumnDefinitions>
                <ColumnDefinition x:Name="EditorColumn" Width="*" />
                <ColumnDefinition x:Name="ListColumn" Width="0" />
            </Grid.ColumnDefinitions>
            <Grid.RowDefinitions>
                <RowDefinition Height="Auto" />
                <RowDefinition Height="*" />
            </Grid.RowDefinitions>

            <Border
                x:Name="LinkEditor"
                Padding="18"
                Background="{ThemeResource CardBackgroundFillColorDefaultBrush}"
                BorderBrush="{ThemeResource CardStrokeColorDefaultBrush}"
                BorderThickness="1"
                CornerRadius="8">
                <StackPanel Spacing="14">
                    <TextBlock Style="{StaticResource SubtitleTextBlockStyle}" Text="Управление доступом" />
                    <ComboBox x:Name="SourceNodeBox" AutomationProperties.Name="Источник связи" DisplayMemberPath="Label" Header="Источник доступа" ItemsSource="{x:Bind Nodes}" />
                    <ComboBox x:Name="TargetNodeBox" AutomationProperties.Name="Цель связи" DisplayMemberPath="Label" Header="Целевой сервер" ItemsSource="{x:Bind Nodes}" />
                    <Grid ColumnSpacing="10">
                        <Grid.ColumnDefinitions><ColumnDefinition Width="*" /><ColumnDefinition Width="*" /></Grid.ColumnDefinitions>
                        <ComboBox x:Name="ProtocolBox" Header="Протокол" SelectedIndex="0">
                            <ComboBoxItem Content="TCP" Tag="tcp" />
                            <ComboBoxItem Content="UDP" Tag="udp" />
                        </ComboBox>
                        <NumberBox x:Name="PortBox" Grid.Column="1" Header="Порт" Maximum="65535" Minimum="1" SpinButtonPlacementMode="Compact" Value="22" />
                    </Grid>
                    <NumberBox x:Name="TtlBox" Header="TTL, минут (0 — вручную)" Maximum="525600" Minimum="0" SpinButtonPlacementMode="Compact" Value="120" />
                    <Grid ColumnSpacing="10">
                        <Grid.ColumnDefinitions><ColumnDefinition Width="*" /><ColumnDefinition Width="*" /></Grid.ColumnDefinitions>
                        <Button HorizontalAlignment="Stretch" HorizontalContentAlignment="Center" Click="ConnectButton_Click" Content="Разрешить" Style="{StaticResource AccentButtonStyle}" />
                        <Button Grid.Column="1" HorizontalAlignment="Stretch" HorizontalContentAlignment="Center" Click="DisconnectButton_Click" Content="Отключить" />
                    </Grid>
                    <InfoBar IsClosable="False" IsOpen="True" Message="Обратное направление не включается автоматически. Отключение Link удаляет разрешение nftables на Hub." Severity="Informational" />
                </StackPanel>
            </Border>

            <Grid x:Name="LinksListPanel" Grid.Row="1" RowSpacing="10">
                <Grid.RowDefinitions><RowDefinition Height="Auto" /><RowDefinition Height="*" /></Grid.RowDefinitions>
                <Grid ColumnSpacing="12">
                    <Grid.ColumnDefinitions><ColumnDefinition Width="*" /><ColumnDefinition Width="Auto" /></Grid.ColumnDefinitions>
                    <StackPanel Spacing="2">
                        <TextBlock Style="{StaticResource SubtitleTextBlockStyle}" Text="Политики направлений" />
                        <TextBlock x:Name="LinksCountText" Foreground="{ThemeResource TextFillColorSecondaryBrush}" />
                    </StackPanel>
                    <ToggleSwitch x:Name="ShowHistoryToggle" Grid.Column="1" Header="Показать историю" Toggled="ShowHistoryToggle_Toggled" />
                </Grid>
                <ListView x:Name="LinksList" Grid.Row="1" AutomationProperties.Name="Список связей" ItemsSource="{x:Bind DisplayedLinks}" SelectionMode="Single">
                    <ListView.ItemTemplate>
                        <DataTemplate x:DataType="local:MeshLinkViewModel">
                            <Grid MinHeight="62" ColumnSpacing="12">
                                <Grid.ColumnDefinitions><ColumnDefinition Width="*" /><ColumnDefinition Width="Auto" /></Grid.ColumnDefinitions>
                                <StackPanel VerticalAlignment="Center">
                                    <TextBlock FontWeight="SemiBold" Text="{x:Bind Label}" TextWrapping="Wrap" />
                                    <TextBlock Text="{x:Bind DesiredStatusText}" TextWrapping="Wrap" />
                                    <TextBlock Text="{x:Bind ActualStatusText}" TextWrapping="Wrap" />
                                    <TextBlock Foreground="{ThemeResource TextFillColorSecondaryBrush}" Text="{x:Bind DriftText}" TextWrapping="Wrap" />
                                    <TextBlock Foreground="{ThemeResource TextFillColorSecondaryBrush}" Text="{x:Bind ErrorText}" TextWrapping="Wrap" />
                                    <TextBlock Foreground="{ThemeResource TextFillColorSecondaryBrush}" Text="{x:Bind VersionText}" />
                                </StackPanel>
                                <FontIcon Grid.Column="1" VerticalAlignment="Center" Glyph="&#xE72A;" />
                            </Grid>
                        </DataTemplate>
                    </ListView.ItemTemplate>
                </ListView>
            </Grid>

            <VisualStateManager.VisualStateGroups>
                <VisualStateGroup>
                    <VisualState x:Name="NarrowState" />
                    <VisualState x:Name="WideState">
                        <VisualState.StateTriggers><AdaptiveTrigger MinWindowWidth="820" /></VisualState.StateTriggers>
                        <VisualState.Setters>
                            <Setter Target="EditorColumn.Width" Value="360" />
                            <Setter Target="ListColumn.Width" Value="*" />
                            <Setter Target="LinksListPanel.(Grid.Row)" Value="0" />
                            <Setter Target="LinksListPanel.(Grid.Column)" Value="1" />
                        </VisualState.Setters>
                    </VisualState>
                </VisualStateGroup>
            </VisualStateManager.VisualStateGroups>
        </Grid>
    </Grid>
</Page>
