<UserControl x:Class="WowUp.WPF.Views.AddonsView"
             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:gif="http://wpfanimatedgif.codeplex.com"
             xmlns:t="http://schemas.telerik.com/2008/xaml/presentation"
             xmlns:i="http://schemas.microsoft.com/xaml/behaviors"
             xmlns:vw = "clr-namespace:WowUp.WPF.Views"
             mc:Ignorable="d" 
             d:DesignHeight="450" d:DesignWidth="800"
             Initialized="UserControl_Initialized">

    <UserControl.Resources>
        <ResourceDictionary >
            <ResourceDictionary.MergedDictionaries>
                <ResourceDictionary Source="Styles.xaml"/>
            </ResourceDictionary.MergedDictionaries>

            <Style x:Key="HyperlinkStyle" TargetType="Hyperlink">
                <Setter Property="Foreground" Value="White" />
                <Setter Property="TextDecorations" Value="None" />
            </Style>

            <DataTemplate x:Key="AddonColumnTemplate" >
                <Border Padding="10">
                    <Grid Background="Transparent">
                        <Grid.ColumnDefinitions>
                            <ColumnDefinition Width="Auto" />
                            <ColumnDefinition Width="*" />
                        </Grid.ColumnDefinitions>
                        <StackPanel Grid.Column="0" Margin="0, 0, 10, 0" VerticalAlignment="Center"
                                    Background="#222">
                            <Border Background="{StaticResource Dark2}"  >
                                <Image Width="40" Height="40"
                                       Source="{Binding ThumbnailUrl, Converter={StaticResource UriToThumbnailConverter}}"
                                       RenderOptions.BitmapScalingMode="HighQuality">
                                </Image>
                            </Border>
                            <TextBlock Visibility="{Binding ShowChannelName, Converter={StaticResource BoolToVisibilty}}" 
                                       Text="{Binding ChannelName}"
                                       HorizontalAlignment="Center"
                                       Foreground="{Binding ChannelNameBrush}"
                                       
                                       FontWeight="ExtraBold"/>
                        </StackPanel>
                        <StackPanel Grid.Column="1" VerticalAlignment="Center">
                            <TextBlock Visibility="{Binding ExternalUrl, Converter={StaticResource StringToVisibilty}}" 
                                       Style="{StaticResource labelAddonName}" >
                                <Hyperlink x:Name="AddonLink"
                                           Style="{StaticResource HyperlinkStyle}"
                                           NavigateUri="{Binding ExternalUrl}"
                                           Command="{Binding OpenLinkCommand}">
                                    <TextBlock Text="{Binding Name}" TextWrapping="Wrap"/>
                                </Hyperlink>    
                            </TextBlock>
                            <TextBlock Visibility="{Binding ExternalUrl, Converter={StaticResource InverseStringToVisibilty}}"
                                       Text="{Binding Name}"  
                                       Style="{StaticResource labelAddonName}" 
                                       TextWrapping="Wrap"/>
                            <TextBlock Text="{Binding CurrentVersion}" 
                                       Style="{StaticResource labelAddonText}" />
                        </StackPanel>
                    </Grid>
                </Border>
            </DataTemplate>

            <DataTemplate x:Key="LatestVersionTemplate" >
                <Border Padding="10">
                    <Grid Background="Transparent">
                        <Grid.ColumnDefinitions>
                            <ColumnDefinition Width="*" />
                        </Grid.ColumnDefinitions>
                        <StackPanel Grid.Column="0" VerticalAlignment="Center">
                            <TextBlock Text="{Binding LatestVersion}" 
                                       TextWrapping="Wrap"
                                       Style="{StaticResource labelAddonText}" />
                        </StackPanel>
                    </Grid>
                </Border>
            </DataTemplate>

            <DataTemplate x:Key="GameVersionTemplate" >
                <Border Padding="10">
                    <Grid Background="Transparent">
                        <Grid.ColumnDefinitions>
                            <ColumnDefinition Width="*" />
                        </Grid.ColumnDefinitions>
                        <StackPanel Grid.Column="0" VerticalAlignment="Center">
                            <TextBlock Text="{Binding GameVersion}" 
                                       TextWrapping="Wrap"
                                       Style="{StaticResource labelAddonText}" />
                        </StackPanel>
                    </Grid>
                </Border>
            </DataTemplate>

            <DataTemplate x:Key="AuthorTemplate" >
                <Border Padding="10">
                    <Grid Background="Transparent">
                        <Grid.ColumnDefinitions>
                            <ColumnDefinition Width="*" />
                        </Grid.ColumnDefinitions>
                        <StackPanel Grid.Column="0" VerticalAlignment="Center">
                            <TextBlock Text="{Binding Author}" 
                                       TextWrapping="Wrap"
                                       Style="{StaticResource labelAddonText}" />
                        </StackPanel>
                    </Grid>
                </Border>
            </DataTemplate>

            <DataTemplate x:Key="SourceTemplate" >
                <Border Padding="10">
                    <Grid Background="Transparent">
                        <Grid.ColumnDefinitions>
                            <ColumnDefinition Width="*" />
                        </Grid.ColumnDefinitions>
                        <StackPanel Grid.Column="0" VerticalAlignment="Center">
                            <TextBlock Text="{Binding ProviderName}" 
                                       TextWrapping="Wrap"
                                       Style="{StaticResource labelAddonText}" />
                        </StackPanel>
                    </Grid>
                </Border>
            </DataTemplate>

            <DataTemplate x:Key="StatusColumnTemplate" >
                <Border Padding="10">
                    <StackPanel VerticalAlignment="Center">
                        <Button Visibility="{Binding ShowInstallButton, Converter={StaticResource BoolToVisibilty}}"
                                Style="{StaticResource purpleButton}"
                                HorizontalAlignment="Center"
                                Command="{Binding InstallCommand}">Install</Button>
                        <Button Visibility="{Binding ShowUpdateButton, Converter={StaticResource BoolToVisibilty}}"
                                Style="{StaticResource purpleButton}"
                                HorizontalAlignment="Center"
                                Command="{Binding UpdateCommand}">Update</Button>
                        <TextBlock Text="{Binding StatusText}"
                                   Style="{StaticResource labelAddonText}"
                                   HorizontalAlignment="Center"
                                   Visibility="{Binding ShowStatusText, Converter={StaticResource BoolToVisibilty}}" />
                        <StackPanel Visibility="{Binding ShowProgressBar, Converter={StaticResource BoolToVisibilty}}">
                            <TextBlock Text="{Binding ProgressText}" 
                                       Style="{StaticResource labelAddonText}"
                                       HorizontalAlignment ="Center"/>
                            <ProgressBar Minimum="0" Maximum="100" Value="{Binding ProgressPercent}" />
                        </StackPanel>
                    </StackPanel>
                </Border>
            </DataTemplate>
        </ResourceDictionary>
    </UserControl.Resources>

    <Grid Background="{StaticResource Dark3}">
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto" />
            <RowDefinition Height="Auto" />
            <RowDefinition Height="*" />
        </Grid.RowDefinitions>
        <Grid x:Name="ControlBar" Grid.Row="0">
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="*" />
                <ColumnDefinition Width="Auto" />
            </Grid.ColumnDefinitions>
            <Border Grid.Column="0" Padding="10">
                <StackPanel  Orientation="Horizontal">
                    <TextBlock Text="World of Warcraft" FontSize="14" Foreground="White" VerticalAlignment="Center" Margin="0, 0, 10,0"/>
                    <ComboBox x:Name="WowClientComboBox"
                              SelectedItem="{Binding SelectedClientType}"
                              Style="{StaticResource ComboBoxFlatStyle}"
                              IsEnabled="{Binding IsBusy, Converter={StaticResource InverseBool}}"
                              ItemsSource="{Binding ClientTypeNames}">
                        <i:Interaction.Triggers>
                            <i:EventTrigger EventName="SelectionChanged">
                                <i:InvokeCommandAction Command="{Binding SelectedWowClientCommand}" 
                                                      CommandParameter="{Binding ElementName=WowClientComboBox, Path=SelectedValue}"/>
                            </i:EventTrigger>
                        </i:Interaction.Triggers>
                    </ComboBox>
                </StackPanel>
            </Border>
            <Border Grid.Column="1" Padding="10">
                <StackPanel Orientation="Horizontal">
                    <Button x:Name="UpdateAllButton" Margin="0, 0, 10, 0" Click="UpdateAllButton_Click" 
                            IsEnabled="{Binding EnableUpdateAll}"
                            Style="{StaticResource purpleButton}">Update All</Button>
                    <Button x:Name="RefreshButton" Margin="0, 0, 10, 0" Click="RefreshButton_Click"
                            IsEnabled="{Binding EnableRefresh}"
                            Style="{StaticResource purpleButton}">Refresh</Button>
                    <Button x:Name="RescanButton" Click="RescanButton_Click"
                            IsEnabled="{Binding EnableRescan}"
                            Style="{StaticResource purpleButton}">Re-Scan</Button>
                    <vw:SearchInput Width="150" Margin="10 0 0 0" VerticalAlignment="Center" DataContext="{Binding SearchInputViewModel}"/>
                </StackPanel>
            </Border>
        </Grid>
        <Grid x:Name="AddonTable" Grid.Row="2">
            <Grid.RowDefinitions>
                <RowDefinition Height="*" />
            </Grid.RowDefinitions>
            <StackPanel Grid.Row="0" Visibility="{Binding IsBusy, Converter={StaticResource BoolToVisibilty}}" VerticalAlignment="Center">
                <TextBlock Text="Loading Addons..." HorizontalAlignment="Center" FontSize="14" Foreground="White" />
                <Image gif:ImageBehavior.AnimatedSource="../Assets/activity.gif" Height="40"
                       RenderOptions.BitmapScalingMode="HighQuality"/>
            </StackPanel>
            <StackPanel Grid.Row="0" Visibility="{Binding ShowEmptyLabel, Converter={StaticResource BoolToVisibilty}}" VerticalAlignment="Center" >
                <TextBlock Text="No Addons found" HorizontalAlignment="Center" FontSize="16" Foreground="White"/>
            </StackPanel>
            <DataGrid Grid.Row="0" Padding="0" Margin="0"
                      x:Name="AddonGrid"
                      Visibility="{Binding ShowResults, Converter={StaticResource BoolToVisibilty}}"
                      ItemsSource="{Binding DisplayAddons}"
                      Background="{StaticResource Dark2}"
                      AutoGenerateColumns="False" 
                      GridLinesVisibility="Horizontal"
                      CanUserReorderColumns="False"
                      IsReadOnly="True"
                      HorizontalGridLinesBrush="{StaticResource Dark2}"
                      RowHeaderWidth="0"
                      BorderThickness="0"
                      VirtualizingPanel.ScrollUnit="Pixel"
                      BorderBrush="Transparent"
                      Sorting="AddonGrid_Sorting">
                <DataGrid.Resources>
                    <ContextMenu x:Key="RowMenu"
                                 Style="{StaticResource DarkMenu}"
                                 DataContext="{Binding PlacementTarget.DataContext, RelativeSource={RelativeSource Self}}">
                        <MenuItem Style="{StaticResource DarkHeaderMenuItem}" Header="{Binding Name}" IsEnabled="False" />
                        <MenuItem Header="Ignore" IsCheckable="True" 
                                  IsChecked="{Binding IsIgnored}" 
                                  Command="{Binding IgnoreCheckedCommand}"
                                  CommandParameter="{Binding}">
                        </MenuItem>
                        <MenuItem Header="Auto Update" IsCheckable="True" 
                                  IsChecked="{Binding IsAutoUpdated}" 
                                  Command="{Binding AutoUpdateCheckedCommand}"
                                  CommandParameter="{Binding}">
                        </MenuItem>
                        <MenuItem Header="Channel">
                            <MenuItem Header="Stable" 
                                      IsCheckable="True" IsChecked="{Binding IsStableChannel}"
                                      IsEnabled="{Binding CanCheckStable}"
                                      Command="{Binding StableCheckedCommand, Mode=TwoWay, UpdateSourceTrigger=Explicit}"></MenuItem>
                            <MenuItem Header="Beta" 
                                      IsCheckable="True" IsChecked="{Binding IsBetaChannel}"
                                      IsEnabled="{Binding CanCheckBeta}"
                                      Command="{Binding BetaCheckedCommand}"></MenuItem>
                            <MenuItem Header="Alpha" 
                                      IsCheckable="True" IsChecked="{Binding IsAlphaChannel}"
                                      IsEnabled="{Binding CanCheckAlpha}"
                                      Command="{Binding AlphaCheckedCommand}"></MenuItem>
                        </MenuItem>
                        <Separator Visibility="{Binding ShowUpdateButton, Converter={StaticResource BoolToVisibilty}}" />
                        <MenuItem Header="Update" 
                                  Visibility="{Binding ShowUpdateButton, Converter={StaticResource BoolToVisibilty}}"
                                  Command="{Binding InstallCommand}"/>
                        <MenuItem Header="Re-Install" 
                                  Visibility="{Binding ShowReInstall, Converter={StaticResource BoolToVisibilty}}"
                                  Command="{Binding ReInstallCommand}"/>
                        <Separator Visibility="{Binding ShowUninstall, Converter={StaticResource BoolToVisibilty}}" />
                        <MenuItem Header="Remove"
                                  Command="{Binding UninstallCommand}"
                                  Visibility="{Binding ShowUninstall, Converter={StaticResource BoolToVisibilty}}" />
                    </ContextMenu>

                    <Style TargetType="{x:Type DataGrid}">
                        <Setter Property="Background" Value="{StaticResource Dark3}"/>
                    </Style>
                    <Style TargetType="{x:Type DataGridRow}">
                        <Setter Property="Background" Value="{StaticResource Dark3}"/>
                        <Style.Triggers>
                            <Trigger Property="IsMouseOver" Value="True">
                                <Setter Property="Background" Value="{StaticResource Dark1}" />
                            </Trigger>
                        </Style.Triggers>
                    </Style>
                    <Style TargetType="{x:Type DataGridRowHeader}">
                        <Setter Property="Background" Value="{StaticResource Dark3}"/>
                    </Style>
                    <Style TargetType="{x:Type DataGridCell}">
                        <Style.Triggers>
                            <Trigger Property="DataGridCell.IsSelected" Value="True">
                                <Setter Property="BorderBrush" Value="Transparent"/>
                                <Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.ControlTextBrushKey}}"/>
                                <Setter Property="Background" Value="Transparent"/>
                            </Trigger>

                        </Style.Triggers>
                    </Style>
                    <Style TargetType="{x:Type DataGridColumnHeader}">
                        <Setter Property="Background" Value="{StaticResource Dark2}"/>
                        <Setter Property="HorizontalContentAlignment" Value="Stretch"/>
                    </Style>
                </DataGrid.Resources>
                <DataGrid.RowStyle>
                    <Style TargetType="DataGridRow" >
                        <Setter Property="ContextMenu" Value="{StaticResource RowMenu}" />
                        <Setter Property="Background" Value="{StaticResource Dark3}"/>
                        <Style.Triggers>
                            <Trigger Property="IsMouseOver" Value="True">
                                <Setter Property="Background" Value="{StaticResource Dark1}" />
                            </Trigger>
                        </Style.Triggers>
                    </Style>
                </DataGrid.RowStyle>
                <DataGrid.Columns>
                    <!--ADDON COLUMN-->
                    <DataGridTemplateColumn SortMemberPath="Name" CellTemplate="{StaticResource AddonColumnTemplate}" Width="*">
                        <DataGridTemplateColumn.HeaderTemplate>
                            <DataTemplate>
                                <Grid >
                                    <Grid.ColumnDefinitions>
                                        <ColumnDefinition Width="Auto" />
                                        <ColumnDefinition Width="Auto" />
                                    </Grid.ColumnDefinitions>
                                    <TextBlock Grid.Column="0" 
                                                Text="{Binding DataContext.AddonHeaderText, RelativeSource={RelativeSource AncestorType={x:Type DataGrid}}}" 
                                                TextWrapping="Wrap" 
                                                Style="{StaticResource labelTableHeader}" />
                                    <Image Grid.Column="1" Margin="10 0 0 0"
                                                Width="20"
                                                Source="/Assets/sort_up_icon.png"
                                                Visibility="{Binding DataContext.AddonNameSortDirection, RelativeSource={RelativeSource AncestorType=UserControl}, Converter={StaticResource SortAscendingToVisibilityConverter}}" />
                                    <Image Grid.Column="1" Margin="10 0 0 0"
                                                Width="20"
                                                Source="/Assets/sort_down_icon.png"
                                                Visibility="{Binding DataContext.AddonNameSortDirection, RelativeSource={RelativeSource AncestorType=UserControl}, Converter={StaticResource SortDescendingToVisibilityConverter}}" />
                                </Grid>
                            </DataTemplate>
                        </DataGridTemplateColumn.HeaderTemplate>
                    </DataGridTemplateColumn>
                    <!--STATUS COLUMN-->
                    <DataGridTemplateColumn SortMemberPath="DisplayState" CellTemplate="{StaticResource StatusColumnTemplate}" Width="120" CanUserResize="False">
                        <DataGridTemplateColumn.HeaderTemplate>
                            <DataTemplate>
                                <Border Padding="5">
                                    <StackPanel Orientation="Horizontal" >
                                        <TextBlock Text="{Binding DataContext.StatusHeaderText, RelativeSource={RelativeSource AncestorType=DataGrid}}" 
                                                   TextWrapping="Wrap" 
                                                   Style="{StaticResource labelTableHeader}" />
                                        <Image Width="20"
                                               Source="/Assets/sort_up_icon.png"
                                               Visibility="{Binding DataContext.StatusSortDirection, RelativeSource={RelativeSource AncestorType=DataGrid}, Converter={StaticResource SortAscendingToVisibilityConverter}}" />
                                        <Image Width="20"
                                               Source="/Assets/sort_down_icon.png"
                                               Visibility="{Binding DataContext.StatusSortDirection, RelativeSource={RelativeSource AncestorType=DataGrid}, Converter={StaticResource SortDescendingToVisibilityConverter}}" />
                                    </StackPanel>
                                </Border>
                            </DataTemplate>
                        </DataGridTemplateColumn.HeaderTemplate>
                    </DataGridTemplateColumn>
                    <!--LATEST VERSION COLUMN-->
                    <DataGridTemplateColumn SortMemberPath="LatestVersion" CellTemplate="{StaticResource LatestVersionTemplate}" Width="120" MinWidth="80">
                        <DataGridTemplateColumn.HeaderTemplate>
                            <DataTemplate>
                                <Border Padding="5">
                                    <StackPanel Orientation="Horizontal" >
                                        <TextBlock 
                                                   Text="{Binding DataContext.LatestVersionHeaderText, RelativeSource={RelativeSource AncestorType=DataGrid}}" 
                                                   TextWrapping="Wrap" 
                                                   Style="{StaticResource labelTableHeader}" />
                                        <Image Width="20"
                                               Source="/Assets/sort_up_icon.png"
                                               Visibility="{Binding DataContext.LatestVersionSortDirection, RelativeSource={RelativeSource AncestorType=DataGrid}, Converter={StaticResource SortAscendingToVisibilityConverter}}" />
                                        <Image Width="20"
                                               Source="/Assets/sort_down_icon.png"
                                               Visibility="{Binding DataContext.LatestVersionSortDirection, RelativeSource={RelativeSource AncestorType=DataGrid}, Converter={StaticResource SortDescendingToVisibilityConverter}}" />
                                    </StackPanel>
                                </Border>
                            </DataTemplate>
                        </DataGridTemplateColumn.HeaderTemplate>
                    </DataGridTemplateColumn>
                    <!--GAME VERSION COLUMN-->
                    <DataGridTemplateColumn SortMemberPath="GameVersion" CellTemplate="{StaticResource GameVersionTemplate}" Width="120" CanUserResize="False">
                        <DataGridTemplateColumn.HeaderTemplate>
                            <DataTemplate>
                                <Border Padding="5">
                                    <StackPanel Orientation="Horizontal" >
                                        <TextBlock Text="{Binding DataContext.GameVersionHeaderText, RelativeSource={RelativeSource AncestorType=DataGrid}}" 
                                                   TextWrapping="Wrap" 
                                                   Style="{StaticResource labelTableHeader}" />
                                        <Image Width="20"
                                               Source="/Assets/sort_up_icon.png"
                                               Visibility="{Binding DataContext.GameVersionSortDirection, RelativeSource={RelativeSource AncestorType=DataGrid}, Converter={StaticResource SortAscendingToVisibilityConverter}}" />
                                        <Image Width="20"
                                               Source="/Assets/sort_down_icon.png"
                                               Visibility="{Binding DataContext.GameVersionSortDirection, RelativeSource={RelativeSource AncestorType=DataGrid}, Converter={StaticResource SortDescendingToVisibilityConverter}}" />
                                    </StackPanel>
                                </Border>
                            </DataTemplate>
                        </DataGridTemplateColumn.HeaderTemplate>
                    </DataGridTemplateColumn>
                    <!--PROVIDER COLUMN-->
                    <DataGridTemplateColumn SortMemberPath="ProviderName" CellTemplate="{StaticResource SourceTemplate}" Width="110" CanUserResize="False">
                        <DataGridTemplateColumn.HeaderTemplate>
                            <DataTemplate>
                                <Border Padding="5">
                                    <StackPanel Orientation="Horizontal" >
                                        <TextBlock Text="{Binding DataContext.ProviderHeaderText, RelativeSource={RelativeSource AncestorType=UserControl}}" 
                                                   TextWrapping="Wrap" 
                                                   Style="{StaticResource labelTableHeader}" />
                                        <Image Width="20"
                                               Source="/Assets/sort_up_icon.png"
                                               Visibility="{Binding DataContext.ProviderNameSortDirection, RelativeSource={RelativeSource AncestorType=UserControl}, Converter={StaticResource SortAscendingToVisibilityConverter}}" />
                                        <Image Width="20"
                                               Source="/Assets/sort_down_icon.png"
                                               Visibility="{Binding DataContext.ProviderNameSortDirection, RelativeSource={RelativeSource AncestorType=UserControl}, Converter={StaticResource SortDescendingToVisibilityConverter}}" />
                                    </StackPanel>
                                </Border>
                            </DataTemplate>
                        </DataGridTemplateColumn.HeaderTemplate>
                    </DataGridTemplateColumn>
                    <!--AUTHOR COLUMN-->
                    <DataGridTemplateColumn SortMemberPath="Author" CellTemplate="{StaticResource AuthorTemplate}" Width="150" MinWidth="80" CanUserResize="False">
                        <DataGridTemplateColumn.HeaderTemplate>
                            <DataTemplate>
                                <Border Padding="5">
                                    <StackPanel Orientation="Horizontal" >
                                        <TextBlock Text="{Binding DataContext.AuthorHeaderText, RelativeSource={RelativeSource AncestorType=UserControl}}" 
                                                   TextWrapping="Wrap" 
                                                   Style="{StaticResource labelTableHeader}" />
                                        <Image Width="20"
                                               Source="/Assets/sort_up_icon.png"
                                               Visibility="{Binding DataContext.AuthorSortDirection, RelativeSource={RelativeSource AncestorType=UserControl}, Converter={StaticResource SortAscendingToVisibilityConverter}}" />
                                        <Image Width="20"
                                               Source="/Assets/sort_down_icon.png"
                                               Visibility="{Binding DataContext.AuthorSortDirection, RelativeSource={RelativeSource AncestorType=UserControl}, Converter={StaticResource SortDescendingToVisibilityConverter}}" />
                                    </StackPanel>
                                </Border>
                            </DataTemplate>
                        </DataGridTemplateColumn.HeaderTemplate>
                    </DataGridTemplateColumn>
                </DataGrid.Columns>
            </DataGrid>
        </Grid>
    </Grid>
</UserControl>
