<UserControl xmlns="https://github.com/avaloniaui"
             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:controls="clr-namespace:GalaxyBudsClient.Interface.Controls"
             xmlns:pages="clr-namespace:GalaxyBudsClient.Interface.ViewModels.Pages"
             xmlns:ext="clr-namespace:GalaxyBudsClient.Interface.MarkupExtensions"
             xmlns:ui="clr-namespace:FluentAvalonia.UI.Controls;assembly=FluentAvalonia"
             xmlns:firmware="clr-namespace:GalaxyBudsClient.Model.Firmware"
             xmlns:converters="clr-namespace:GalaxyBudsClient.Interface.Converters"
             xmlns:input="clr-namespace:FluentAvalonia.UI.Input;assembly=FluentAvalonia"
             xmlns:i18N="clr-namespace:GalaxyBudsClient.Generated.I18N"
             mc:Ignorable="d" d:DesignWidth="800" d:DesignHeight="400"
             x:Class="GalaxyBudsClient.Interface.Pages.FirmwarePage"
             x:DataType="pages:FirmwarePageViewModel"
             x:CompileBindings="True">

    <Design.DataContext>
        <pages:FirmwarePageViewModel />
    </Design.DataContext>

    <UserControl.Resources>
        <input:XamlUICommand x:Key="InstallCommand"
                             Label="{ext:Translate fw_select_install_short}"
                             IconSource="{StaticResource InstallIcon}"
                             Description="{ext:Translate fw_select_install}"
                             Command="{Binding DoInstallCommand}" />

        <input:XamlUICommand x:Key="RefreshCommand"
                             Label="{ext:Translate fw_select_refresh}"
                             IconSource="{StaticResource ReloadIcon}"
                             Description="{ext:Translate fw_select_refresh}"
                             Command="{Binding DoRefreshCommand}" />

        <input:XamlUICommand x:Key="SideloadCommand"
                             Label="{ext:Translate fw_select_from_disk_short}"
                             IconSource="{StaticResource OpenIcon}"
                             Description="{ext:Translate fw_select_from_disk}"
                             Command="{Binding DoSideloadCommand}" />
    </UserControl.Resources>

    <Panel Margin="{StaticResource AppPageMargin}">
        <Interaction.Behaviors>
            <ext:RequiresConnectedDeviceBehavior />
        </Interaction.Behaviors>
        
        <Grid RowDefinitions="*,8,Auto">
            <Border Classes="Card">
                <Grid RowDefinitions="Auto,*">
                    <ui:CommandBar DefaultLabelPosition="Right" Padding="5,0">
                        <ui:CommandBar.PrimaryCommands>
                            <ui:CommandBarButton
                                IsEnabled="{Binding #FwList.SelectedValue, Converter={x:Static ObjectConverters.IsNotNull}}"
                                Command="{StaticResource InstallCommand}"
                                CommandParameter="{Binding #FwList.SelectedItem}" />
                            <ui:CommandBarButton Command="{StaticResource RefreshCommand}" />
                            <ui:CommandBarSeparator />
                            <ui:CommandBarButton Command="{StaticResource SideloadCommand}" />
                        </ui:CommandBar.PrimaryCommands>
                    </ui:CommandBar>

                    <ListBox Grid.Row="1"
                             ItemsSource="{Binding AvailableFirmware}"
                             IsVisible="{Binding !NoResults}"
                             Name="FwList">
                        <ListBox.ItemTemplate>
                            <DataTemplate x:DataType="firmware:FirmwareRemoteBinary">
                                <Border Background="Transparent">
                                    <Panel>
                                        <StackPanel Spacing="4" Margin="4 10" VerticalAlignment="Center">
                                            <TextBlock Text="{Binding BuildName}"
                                                       Classes="BaseTextBlockStyle"
                                                       VerticalAlignment="Bottom"
                                                       HorizontalAlignment="Left"
                                                       TextTrimming="CharacterEllipsis" />

                                            <TextBlock Classes="CaptionTextBlockStyle"
                                                       Foreground="{DynamicResource TextFillColorSecondaryBrush}"
                                                       VerticalAlignment="Top"
                                                       TextWrapping="Wrap">
                                                <TextBlock.Text>
                                                    <MultiBinding StringFormat="{}{1} {0}, rev{2} ({3})">
                                                        <Binding Path="Year" />
                                                        <Binding Path="Month"
                                                                 Converter="{x:Static converters:MonthValueConverter.Instance}" />
                                                        <Binding Path="Revision" />
                                                        <Binding Path="Model" />
                                                    </MultiBinding>
                                                </TextBlock.Text>
                                            </TextBlock>
                                        </StackPanel>

                                        <StackPanel Orientation="Horizontal"
                                                    HorizontalAlignment="Right">
                                            <ui:CommandBarButton HorizontalAlignment="Right"
                                                                 Command="{StaticResource InstallCommand}"
                                                                 CommandParameter="{Binding RelativeSource={RelativeSource Self}, Path=DataContext}"
                                                                 HotKey="{x:Null}" />
                                        </StackPanel>
                                    </Panel>
                                </Border>
                            </DataTemplate>
                        </ListBox.ItemTemplate>
                    </ListBox>

                    <controls:EmptyView Grid.Row="1"
                                        IsVisible="{Binding NoResults}"
                                        Text="{ext:Translate {x:Static i18N:Keys.FwSelectNoResultsShort}}"
                                        Symbol="DocumentSplitHintOff" />
                </Grid>
            </Border>

            <controls:SettingsGroup Grid.Row="2">
                <controls:SettingsSwitchItem Content="{ext:Translate {x:Static i18N:Keys.FwSelectDowngrade}}"
                                             Description="{ext:Translate {x:Static i18N:Keys.FwSelectDowngradeDesc}}"
                                             Symbol="CubeArrowCurveDown"
                                             IsChecked="{Binding IsDowngradingAllowed}" />
            </controls:SettingsGroup>
        </Grid>
    </Panel>
</UserControl>