<UserControl x:Class="NSwagStudio.Views.DocumentView"
             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:viewModels="clr-namespace:NSwagStudio.ViewModels"
             xmlns:converters="clr-namespace:MyToolkit.Converters;assembly=MyToolkit.Extended"
             mc:Ignorable="d" 
             d:DesignHeight="300" d:DesignWidth="1000">
    
    <UserControl.Resources>
        <viewModels:DocumentViewModel x:Key="ViewModel" />
        <converters:NotConverter x:Key="NotConverter" />
        <converters:VisibilityConverter x:Key="VisibilityConverter" />
    </UserControl.Resources>

    <Border DataContext="{StaticResource ViewModel}" IsEnabled="{Binding IsLoading, Converter={StaticResource NotConverter}}">
        <Grid>
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="2*" MinWidth="400" />
                <ColumnDefinition Width="Auto" />
                <ColumnDefinition Width="3*" MinWidth="400" />
            </Grid.ColumnDefinitions>

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

            <!-- Input -->
            <Grid Margin="0,0,4,0" Grid.RowSpan="2">
                <Grid.RowDefinitions>
                    <RowDefinition Height="Auto" />
                    <RowDefinition Height="Auto" />
                    <RowDefinition Height="*" />
                </Grid.RowDefinitions>

                <StackPanel>
                    <TextBlock FontSize="16" Margin="0,0,0,16">
                        <Run Text="Input:" FontWeight="Bold" />
                        <Run Text="{Binding SelectedItem.Title, ElementName=InputTabControl, Mode=OneWay}" />
                    </TextBlock>

                    <TextBlock Margin="0,0,0,6" Text="Runtime" TextWrapping="Wrap" FontWeight="Bold" />
                    <ComboBox SelectedItem="{Binding Document.Document.Runtime, Mode=TwoWay}" 
                              ItemsSource="{Binding Runtimes}" Margin="0,0,0,6" />
                    <TextBlock Text="Specifies the used command line binary; should match the selected assembly type." 
                               TextWrapping="Wrap" Margin="0,0,0,12" />

                    <TextBlock Margin="0,0,0,4" Text="Default Variables ('foo=bar,baz=bar'), usage: $(foo)" TextWrapping="Wrap" FontWeight="Bold" />
                    <TextBox Margin="0,0,0,12" Text="{Binding Document.Document.DefaultVariables}" />
                </StackPanel>
                <TabControl Grid.Row="2" x:Name="InputTabControl"
                            ItemsSource="{Binding Document.SwaggerGeneratorViews}" 
                            SelectedValuePath="Command"
                            SelectedValue="{Binding Document.Document.SelectedSwaggerGenerator, Mode=TwoWay}">
                    <TabControl.ItemContainerStyle>
                        <Style TargetType="{x:Type TabItem}">
                            <Setter Property="Header" Value="{Binding Title}" />
                        </Style>
                    </TabControl.ItemContainerStyle>
                </TabControl>
            </Grid>

            <GridSplitter Grid.Column="1" Grid.Row="0" Grid.RowSpan="2"
                          Width="4" Margin="4,0,4,0"
                          ResizeBehavior="PreviousAndNext" />

            <!-- Output -->
            <Grid Grid.Column="2" Grid.Row="0" Margin="4,0,0,0">
                <Grid.RowDefinitions>
                    <RowDefinition Height="Auto" />
                    <RowDefinition Height="Auto" />
                    <RowDefinition Height="*" />
                </Grid.RowDefinitions>

                <TextBlock Text="Outputs" FontSize="16" FontWeight="Bold" Margin="0,0,0,8" />
                <ItemsControl Grid.Row="1" ItemsSource="{Binding Document.CodeGenerators}" Margin="0,0,0,8">
                    <ItemsControl.ItemsPanel>
                        <ItemsPanelTemplate>
                            <StackPanel Orientation="Horizontal"/>
                        </ItemsPanelTemplate>
                    </ItemsControl.ItemsPanel>
                    <ItemsControl.ItemTemplate>
                        <DataTemplate>
                            <CheckBox IsChecked="{Binding View.IsSelected}" 
                                      Visibility="{Binding View.IsPersistent, Converter={StaticResource NotConverter}}"
                                      Content="{Binding View.Title}"
                                      ToolTip="{Binding View.PropertyName}"
                                      Margin="0,0,16,0" />
                        </DataTemplate>
                    </ItemsControl.ItemTemplate>
                </ItemsControl>

                <TabControl Grid.Row="2" ItemsSource="{Binding Document.CodeGenerators}" x:Name="OutputTabs">
                    <TabControl.ItemContainerStyle>
                        <Style TargetType="{x:Type TabItem}">
                            <Setter Property="Header" Value="{Binding View.Title}" />
                        </Style>
                    </TabControl.ItemContainerStyle>
                    <TabControl.ContentTemplate>
                        <DataTemplate>
                            <ContentPresenter Content="{Binding View}" />
                        </DataTemplate>
                    </TabControl.ContentTemplate>
                </TabControl>
            </Grid>

            <Grid Grid.Row="1" Grid.Column="2" Height="50" Margin="4,8,0,0" Visibility="{Binding IsLoading, Converter={StaticResource NotConverter}}">
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="*"></ColumnDefinition>
                    <ColumnDefinition Width="Auto"></ColumnDefinition>
                </Grid.ColumnDefinitions>
                <Button Command="{Binding GenerateCommand}" Click="OnGenerate" Padding="8">
                    <TextBlock Text="Generate Outputs" FontSize="16" Margin="0,4,0,4" />
                </Button>
                <Button Grid.Column="1" 
                        Command="{Binding GenerateCommand}" 
                        CommandParameter="files"
                        Click="OnGenerate" Padding="8" Margin="8,0,0,0">
                    <TextBlock Text="Generate Files" FontSize="16" Margin="8,4,8,4" />
                </Button>
            </Grid>
            <ProgressBar Grid.Row="1" Grid.Column="2" IsIndeterminate="True" Height="50" Margin="4,8,0,0"
                         Visibility="{Binding IsLoading, Converter={StaticResource VisibilityConverter}}">
            </ProgressBar>
        </Grid>
    </Border>
</UserControl>
