<UserControl x:Class="NebulaAuth.View.Dialogs.UpdateDialog"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
             xmlns:materialDesign="http://materialdesigninxaml.net/winfx/xaml/themes"
             xmlns:other="clr-namespace:NebulaAuth.ViewModel.Other"
             xmlns:nebulaAuth="clr-namespace:NebulaAuth"
             mc:Ignorable="d"
             TextElement.Foreground="{DynamicResource BaseContentBrush}"
             d:DataContext="{d:DesignInstance other:UpdateDialogVM}"
             Background="{DynamicResource WindowBackground}"
             MinWidth="420" MaxWidth="600">
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto" />
            <RowDefinition Height="Auto" />
            <RowDefinition Height="Auto" />
        </Grid.RowDefinitions>

        <!-- Header -->
        <nebulaAuth:DialogHeader IconKind="Update"
                                 Title="{Tr UpdateDialog.Title, IsDynamic=False}"
                                 TitleAccent="{Binding Version, Mode=OneWay}" />

        <!-- Changelog -->
        <Grid Grid.Row="1" Margin="12,10,12,4">
            <Grid.RowDefinitions>
                <RowDefinition Height="Auto" />
                <RowDefinition Height="Auto" />
            </Grid.RowDefinitions>

            <TextBlock Text="{Tr UpdateDialog.Changelog}" FontSize="14" FontWeight="SemiBold"
                       Margin="0,0,0,8"
                       Visibility="{Binding HasJsonChangelog, Converter={StaticResource BooleanToVisibilityConverter}}" />

            <!-- JSON changelog items -->
            <materialDesign:Card Grid.Row="1" Padding="10">
                <ScrollViewer MaxHeight="350" VerticalScrollBarVisibility="Auto"
                              Visibility="{Binding HasJsonChangelog, Converter={StaticResource BooleanToVisibilityConverter}}">
                    <ItemsControl ItemsSource="{Binding Changelog.Changes}">
                        <ItemsControl.ItemTemplate>
                            <DataTemplate>
                                <StackPanel>
                                    <StackPanel Orientation="Horizontal" Margin="0,6,0,6">
                                        <Border CornerRadius="3" Padding="4,2,4,2" Margin="0,0,8,0"
                                                VerticalAlignment="Center">
                                            <Border.Style>
                                                <Style TargetType="Border">
                                                    <Setter Property="Background"
                                                            Value="{DynamicResource MaterialDesign.Brush.Primary}" />
                                                    <Style.Triggers>
                                                        <DataTrigger Binding="{Binding Type}" Value="NEW">
                                                            <Setter Property="Background"
                                                                    Value="{DynamicResource SuccessBrush}" />
                                                        </DataTrigger>
                                                        <DataTrigger Binding="{Binding Type}" Value="FIX">
                                                            <Setter Property="Background"
                                                                    Value="{DynamicResource WarningBrush}" />
                                                        </DataTrigger>
                                                        <DataTrigger Binding="{Binding Type}" Value="IMPROVEMENT">
                                                            <Setter Property="Background"
                                                                    Value="{DynamicResource AccentBrush}" />
                                                        </DataTrigger>
                                                        <DataTrigger Binding="{Binding Type}" Value="SECURITY">
                                                            <Setter Property="Background"
                                                                    Value="{DynamicResource ErrorBrush}" />
                                                        </DataTrigger>
                                                    </Style.Triggers>
                                                </Style>
                                            </Border.Style>
                                            <TextBlock Text="{Binding Type}" FontSize="11" FontWeight="Bold"
                                                       Foreground="White" />
                                        </Border>
                                        <TextBlock Text="{Binding Text}" TextWrapping="Wrap"
                                                   VerticalAlignment="Center" FontSize="14" MaxWidth="420" />
                                        <Button Style="{StaticResource MaterialDesignIconForegroundButton}"
                                                Width="20" Height="20"
                                                Padding="2"
                                                Margin="4,0,0,0"
                                                VerticalAlignment="Center"
                                                Command="{Binding DataContext.OpenLinkCommand, RelativeSource={RelativeSource AncestorType=UserControl}}"
                                                CommandParameter="{Binding Link}"
                                                ToolTip="Open Link"
                                                Visibility="{Binding Link, Converter={StaticResource NullableToVisibilityConverter}}">
                                            <materialDesign:PackIcon Kind="OpenInNew" Width="14" Height="14" />
                                        </Button>
                                    </StackPanel>
                                    <Separator Opacity="0.2" Margin="0,2,0,2" />
                                </StackPanel>
                            </DataTemplate>
                        </ItemsControl.ItemTemplate>
                    </ItemsControl>
                </ScrollViewer>
            </materialDesign:Card>
            <!-- No changelog fallback -->
            <TextBlock Grid.Row="1" Text="{Tr UpdateDialog.NoChangelog}" FontSize="13" Opacity="0.6"
                       Visibility="{Binding HasJsonChangelog, Converter={StaticResource InverseBooleanToVisibilityConverter}}" />
        </Grid>

        <!-- Buttons -->
        <Grid Grid.Row="2" Margin="12,6,12,14">

            <!-- Normal buttons -->
            <StackPanel
                Visibility="{Binding ShowRemindOptions, Converter={StaticResource InverseBooleanToVisibilityConverter}}">
                <Button Style="{StaticResource MaterialDesignRaisedButton}"
                        Command="{Binding UpdateNowCommand}"
                        HorizontalAlignment="Stretch"
                        Margin="0,0,0,8"
                        Content="{Tr UpdateDialog.UpdateNow}" />
                <Grid>
                    <Grid.ColumnDefinitions>
                        <ColumnDefinition />
                        <ColumnDefinition Width="8" />
                        <ColumnDefinition />
                    </Grid.ColumnDefinitions>
                    <Button Style="{StaticResource MaterialDesignOutlinedButton}"
                            Command="{Binding ToggleRemindOptionsCommand}"
                            Content="{Tr UpdateDialog.RemindLater}" />
                    <Button Grid.Column="2"
                            Style="{StaticResource MaterialDesignFlatButton}"
                            Command="{Binding SkipVersionCommand}"
                            Content="{Tr UpdateDialog.SkipVersion}" />
                </Grid>
            </StackPanel>

            <!-- Remind later options -->
            <StackPanel
                Visibility="{Binding ShowRemindOptions, Converter={StaticResource BooleanToVisibilityConverter}}">
                <TextBlock Text="{Tr UpdateDialog.RemindAfter}" Margin="0,0,0,8" FontSize="14" />
                <WrapPanel>
                    <Button Style="{StaticResource MaterialDesignOutlinedButton}" Margin="0,0,4,4"
                            Command="{Binding SelectRemindOptionCommand}"
                            CommandParameter="1H"
                            Content="{Tr UpdateDialog.RemindOptions.1Hour}" />
                    <Button Style="{StaticResource MaterialDesignOutlinedButton}" Margin="0,0,4,4"
                            Command="{Binding SelectRemindOptionCommand}"
                            CommandParameter="1D"
                            Content="{Tr UpdateDialog.RemindOptions.1Day}" />
                    <Button Style="{StaticResource MaterialDesignOutlinedButton}" Margin="0,0,4,4"
                            Command="{Binding SelectRemindOptionCommand}"
                            CommandParameter="3D"
                            Content="{Tr UpdateDialog.RemindOptions.3Days}" />
                    <Button Style="{StaticResource MaterialDesignOutlinedButton}" Margin="0,0,4,4"
                            Command="{Binding SelectRemindOptionCommand}"
                            CommandParameter="7D"
                            Content="{Tr UpdateDialog.RemindOptions.7Days}" />
                </WrapPanel>
                <Button Style="{StaticResource MaterialDesignFlatButton}"
                        HorizontalAlignment="Left"
                        Command="{Binding ToggleRemindOptionsCommand}"
                        Content="{Tr Common.Cancel}" />
            </StackPanel>

        </Grid>
    </Grid>
</UserControl>