<!--
Copyright 2019 faddenSoft

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-->

<Window x:Class="SourceGen.WpfGui.FormatAddressTable"
        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:system="clr-namespace:System;assembly=mscorlib"
        xmlns:local="clr-namespace:SourceGen.WpfGui"
        mc:Ignorable="d"
        Title="Format Address Table"
        SizeToContent="WidthAndHeight" ResizeMode="NoResize"
        ShowInTaskbar="False" WindowStartupLocation="CenterOwner"
        Loaded="Window_Loaded">

    <Window.Resources>
        <system:String x:Key="str_SingleByte">Only one byte is selected!</system:String>
        <system:String x:Key="str_SingleGroup">There are {0} bytes selected.</system:String>
        <system:String x:Key="str_MultiGroup">There are {0} bytes selected, across {1} groups.</system:String>

        <BooleanToVisibilityConverter x:Key="BoolToVis"/>
    </Window.Resources>

    <Grid Margin="8">
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="Auto"/>
            <ColumnDefinition Width="*"/>
        </Grid.ColumnDefinitions>
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto"/>
            <RowDefinition Height="*"/>
        </Grid.RowDefinitions>

        <TextBlock Name="selectionInfoLabel" Grid.Column="0" Grid.Row="0" Grid.ColumnSpan="2"
                   Text="{StaticResource str_MultiGroup}"/>

        <StackPanel Grid.Column="0" Grid.Row="1" Margin="0,8,8,0">
            <GroupBox Header="Address Characteristics">
                <StackPanel>
                    <RadioButton Name="width16Radio" GroupName="Addr" Margin="0,4,0,0"
                                 Content="16-bit" Checked="WidthRadio_CheckedChanged"/>
                    <RadioButton Name="width24Radio" GroupName="Addr" Margin="0,4,0,0"
                                 Content="24-bit" Checked="WidthRadio_CheckedChanged"/>
                    <RadioButton Name="width32Radio" GroupName="Addr" Margin="0,4,0,0"
                                 Content="24-bit + pad byte" Checked="WidthRadio_CheckedChanged"/>
                    <CheckBox Margin="0,4,0,0" Content="Parts are split across sub-tables"
                              IsChecked="{Binding IsSplitTable}"/>
                    <CheckBox Margin="0,4,0,0" Content="Adjusted for RTS/RTL (target - 1)"
                              IsChecked="{Binding IsAdjustedForReturn}"/>
                </StackPanel>
            </GroupBox>

            <GroupBox Header="Low Byte Source">
                <StackPanel>
                    <RadioButton Name="lowFirstPartRadio" GroupName="Low" Margin="0,4,0,0"
                                 Content="First part of selection"
                                 Checked="LowByte_CheckedChanged"/>
                    <RadioButton Name="lowSecondPartRadio" GroupName="Low" Margin="0,4,0,0"
                                 Content="Second part of selection"
                                 Checked="LowByte_CheckedChanged"/>
                    <RadioButton Name="lowThirdPartRadio" GroupName="Low" Margin="0,4,0,0"
                                 Content="Third part of selection"
                                 IsEnabled="{Binding IsBankEnabled}"
                                 Checked="LowByte_CheckedChanged"/>
                </StackPanel>
            </GroupBox>

            <GroupBox Header="High Byte Source">
                <StackPanel>
                    <RadioButton Name="highFirstPartRadio" GroupName="High" Margin="0,4,0,0"
                                 Content="First part of selection"
                                 Checked="HighByte_CheckedChanged"/>
                    <RadioButton Name="highSecondPartRadio" GroupName="High" Margin="0,4,0,0"
                                 Content="Second part of selection"
                                 Checked="HighByte_CheckedChanged"/>
                    <RadioButton Name="highThirdPartRadio" GroupName="High" Margin="0,4,0,0"
                                 Content="Third part of selection"
                                 IsEnabled="{Binding IsBankEnabled}"
                                 Checked="HighByte_CheckedChanged"/>
                    <StackPanel Orientation="Horizontal">
                        <RadioButton Name="highConstantRadio" GroupName="High" Margin="0,4,0,0"
                                     Content="Constant:" Checked="HighByte_CheckedChanged"/>
                        <TextBox Name="highConstantTextBox" Width="100" Margin="8,2,0,0"
                                 TextChanged="HighConstantTextBox_TextChanged"/>
                    </StackPanel>
                </StackPanel>
            </GroupBox>

            <GroupBox Header="Bank Byte Source">
                <StackPanel IsEnabled="{Binding IsBankEnabled}">
                    <RadioButton Name="bankNthPartRadio" GroupName="Bank" Margin="0,4,0,0"
                                 Content="Nth part of selection"
                                 Checked="BankByte_CheckedChanged"/>
                    <StackPanel Orientation="Horizontal">
                        <RadioButton Name="bankConstantRadio" GroupName="Bank" Margin="0,4,0,0"
                                     Content="Constant:"
                                     Checked="BankByte_CheckedChanged"/>
                        <TextBox Name="bankConstantTextBox" Width="100" Margin="8,2,0,0"
                                 TextChanged="BankConstantTextBox_TextChanged"/>
                    </StackPanel>
                </StackPanel>
            </GroupBox>

            <GroupBox Header="Options">
                <StackPanel>
                    <CheckBox Content="Tag targets as code start points" Margin="0,4,0,0"
                              IsChecked="{Binding WantCodeStartPoints}"/>
                </StackPanel>
            </GroupBox>
        </StackPanel>

        <Grid Grid.Column="1" Grid.Row="1" Margin="0,8,0,0">
            <DockPanel>
                <StackPanel DockPanel.Dock="Bottom" Orientation="Horizontal" HorizontalAlignment="Right" Margin="0,8,0,0">
                    <Button Name="okButton" Content="OK" IsDefault="True" Width="70"
                        IsEnabled="{Binding IsValid}" Click="OkButton_Click"/>
                    <Button Name="cancelButton" Content="Cancel" IsCancel="True"
                        Width="70" Margin="4,0,0,0"/>
                </StackPanel>

                <GroupBox DockPanel.Dock="Top" Header="Generated Addresses">
                    <!-- We're currently auto-sizing everything outside this, so the width of
                    the columns determines the width of the window.  Resizing the columns resizes
                    the window, even though the window isn't resizeable.  "It's a feature." -->
                    <!-- We set a fixed height so long lists don't make the window absurdly tall. -->
                    <DataGrid Name="outputPreviewList"
                              Margin="0,2,0,0"
                              Height="345"
                              IsReadOnly="True"
                              ItemsSource="{Binding OutputPreviewList}"
                              FontFamily="{StaticResource GeneralMonoFont}"
                              SnapsToDevicePixels="True"
                              GridLinesVisibility="Vertical"
                              VerticalGridLinesBrush="#FF7F7F7F"
                              AutoGenerateColumns="False"
                              HeadersVisibility="Column"
                              CanUserReorderColumns="False"
                              SelectionMode="Single">
                        <DataGrid.Columns>
                            <DataGridTextColumn Header="Addr" Width="60" Binding="{Binding Addr}"/>
                            <DataGridTextColumn Header="Offset" Width="60" Binding="{Binding Offset}"/>
                            <DataGridTextColumn Header="Symbol" Width="180" Binding="{Binding Symbol}"/>
                        </DataGrid.Columns>
                    </DataGrid>
                </GroupBox>
            </DockPanel>

            <TextBlock Name="incompatibleSelectionLabel"
                       VerticalAlignment="Center" HorizontalAlignment="Center" Margin="0,-20,0,0"
                       Text="Options incompatible with selection" FontSize="16" Foreground="Red"
                       Visibility="{Binding IncompatibleSelectionVisibility, Converter={StaticResource BoolToVis}}"/>
            <TextBlock Name="invalidConstantLabel"
                       VerticalAlignment="Center" HorizontalAlignment="Center" Margin="0,20,0,0"
                       Text="Invalid constant" FontSize="16" Foreground="Red"
                       Visibility="{Binding InvalidConstantVisibility, Converter={StaticResource BoolToVis}}"/>
        </Grid>
    </Grid>
</Window>
