<UserControl
    x:Class="AmbientSounds.Controls.UploadForm"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:controls="using:Microsoft.Toolkit.Uwp.UI.Controls"
    xmlns:convert="using:AmbientSounds.Converters"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:local="using:AmbientSounds.Controls"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    xmlns:strings="using:AmbientSounds.Strings"
    xmlns:viewmodels="using:AmbientSounds.ViewModels"
    xmlns:winui="using:Microsoft.UI.Xaml.Controls"
    d:DesignHeight="300"
    d:DesignWidth="400"
    mc:Ignorable="d">

    <UserControl.Resources>
        <DataTemplate x:Key="ErrorTemplate" x:DataType="viewmodels:ErrorViewModel">
            <winui:InfoBar
                Margin="0,0,0,12"
                IsClosable="False"
                IsIconVisible="False"
                IsOpen="True"
                Message="{x:Bind convert:ErrorLoader.GetMessage(ErrorId, ErrorMessage)}"
                Severity="Error">
                <winui:InfoBar.Transitions>
                    <TransitionCollection>
                        <AddDeleteThemeTransition />
                    </TransitionCollection>
                </winui:InfoBar.Transitions>
            </winui:InfoBar>
        </DataTemplate>
    </UserControl.Resources>

    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="auto" />
            <RowDefinition Height="*" />
        </Grid.RowDefinitions>

        <Grid Margin="20,0">
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="*" />
                <ColumnDefinition Width="auto" />
            </Grid.ColumnDefinitions>

            <TextBlock Style="{StaticResource PageHeaderTextStyle}" Text="{x:Bind strings:Resources.NewSound}" />
        </Grid>

        <ScrollViewer
            Grid.Row="1"
            Margin="0,12,0,0"
            Padding="20,0">
            <StackPanel>
                <TextBox
                    Header="{x:Bind strings:Resources.Name}"
                    PlaceholderText="{x:Bind strings:Resources.Required}"
                    Text="{x:Bind ViewModel.Name, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" />

                <TextBox
                    Margin="0,12,0,0"
                    Header="{x:Bind strings:Resources.Attribution}"
                    PlaceholderText="{x:Bind strings:Resources.Required}"
                    Text="{x:Bind ViewModel.Attribution, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" />

                <TextBox
                    Margin="0,12,0,0"
                    Header="{x:Bind strings:Resources.ImageUrl}"
                    PlaceholderText="{x:Bind strings:Resources.Required}"
                    Text="{x:Bind ViewModel.ImageUrl, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" />

                <!--<TextBox
                    Margin="0,12,0,0"
                    Header="{x:Bind strings:Resources.DonateUrl}"
                    PlaceholderText="{x:Bind strings:Resources.Optional}"
                    Text="{x:Bind ViewModel.DonateUrl, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" />-->


                <Grid Margin="0,12,0,0">
                    <Grid.ColumnDefinitions>
                        <ColumnDefinition Width="*" />
                        <ColumnDefinition Width="Auto" />
                    </Grid.ColumnDefinitions>
                    <TextBox
                        Header="{x:Bind strings:Resources.PathToSound}"
                        IsReadOnly="True"
                        PlaceholderText="{x:Bind strings:Resources.Required}"
                        Text="{x:Bind ViewModel.SoundPath, Mode=OneWay}" />
                    <Button
                        Grid.Column="1"
                        Height="33"
                        Margin="8,0,0,0"
                        VerticalAlignment="Bottom"
                        AutomationProperties.Name="{x:Bind strings:Resources.BrowseText}"
                        Background="Transparent"
                        Command="{x:Bind ViewModel.PickSoundCommand}"
                        ToolTipService.ToolTip="{x:Bind strings:Resources.BrowseText}">
                        <FontIcon FontFamily="{StaticResource FluentUIGlyphs}" Glyph="&#xEA5A;" />
                    </Button>
                </Grid>

                <CheckBox Margin="0,12,0,0" IsChecked="{x:Bind ViewModel.Rule1, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}">
                    <TextBlock TextWrapping="WrapWholeWords">
                        <Hyperlink Click="{x:Bind ViewModel.TermsClicked}" NavigateUri="https://github.com/jenius-apps/ambie/blob/main/termsofuse.md">
                            <Run Text="{x:Bind strings:Resources.UploadTermsMessage}" />
                        </Hyperlink>
                    </TextBlock>
                </CheckBox>

                <Button
                    Height="36"
                    Margin="0,12,0,0"
                    HorizontalAlignment="Stretch"
                    Command="{x:Bind ViewModel.SubmitCommand}"
                    IsEnabled="{x:Bind ViewModel.IsUploadButtonEnabled, Mode=OneWay}">
                    <Grid>
                        <TextBlock
                            HorizontalAlignment="Center"
                            VerticalAlignment="Center"
                            Text="{x:Bind strings:Resources.UploadSubmitButton}"
                            Visibility="{x:Bind convert:VisibilityConverter.InvertBool(ViewModel.Uploading), Mode=OneWay}" />
                        <winui:ProgressRing
                            Width="20"
                            Height="20"
                            VerticalAlignment="Center"
                            Foreground="{ThemeResource DefaultTextForegroundThemeBrush}"
                            IsActive="{x:Bind ViewModel.Uploading, Mode=OneWay}"
                            Visibility="{x:Bind ViewModel.Uploading, Mode=OneWay}" />
                    </Grid>
                </Button>

                <TextBlock
                    Margin="0,24,0,0"
                    Text="{x:Bind strings:Resources.Preview}"
                    Visibility="{x:Bind ViewModel.PreviewVisible, Mode=OneWay}" />
                <Grid Margin="0,4,0,0" Visibility="{x:Bind ViewModel.PreviewVisible, Mode=OneWay}">
                    <Grid.ColumnDefinitions>
                        <ColumnDefinition Width="auto" />
                        <ColumnDefinition Width="*" />
                    </Grid.ColumnDefinitions>
                    <Grid
                        Width="80"
                        VerticalAlignment="Stretch"
                        CornerRadius="8">
                        <controls:ImageEx Source="{x:Bind ViewModel.ImageUrl, Mode=OneWay}" Stretch="UniformToFill" />
                    </Grid>
                    <StackPanel Grid.Column="1" Margin="4,0,0,0">
                        <TextBlock
                            FontSize="16"
                            FontWeight="SemiBold"
                            Text="{x:Bind ViewModel.Name, Mode=OneWay}"
                            TextTrimming="CharacterEllipsis" />
                        <TextBlock
                            IsTextSelectionEnabled="True"
                            Text="{x:Bind ViewModel.Attribution, Mode=OneWay}"
                            TextTrimming="CharacterEllipsis" />
                    </StackPanel>
                </Grid>

                <winui:ItemsRepeater
                    Margin="0,12,0,0"
                    ItemTemplate="{StaticResource ErrorTemplate}"
                    ItemsSource="{x:Bind ViewModel.Errors}" />
            </StackPanel>
        </ScrollViewer>
    </Grid>
</UserControl>
