<Page
    x:Class="APKInstaller.Pages.ToolsPages.ApplicationsPage"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:controls="using:APKInstaller.Controls"
    xmlns:converter="using:APKInstaller.Helpers.Converter"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    xmlns:toolspages="using:APKInstaller.ViewModels.ToolsPages"
    d:DataContext="{d:DesignInstance Type=toolspages:ApplicationsViewModel}"
    NavigationCacheMode="Enabled"
    mc:Ignorable="d">
    <Page.Resources>
        <toolspages:ApplicationConverter x:Key="ApplicationConverter" />
        <converter:StringToBoolConverter x:Key="StringToBoolConverter" />
    </Page.Resources>
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto" />
            <RowDefinition Height="*" />
        </Grid.RowDefinitions>
        <controls:TitleBar
            x:Name="TitleBar"
            x:Uid="/ApplicationsPage/TitleBar"
            BackRequested="TitleBar_BackRequested"
            IsBackButtonVisible="True"
            IsBackEnabled="{x:Bind Frame.CanGoBack}"
            IsRefreshButtonVisible="True"
            IsRefreshEnabled="True"
            RefreshRequested="TitleBar_RefreshEvent">
            <ComboBox
                x:Name="DeviceComboBox"
                x:Uid="/ApplicationsPage/DeviceComboBox"
                VerticalAlignment="Center"
                ItemsSource="{x:Bind Provider.DeviceList, Mode=OneWay}"
                Loaded="ComboBox_Loaded"
                SelectionChanged="ComboBox_SelectionChanged" />
        </controls:TitleBar>
        <ListView
            x:Name="ListView"
            Grid.Row="1"
            ItemsSource="{x:Bind Provider.Applications, Mode=OneWay}"
            Loaded="ListView_Loaded">
            <ListView.ItemTemplate>
                <DataTemplate x:DataType="toolspages:APKInfo">
                    <ListViewItem Background="Transparent">
                        <ListViewItem.Resources>
                            <MenuFlyout x:Name="MenuFlyout">
                                <MenuFlyoutItem
                                    x:Name="Stop"
                                    x:Uid="/ApplicationsPage/Stop"
                                    x:Load="{x:Bind IsActive}"
                                    Click="MenuFlyoutItem_Click"
                                    Tag="{x:Bind Name}" />
                                <MenuFlyoutItem
                                    x:Name="Start"
                                    x:Uid="/ApplicationsPage/Start"
                                    x:Load="{x:Bind IsActive, Converter={StaticResource BoolNegationConverter}}"
                                    Click="MenuFlyoutItem_Click"
                                    Tag="{x:Bind Name}" />
                                <MenuFlyoutItem
                                    x:Name="Uninstall"
                                    x:Uid="/ApplicationsPage/Uninstall"
                                    x:Load="False"
                                    Click="MenuFlyoutItem_Click"
                                    Tag="{x:Bind Name}" />
                            </MenuFlyout>
                        </ListViewItem.Resources>
                        <controls:Setting
                            ContextFlyout="{StaticResource MenuFlyout}"
                            Header="{x:Bind Name}"
                            Icon="{x:Bind Icon}"
                            Style="{StaticResource ListViewItemContentSettingStyle}">
                            <controls:Setting.Description>
                                <Grid>
                                    <TextBlock x:Name="InfoText" x:Load="{x:Bind PackageName, Converter={StaticResource StringToBoolConverter}}">
                                        <Run Text="{x:Bind VersionInfo.VersionName}" />
                                        <Run Foreground="{StaticResource CardPrimaryForegroundBrush}" Text=" | " />
                                        <Run Text="{x:Bind PackageName}" />
                                        <Run Foreground="{StaticResource CardPrimaryForegroundBrush}" Text=" | " />
                                        <Run Text="{x:Bind IsActive, Converter={StaticResource ApplicationConverter}, ConverterParameter='State'}" />
                                    </TextBlock>
                                    <TextBlock x:Name="InfoTextWithoutName" x:Load="{x:Bind PackageName, Converter={StaticResource StringToBoolConverter}, ConverterParameter='true'}">
                                        <Run Text="{x:Bind VersionInfo.VersionName}" />
                                        <Run Foreground="{StaticResource CardPrimaryForegroundBrush}" Text=" | " />
                                        <Run Text="{x:Bind IsActive, Converter={StaticResource ApplicationConverter}, ConverterParameter='State'}" />
                                    </TextBlock>
                                </Grid>
                            </controls:Setting.Description>
                            <Button
                                Width="{ThemeResource SettingCardActionButtonWidth}"
                                Height="{ThemeResource SettingCardActionButtonHeight}"
                                Padding="0"
                                Flyout="{StaticResource MenuFlyout}"
                                Style="{StaticResource TitleBarSettingsButtonStyle}" />
                        </controls:Setting>
                    </ListViewItem>
                </DataTemplate>
            </ListView.ItemTemplate>
        </ListView>
    </Grid>
</Page>
