<Window x:Class="Machinations.WPF.UserComponentEditor"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:Machinations.WPF"
        xmlns:machinations="clr-namespace:Machinations"
        xmlns:xctk="http://schemas.xceed.com/wpf/xaml/toolkit"
        xmlns:converters="clr-namespace:Machinations.WPF.Converters"
        mc:Ignorable="d"
        Title="{Binding ComponentName}" 
        DataContext="{Binding RelativeSource={RelativeSource Self}}"
        SizeToContent="WidthAndHeight"
        ResizeMode ="CanMinimize">
    <Window.Resources>
        <BooleanToVisibilityConverter x:Key="BoolToVisConverter" />
         <converters:InverseBoolToVisibilityConverter x:Key="InverseBoolToVisibilityConverter"/>
        <Style TargetType="TextBox" x:Key="TbStyle">
            <Setter Property="MaxWidth" Value="100"/>
        </Style>
    </Window.Resources>
    <Grid KeyDown="OnKeyDown">
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="Auto"/>
            <ColumnDefinition Width="Auto"/>
        </Grid.ColumnDefinitions>
        <local:PaintingCanvas x:Name="PaintingCanvas"/>
        <Grid Grid.Column="1" x:Name="EditPanel">
            <Grid.RowDefinitions>
                <RowDefinition Height="Auto"/>
                <RowDefinition Height="*"/>
        </Grid.RowDefinitions>
         <TabControl Grid.Row="0" Margin="5">
            <TabItem Header="Edit graph" MinWidth="40" HorizontalAlignment="Center">
                <Grid MinHeight="300" Width="240">
                    <ScrollViewer MaxHeight="480" VerticalScrollBarVisibility="Auto">
                        <StackPanel >
                        <machinations:GraphControl x:Name="GraphControl" />
                        <Grid >
                            <Grid.ColumnDefinitions>
                                <ColumnDefinition Width="Auto"/>
                                <ColumnDefinition Width="*"/>
                            </Grid.ColumnDefinitions>
                            <TextBlock Text="ComponentName:" Margin="0,0,10,0"/>
                            <TextBox Grid.Column ="1" Style="{StaticResource TbStyle}" Text="{Binding ComponentName ,UpdateSourceTrigger=PropertyChanged}" />
                        </Grid>
                        <local:UserComponentControlView x:Name="UserComponents" Visibility="{Binding IsEditable, Converter={StaticResource InverseBoolToVisibilityConverter}}"/>
                        <local:ComponentsSettings x:Name="ComponentsSettings"/>
                    </StackPanel>
                    </ScrollViewer>
                </Grid>
            </TabItem>
            <TabItem Header="Card settings"  Visibility="{Binding IsDeck, Converter={StaticResource BoolToVisConverter}}">
                <Grid Name="CardSettings"
                      Width="240">
                            <Grid.RowDefinitions>
                                <RowDefinition Height="Auto"/>
                                <RowDefinition Height="Auto"/>
                                <RowDefinition Height="Auto"/>
                                <RowDefinition Height="Auto"/>
                                <RowDefinition Height="Auto"/>
                                 <RowDefinition Height="Auto"/>
                            </Grid.RowDefinitions>
                            <Grid Grid.Row="0" >
                                <Grid.ColumnDefinitions>
                                    <ColumnDefinition Width="Auto"/>
                                    <ColumnDefinition Width="*"/>
                                </Grid.ColumnDefinitions>
                                <TextBlock Text="CardName:" Margin="0,0,10,0"/>
                                <TextBox Grid.Column="1" Text="{Binding CardName, UpdateSourceTrigger=PropertyChanged}" Width="80" HorizontalAlignment="Right"/>
                            </Grid>
                            <Grid Grid.Row="1">
                                <Grid.ColumnDefinitions>
                                    <ColumnDefinition Width="Auto"/>
                                    <ColumnDefinition Width="34*"/>
                                    <ColumnDefinition Width="95*"/>
                                </Grid.ColumnDefinitions>
                                <TextBlock Text="Density:" Margin="0,0,10,0"/>
                                <xctk:ShortUpDown Grid.Column="1" Value="{Binding CardDensity}" Minimum="0" Maximum="20" Increment="1" Grid.ColumnSpan="2" Width="80" HorizontalAlignment="Right"/>
                            </Grid>
                            <Grid Grid.Row="2">
                                <Grid.ColumnDefinitions>
                                    <ColumnDefinition Width="Auto"/>
                                    <ColumnDefinition Width="*"/>
                                </Grid.ColumnDefinitions>
                                <TextBlock Text="Card:"/>
                                <ComboBox Grid.Column="1" Name="CardComboBox" ItemsSource="{Binding Cards2}" SelectedItem="{Binding SelectedCard}" Width="90" HorizontalAlignment="Right"/>
                            </Grid>
                            <Grid Grid.Row="3">
                                <Grid.ColumnDefinitions>
                                    <ColumnDefinition Width="*"/>
                                    <ColumnDefinition Width="*"/>
                                </Grid.ColumnDefinitions>
                                <Button Content="Add new card" Click="AddCardClick" Margin="2"/>
                                <Button Grid.Column="1" Content="Remove card" Click="RemoveCardClick" Margin="2"/>
                            </Grid>
                             <Grid Grid.Row="4" Margin="0,10,0,0">
                                <Grid.ColumnDefinitions>
                                    <ColumnDefinition Width="*"/>
                                    <ColumnDefinition Width="*"/>
                                </Grid.ColumnDefinitions>
                                <TextBlock Text="Random order: " Margin="0,0,10,0"/>
                                <CheckBox Grid.Column="1" Grid.Row="0" IsChecked="{Binding Shuffle}" HorizontalAlignment="Right" Margin="0,1,30,0"/>
                            </Grid>
                            <Button Grid.Row ="5" Content="Change order" Click="ChangeOrder" Margin="2"/>
                        </Grid>
            </TabItem>
        </TabControl>
            <Grid x:Name="ButtonGrid" Grid.Row="1" VerticalAlignment="Bottom">
                <Grid.RowDefinitions>
                    <RowDefinition Height="Auto"/>
                    <RowDefinition Height="Auto"/>
                    <RowDefinition Height="Auto"/>
                    <RowDefinition Height="Auto"/>
                </Grid.RowDefinitions>
                <Button Grid.Row ="0" Content="Delete" Click="SaveClick" Tag="Delete" Margin="2,10"/>
                <Button Grid.Row ="1" Content="Save as new component" IsEnabled="{Binding HasName}" Click="SaveClick" Tag="ChangeOne"/>
                <Button Grid.Row ="2" Content="Save and overwrite" IsEnabled="{Binding NotFromInitComponent}" Click="SaveClick" Tag="ChangeAll"/>
                <Button Grid.Row="3" Content="Cancel" Click="CancelClick"/>
            </Grid>
        </Grid>
    </Grid>
</Window>
