<UserControl
    x:Class="SHH.CameraDashboard.CameraImageSubscription"
    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:local="clr-namespace:SHH.CameraDashboard"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    d:DesignHeight="600"
    d:DesignWidth="900"
    mc:Ignorable="d">

    <UserControl.Resources>
        <BooleanToVisibilityConverter x:Key="BoolToVis" />
        <local:SubscriptionTypeConverter x:Key="TypeConverter" />
    </UserControl.Resources>
    <Grid Margin="20">
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto" />
            <RowDefinition Height="*" />
            <RowDefinition Height="20" />
            <RowDefinition Height="Auto" />
        </Grid.RowDefinitions>

        <StackPanel
            Grid.Row="0"
            Margin="0,0,0,15"
            Orientation="Horizontal">
            <TextBlock
                Margin="0,0,8,0"
                VerticalAlignment="Center"
                FontSize="18"
                Text="📡" />
            <TextBlock
                VerticalAlignment="Center"
                FontSize="18"
                FontWeight="SemiBold"
                Foreground="{DynamicResource Brush.Text.Primary}"
                Text="订阅管理" />
            <TextBlock
                Margin="5,2,0,0"
                VerticalAlignment="Center"
                FontSize="14"
                Foreground="{DynamicResource Brush.Text.Secondary}"
                Text=" (实时分发控制)" />
        </StackPanel>

        <Border
            Grid.Row="1"
            Background="{DynamicResource Brush.Bg.Panel}"
            BorderBrush="{DynamicResource Brush.Border}"
            BorderThickness="1"
            CornerRadius="{StaticResource Radius.Normal}">
            <DataGrid
                BorderThickness="0"
                IsReadOnly="True"
                ItemsSource="{Binding Subscriptions}"
                SelectedItem="{Binding SelectedSubscription}"
                SelectionMode="Single">
                <DataGrid.Columns>

                    <DataGridTextColumn
                        Width="80"
                        Binding="{Binding AppId}"
                        Header="App ID">
                        <DataGridTextColumn.ElementStyle>
                            <Style TargetType="TextBlock">
                                <Setter Property="VerticalAlignment" Value="Center" />
                                <Setter Property="Foreground" Value="{DynamicResource Brush.Text.Primary}" />
                                <Setter Property="FontWeight" Value="SemiBold" />
                            </Style>
                        </DataGridTextColumn.ElementStyle>
                    </DataGridTextColumn>

                    <DataGridTextColumn
                        Width="125"
                        Binding="{Binding Type, Converter={StaticResource TypeConverter}}"
                        Header="类型">
                        <DataGridTextColumn.ElementStyle>
                            <Style TargetType="TextBlock">
                                <Setter Property="VerticalAlignment" Value="Center" />
                                <Setter Property="Margin" Value="5,0" />
                            </Style>
                        </DataGridTextColumn.ElementStyle>
                    </DataGridTextColumn>

                    <DataGridTemplateColumn Width="70" Header="目标帧率">
                        <DataGridTemplateColumn.CellTemplate>
                            <DataTemplate>
                                <Border
                                    Padding="5,1"
                                    HorizontalAlignment="Center"
                                    Background="{DynamicResource Brush.Bg.L3}"
                                    CornerRadius="3"
                                    ToolTip="配置的期望帧率">
                                    <TextBlock
                                        FontSize="11"
                                        Foreground="{DynamicResource Brush.Text.Inverse}"
                                        Text="{Binding DisplayFps, StringFormat={}{0} FPS}" />
                                </Border>
                            </DataTemplate>
                        </DataGridTemplateColumn.CellTemplate>
                    </DataGridTemplateColumn>

                    <DataGridTemplateColumn Width="70" Header="实际帧率">
                        <DataGridTemplateColumn.CellTemplate>
                            <DataTemplate>
                                <Border
                                    Padding="5,1"
                                    HorizontalAlignment="Center"
                                    CornerRadius="3">
                                    <TextBlock
                                        FontSize="11"
                                        FontWeight="Bold"
                                        Foreground="{DynamicResource Brush.State.Success}"
                                        Text="{Binding RealFps, StringFormat={}{0:F1} FPS}" />
                                </Border>
                            </DataTemplate>
                        </DataGridTemplateColumn.CellTemplate>
                    </DataGridTemplateColumn>

                    <DataGridTextColumn
                        Width="2*"
                        Binding="{Binding Memo}"
                        Header="备注">
                        <DataGridTextColumn.ElementStyle>
                            <Style TargetType="TextBlock">
                                <Setter Property="VerticalAlignment" Value="Center" />
                                <Setter Property="TextTrimming" Value="CharacterEllipsis" />
                                <Setter Property="ToolTip" Value="{Binding Memo}" />
                                <Setter Property="Foreground" Value="{DynamicResource Brush.Text.Secondary}" />
                            </Style>
                        </DataGridTextColumn.ElementStyle>
                    </DataGridTextColumn>

                    <DataGridTemplateColumn Width="50" Header="操作">
                        <DataGridTemplateColumn.CellTemplate>
                            <DataTemplate>
                                <Button
                                    Height="30"
                                    Command="{Binding DataContext.DeleteCommand, RelativeSource={RelativeSource AncestorType=UserControl}}"
                                    CommandParameter="{Binding AppId}"
                                    Content="注销"
                                    Style="{StaticResource Btn.Ghost.Danger}" />
                            </DataTemplate>
                        </DataGridTemplateColumn.CellTemplate>
                    </DataGridTemplateColumn>

                </DataGrid.Columns>
            </DataGrid>
        </Border>

        <Border
            Grid.Row="3"
            Padding="20"
            Background="{DynamicResource Brush.Bg.Panel}"
            BorderBrush="{DynamicResource Brush.Border}"
            BorderThickness="1"
            CornerRadius="{StaticResource Radius.Normal}">

            <Grid>
                <Grid.RowDefinitions>
                    <RowDefinition Height="Auto" />
                    <RowDefinition Height="Auto" />
                    <RowDefinition Height="Auto" />
                    <RowDefinition Height="Auto" />
                    <RowDefinition Height="Auto" />
                </Grid.RowDefinitions>
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="80" />
                    <ColumnDefinition Width="*" />
                    <ColumnDefinition Width="30" />
                    <ColumnDefinition Width="80" />
                    <ColumnDefinition Width="*" />
                </Grid.ColumnDefinitions>

                <StackPanel
                    Grid.Row="0"
                    Grid.ColumnSpan="5"
                    Margin="0,0,0,15"
                    Orientation="Horizontal">
                    <TextBlock
                        Margin="0,0,5,0"
                        Foreground="{DynamicResource Brush.Text.Primary}"
                        Text="➕" />
                    <TextBlock
                        FontSize="14"
                        FontWeight="Bold"
                        Foreground="{DynamicResource Brush.Text.Primary}"
                        Text="新增 / 编辑订阅" />
                </StackPanel>

                <TextBlock
                    Grid.Row="1"
                    Grid.Column="0"
                    VerticalAlignment="Center"
                    Foreground="{DynamicResource Brush.Text.Secondary}"
                    Text="App ID:" />
                <TextBox
                    Grid.Row="1"
                    Grid.Column="1"
                    Margin="0,5"
                    Text="{Binding EditAppId}" />
                <TextBlock
                    Grid.Row="1"
                    Grid.Column="3"
                    VerticalAlignment="Center"
                    Foreground="{DynamicResource Brush.Text.Secondary}"
                    Text="类型:" />
                <ComboBox
                    Grid.Row="1"
                    Grid.Column="4"
                    Height="{StaticResource Height.Input}"
                    Margin="0,5"
                    VerticalContentAlignment="Center"
                    DisplayMemberPath="Value"
                    ItemsSource="{Binding SubscriptionTypes}"
                    SelectedValue="{Binding EditType}"
                    SelectedValuePath="Key" />

                <TextBlock
                    Grid.Row="2"
                    Grid.Column="0"
                    VerticalAlignment="Center"
                    Foreground="{DynamicResource Brush.Text.Secondary}"
                    Text="目标帧率:" />
                <Grid
                    Grid.Row="2"
                    Grid.Column="1"
                    Margin="0,5">
                    <Grid.ColumnDefinitions>
                        <ColumnDefinition Width="*" />
                        <ColumnDefinition Width="Auto" />
                    </Grid.ColumnDefinitions>
                    <Slider
                        VerticalAlignment="Center"
                        IsSnapToTickEnabled="True"
                        Maximum="25"
                        Minimum="1"
                        Value="{Binding EditFps}" />
                    <TextBlock
                        Grid.Column="1"
                        Margin="10,0,0,0"
                        VerticalAlignment="Center"
                        FontWeight="Bold"
                        Foreground="{DynamicResource Brush.Brand}"
                        Text="{Binding EditFps, StringFormat={}{0} fps}" />
                </Grid>

                <TextBlock
                    Grid.Row="2"
                    Grid.Column="3"
                    VerticalAlignment="Center"
                    Foreground="{DynamicResource Brush.Text.Secondary}"
                    Text="备注:" />
                <TextBox
                    Grid.Row="2"
                    Grid.Column="4"
                    Margin="0,5"
                    Text="{Binding EditMemo}" />

                <Grid
                    Grid.Row="3"
                    Grid.ColumnSpan="5"
                    Visibility="{Binding IsNetworkType, Converter={StaticResource BoolToVis}}">
                    <Grid.ColumnDefinitions>
                        <ColumnDefinition Width="80" />
                        <ColumnDefinition Width="*" />
                        <ColumnDefinition Width="30" />
                        <ColumnDefinition Width="80" />
                        <ColumnDefinition Width="*" />
                    </Grid.ColumnDefinitions>

                    <TextBlock
                        Grid.Column="0"
                        VerticalAlignment="Center"
                        Foreground="{DynamicResource Brush.Text.Secondary}"
                        Text="目标 IP:" />
                    <TextBox
                        Grid.Column="1"
                        Margin="0,5"
                        Text="{Binding EditTargetIp}" />

                    <TextBlock
                        Grid.Column="3"
                        VerticalAlignment="Center"
                        Foreground="{DynamicResource Brush.Text.Secondary}"
                        Text="目标端口:" />
                    <TextBox
                        Grid.Column="4"
                        Margin="0,5"
                        Text="{Binding EditTargetPort}" />
                </Grid>

                <StackPanel
                    Grid.Row="4"
                    Grid.ColumnSpan="5"
                    Margin="0,15,0,0"
                    HorizontalAlignment="Right"
                    Orientation="Horizontal">

                    <Button
                        Width="100"
                        Margin="0,0,10,0"
                        Command="{Binding ClearFormCommand}"
                        Content="重置表单"
                        Style="{StaticResource Btn.Ghost}" />

                    <Button
                        Width="120"
                        Command="{Binding SaveCommand}"
                        Content="提交保存"
                        Style="{StaticResource PrimaryBtnStyle}" />
                </StackPanel>
            </Grid>
        </Border>
    </Grid>
</UserControl>