<UserControl x:Class="Artemis.UI.Screens.Sidebar.Dialogs.ProfileEdit.ProfileEditView"
             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:s="https://github.com/canton7/Stylet"
             xmlns:materialDesign="http://materialdesigninxaml.net/winfx/xaml/themes"
             xmlns:converters="clr-namespace:Artemis.UI.Converters"
             xmlns:shared="clr-namespace:Artemis.UI.Shared;assembly=Artemis.UI.Shared"
             xmlns:profileEdit="clr-namespace:Artemis.UI.Screens.Sidebar.Dialogs.ProfileEdit"
             xmlns:core="clr-namespace:Artemis.Core;assembly=Artemis.Core"
             mc:Ignorable="d"
             d:DesignHeight="450" d:DesignWidth="280"
             d:DataContext="{d:DesignInstance {x:Type profileEdit:ProfileEditViewModel}}"
             Width="800">
    <UserControl.Resources>
        <converters:InverseBooleanConverter x:Key="InverseBooleanConverter" />
        <shared:BindingProxy x:Key="DataContextProxy" Data="{Binding}" />
        <shared:NullToVisibilityConverter x:Key="NullToVisibilityConverter" />
        <shared:StreamToBitmapImageConverter x:Key="StreamToBitmapImageConverter" />
        <shared:StreamToSvgImageConverter x:Key="StreamToSvgImageConverter" />
    </UserControl.Resources>
    <Grid Margin="16">
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto" />
            <RowDefinition Height="Auto" />
            <RowDefinition Height="Auto" />
            <RowDefinition Height="Auto" />
            <RowDefinition Height="Auto" />
        </Grid.RowDefinitions>
        <Grid.ColumnDefinitions>
            <ColumnDefinition />
            <ColumnDefinition Width="Auto" />
            <ColumnDefinition />
        </Grid.ColumnDefinitions>

        <TextBlock Grid.Row="0"
                   Grid.Column="0"
                   Style="{StaticResource MaterialDesignHeadline6TextBlock}"
                   Margin="0 0 0 10"
                   Visibility="{Binding IsNew, Converter={x:Static s:BoolToVisibilityConverter.Instance}, Mode=OneWay}">
            Add a new profile
        </TextBlock>
        <TextBlock Grid.Row="0"
                   Grid.Column="0"
                   Style="{StaticResource MaterialDesignHeadline6TextBlock}"
                   Margin="0 0 0 10"
                   Visibility="{Binding IsNew, Converter={x:Static s:BoolToVisibilityConverter.InverseInstance}, Mode=OneWay}">
            <Run Text="{Binding ProfileConfiguration.Name}" /> | Properties
        </TextBlock>

        <StackPanel Grid.Row="1" Grid.Column="0">
            <StackPanel>
                <TextBlock Style="{StaticResource MaterialDesignHeadline6TextBlock}" Margin="0 0 0 14">
                    General
                </TextBlock>
            </StackPanel>
            <TextBox materialDesign:HintAssist.Hint="Profile name"
                     Margin="0 4 0 8"
                     Style="{StaticResource MaterialDesignFilledTextBox}"
                     Text="{Binding ProfileName, UpdateSourceTrigger=PropertyChanged}" />

            <ComboBox materialDesign:HintAssist.Hint="Module"
                      materialDesign:TextFieldAssist.HasClearButton="True"
                      materialDesign:HintAssist.HelperText="Optional and binds the profile to the selected module, making module data available."
                      Margin="0 0 0 32"
                      Style="{StaticResource MaterialDesignFilledComboBox}"
                      SelectedItem="{Binding SelectedModule}">
                <ComboBox.ItemsSource>
                    <CompositeCollection>
                        <ComboBoxItem IsEnabled="False"
                                      Visibility="{Binding HasUsableModules, Converter={x:Static s:BoolToVisibilityConverter.InverseInstance}, Mode=OneWay}">
                            No usable modules found
                        </ComboBoxItem>
                        <CollectionContainer Collection="{Binding Data.Modules, Source={StaticResource DataContextProxy}}" />
                    </CompositeCollection>
                </ComboBox.ItemsSource>
                <ComboBox.ItemsPanel>
                    <ItemsPanelTemplate>
                        <VirtualizingStackPanel />
                    </ItemsPanelTemplate>
                </ComboBox.ItemsPanel>
                <ComboBox.ItemTemplate>
                    <DataTemplate DataType="{x:Type profileEdit:ProfileModuleViewModel}">
                        <StackPanel Orientation="Horizontal">
                            <shared:ArtemisIcon Icon="{Binding Icon}" Width="16" Height="16" Margin="0 0 5 0" />
                            <TextBlock Text="{Binding Name}" />
                        </StackPanel>
                    </DataTemplate>
                </ComboBox.ItemTemplate>
            </ComboBox>

            <ComboBox materialDesign:HintAssist.Hint="Icon type"
                      Margin="0 0 0 8"
                      Style="{StaticResource MaterialDesignFilledComboBox}"
                      SelectedValue="{Binding SelectedIconType}"
                      ItemsSource="{Binding IconTypes}"
                      SelectedValuePath="Value"
                      DisplayMemberPath="Description" />

            <ContentControl VerticalAlignment="Center">
                <ContentControl.Style>
                    <Style TargetType="ContentControl">
                        <Style.Triggers>
                            <DataTrigger Binding="{Binding SelectedIconType}" Value="{x:Static core:ProfileConfigurationIconType.MaterialIcon}">
                                <Setter Property="ContentTemplate">
                                    <Setter.Value>
                                        <DataTemplate>
                                            <Grid>
                                                <Grid.ColumnDefinitions>
                                                    <ColumnDefinition Width="Auto" />
                                                    <ColumnDefinition />
                                                </Grid.ColumnDefinitions>
                                                <materialDesign:PackIcon Grid.Column="0"
                                                                         Kind="{Binding DataContext.SelectedIcon.Icon, RelativeSource={RelativeSource AncestorType={x:Type UserControl}}, Mode=OneWay}"
                                                                         Width="45"
                                                                         Height="45"
                                                                         Margin="0 0 10 0"
                                                                         VerticalAlignment="Center" />

                                                <ComboBox Grid.Column="1"
                                                          materialDesign:HintAssist.Hint="Profile icon"
                                                          IsEditable="True"
                                                          Style="{StaticResource MaterialDesignFilledComboBox}"
                                                          SelectedItem="{Binding DataContext.SelectedIcon, RelativeSource={RelativeSource AncestorType={x:Type UserControl}}}"
                                                          IsEnabled="{Binding DataContext.Initializing, RelativeSource={RelativeSource AncestorType={x:Type UserControl}}, Converter={StaticResource InverseBooleanConverter}}"
                                                          ItemsSource="{Binding DataContext.Icons, RelativeSource={RelativeSource AncestorType={x:Type UserControl}}}"
                                                          TextSearch.TextPath="IconName">
                                                    <ComboBox.ItemsPanel>
                                                        <ItemsPanelTemplate>
                                                            <VirtualizingStackPanel />
                                                        </ItemsPanelTemplate>
                                                    </ComboBox.ItemsPanel>
                                                    <ComboBox.ItemTemplate>
                                                        <DataTemplate DataType="{x:Type profileEdit:ProfileIconViewModel}">
                                                            <StackPanel Orientation="Horizontal">
                                                                <materialDesign:PackIcon Kind="{Binding Icon}" Width="16" Margin="0 0 5 0" />
                                                                <TextBlock Text="{Binding IconName}" />
                                                            </StackPanel>
                                                        </DataTemplate>
                                                    </ComboBox.ItemTemplate>
                                                </ComboBox>
                                            </Grid>
                                        </DataTemplate>
                                    </Setter.Value>
                                </Setter>
                            </DataTrigger>
                            <DataTrigger Binding="{Binding SelectedIconType}" Value="{x:Static core:ProfileConfigurationIconType.BitmapImage}">
                                <Setter Property="ContentTemplate">
                                    <Setter.Value>
                                        <DataTemplate>
                                            <Grid>
                                                <Grid.ColumnDefinitions>
                                                    <ColumnDefinition Width="Auto" />
                                                    <ColumnDefinition />
                                                </Grid.ColumnDefinitions>
                                                <Image Grid.Column="0"
                                                       Source="{Binding DataContext.SelectedImage, Converter={StaticResource StreamToBitmapImageConverter}, RelativeSource={RelativeSource AncestorType={x:Type UserControl}}}"
                                                       Margin="0 0 10 0"
                                                       Width="45"
                                                       Height="45"
                                                       VerticalAlignment="Center"
                                                       RenderOptions.BitmapScalingMode="HighQuality" />
                                                <Button Grid.Column="1"
                                                        Command="{s:Action SelectBitmapFile}"
                                                        Style="{StaticResource MaterialDesignRaisedButton}"
                                                        ToolTip="Select an image">
                                                    <StackPanel Orientation="Horizontal">
                                                        <materialDesign:PackIcon Kind="Paperclip" Margin="0 0 10 0" />
                                                        <TextBlock>BROWSE IMAGE</TextBlock>
                                                    </StackPanel>
                                                </Button>
                                            </Grid>
                                        </DataTemplate>
                                    </Setter.Value>
                                </Setter>
                            </DataTrigger>
                            <DataTrigger Binding="{Binding SelectedIconType}" Value="{x:Static core:ProfileConfigurationIconType.SvgImage}">
                                <Setter Property="ContentTemplate">
                                    <Setter.Value>
                                        <DataTemplate>
                                            <Grid>
                                                <Grid.ColumnDefinitions>
                                                    <ColumnDefinition Width="Auto" />
                                                    <ColumnDefinition />
                                                </Grid.ColumnDefinitions>

                                                <Image Grid.Column="0"
                                                       Source="{Binding DataContext.SelectedImage, Converter={StaticResource StreamToSvgImageConverter}, RelativeSource={RelativeSource AncestorType={x:Type UserControl}}}"
                                                       Margin="0 0 10 0"
                                                       Width="45"
                                                       Height="45"
                                                       VerticalAlignment="Center"
                                                       RenderOptions.BitmapScalingMode="HighQuality" />
                                                <Button Grid.Column="1"
                                                        Command="{s:Action SelectSvgFile}"
                                                        Style="{StaticResource MaterialDesignRaisedButton}"
                                                        ToolTip="Select an image">
                                                    <StackPanel Orientation="Horizontal">
                                                        <materialDesign:PackIcon Kind="Paperclip" Margin="0 0 10 0" />
                                                        <TextBlock>BROWSE IMAGE</TextBlock>
                                                    </StackPanel>
                                                </Button>
                                            </Grid>
                                        </DataTemplate>
                                    </Setter.Value>
                                </Setter>
                            </DataTrigger>
                        </Style.Triggers>
                    </Style>
                </ContentControl.Style>
            </ContentControl>
        </StackPanel>

        <Border Grid.Row="1" Grid.Column="1" BorderThickness="1" BorderBrush="{StaticResource MaterialDesignDivider}" Width="1" Margin="10 0" />

        <Grid Grid.Row="1" Grid.Column="2">
            <Grid.RowDefinitions>
                <RowDefinition Height="Auto" />
                <RowDefinition Height="*" />
            </Grid.RowDefinitions>
            <StackPanel Grid.Row="0">
                <TextBlock Style="{StaticResource MaterialDesignSubtitle1TextBlock}">
                    Hotkeys
                </TextBlock>
                <TextBlock Style="{StaticResource MaterialDesignBody2TextBlock}">You may set up hotkeys to activate/deactivate the profile</TextBlock>

                <ComboBox materialDesign:HintAssist.Hint="Hotkey mode"
                          Margin="0 6 0 8"
                          Style="{StaticResource MaterialDesignFilledComboBox}"
                          SelectedValue="{Binding SelectedHotkeyMode}"
                          ItemsSource="{Binding HotkeyModes}"
                          SelectedValuePath="Value"
                          DisplayMemberPath="Description" />

                <ContentControl s:View.Model="{Binding EnableHotkeyViewModel}"
                                VerticalContentAlignment="Stretch" 
                                HorizontalContentAlignment="Stretch"
                                IsTabStop="False" 
                                Visibility="{Binding ShowEnableHotkey, Converter={x:Static s:BoolToVisibilityConverter.Instance}, Mode=OneWay}"
                                Margin="0 0 0 8"/>
                <ContentControl s:View.Model="{Binding DisableHotkeyViewModel}"
                                VerticalContentAlignment="Stretch" 
                                HorizontalContentAlignment="Stretch"
                                IsTabStop="False" 
                                Visibility="{Binding ShowDisableHotkey, Converter={x:Static s:BoolToVisibilityConverter.Instance}, Mode=OneWay}"/>
            </StackPanel>
        </Grid>

        <Border Grid.Row="2" Grid.Column="0" Grid.ColumnSpan="3" Margin="0 16" BorderThickness="1" Height="1" BorderBrush="{StaticResource MaterialDesignDivider}" />

        <Grid Grid.Row="3" Grid.Column="0" Grid.ColumnSpan="3">
            <Grid.RowDefinitions>
                <RowDefinition Height="Auto" />
                <RowDefinition Height="*" />
                <RowDefinition Height="Auto" />
                <RowDefinition Height="Auto" />
            </Grid.RowDefinitions>
            <StackPanel Grid.Row="0" Margin="0 0 0 4">
                <TextBlock Style="{StaticResource MaterialDesignSubtitle1TextBlock}">
                    Activation conditions
                </TextBlock>
                <TextBlock Style="{StaticResource MaterialDesignBody2TextBlock}">If you only want this profile to be active under certain conditions, configure those conditions below</TextBlock>
            </StackPanel>

            <materialDesign:Card Grid.Row="1" Background="{DynamicResource MaterialDesignToolBarBackground}" Padding="10">
                <ScrollViewer HorizontalScrollBarVisibility="Auto"
                              VerticalScrollBarVisibility="Auto"
                              MinHeight="150"
                              MaxHeight="300"
                              Margin="0 5">
                    <ContentControl s:View.Model="{Binding ActivationConditionViewModel}" />
                </ScrollViewer>
            </materialDesign:Card>

            <Grid Grid.Row="2" Margin="0 10" Visibility="{Binding SelectedModule, Converter={StaticResource NullToVisibilityConverter}, Mode=OneWay}">
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="*" />
                    <ColumnDefinition Width="Auto" />
                    <ColumnDefinition Width="*" />
                </Grid.ColumnDefinitions>
                <Border Grid.Column="0" BorderThickness="1" Height="1" BorderBrush="{StaticResource MaterialDesignDivider}" />
                <TextBlock Grid.Column="1" Margin="16 0" Style="{StaticResource MaterialDesignBody2TextBlock}">AND</TextBlock>
                <Border Grid.Column="2" BorderThickness="1" Height="1" BorderBrush="{StaticResource MaterialDesignDivider}" />
            </Grid>

            <materialDesign:Card Grid.Row="3"
                                 Background="{DynamicResource MaterialDesignToolBarBackground}"
                                 Padding="10"
                                 Visibility="{Binding SelectedModule, Converter={StaticResource NullToVisibilityConverter}, Mode=OneWay}">
                <ContentControl s:View.Model="{Binding ModuleActivationRequirementsViewModel}" />
            </materialDesign:Card>

        </Grid>

        <Grid Grid.Row="4" Grid.Column="0" Grid.ColumnSpan="3" Margin="0 16 0 0">
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="*" />
                <ColumnDefinition Width="Auto" />
            </Grid.ColumnDefinitions>
            <Button Grid.Column="0"
                    HorizontalAlignment="Left"
                    Style="{StaticResource MaterialDesignFlatAccentButton}"
                    Command="{s:Action Delete}"
                    Visibility="{Binding IsNew, Converter={x:Static s:BoolToVisibilityConverter.InverseInstance}, Mode=OneWay}">
                DELETE
            </Button>
            <StackPanel Grid.Column="1" Orientation="Horizontal">
                <Button Style="{StaticResource MaterialDesignFlatButton}" IsCancel="True" Command="{s:Action Cancel}">
                    CANCEL
                </Button>
                <Button Style="{StaticResource MaterialDesignFlatButton}" Command="{s:Action Accept}">
                    ACCEPT
                </Button>
            </StackPanel>
        </Grid>
    </Grid>
</UserControl>