<!--
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.EditInstructionOperand"
        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="Edit Instruction Operand"
        SizeToContent="WidthAndHeight" ResizeMode="NoResize"
        ShowInTaskbar="False" WindowStartupLocation="CenterOwner"
        Loaded="Window_Loaded"
        ContentRendered="Window_ContentRendered">

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

        <system:String x:Key="str_SymbolNotUsed">N/A</system:String>
        <system:String x:Key="str_SymbolNotValid">[invalid symbol name]</system:String>
        <system:String x:Key="str_SymbolUnknown">?</system:String>

        <system:String x:Key="str_CreateLocalVariable">Create Local _Variable</system:String>
        <system:String x:Key="str_EditLocalVariable">Edit Local _Variable</system:String>
        <system:String x:Key="str_LvMatchFoundAddress">Match found (ZP address):</system:String>
        <system:String x:Key="str_LvMatchFoundConstant">Match found (StackRel constant):</system:String>

        <system:String x:Key="str_CreateLabel">Create _Label</system:String>
        <system:String x:Key="str_EditLabel">Edit _Label</system:String>
        <system:String x:Key="str_CreateProjectSymbol">Create _Project Symbol</system:String>
        <system:String x:Key="str_EditProjectSymbol">Edit _Project Symbol</system:String>
        <system:String x:Key="str_CurrentLabel">Current label:</system:String>
        <system:String x:Key="str_CurrentLabelAdjustedFmt">Current label (adjusted {0}):</system:String>
    </Window.Resources>

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

        <TextBlock Grid.Column="0" Grid.Row="0" Grid.ColumnSpan="2" Margin="0,0,0,8"
                   Text="Select operand format:"/>

        <StackPanel Grid.Column="0" Grid.Row="1" Grid.ColumnSpan="2" Orientation="Horizontal" Margin="0,0,0,8">
            <RadioButton GroupName="Main" Margin="0,2,0,0"
                         Content="D_efault (auto-format as hex, local var, or reference to label/platform/project symbol)"
                         IsChecked="{Binding FormatDefault}"/>
        </StackPanel>

        <GroupBox Grid.Column="0" Grid.Row="2" Grid.ColumnSpan="2" Padding="2,4" Header="Explicit Format">
            <Grid>
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="*"/>
                    <ColumnDefinition Width="*"/>
                </Grid.ColumnDefinitions>

                <StackPanel Grid.Column="0">
                    <RadioButton GroupName="Main" Content="_Hexadecimal"
                                 IsChecked="{Binding FormatHex}"/>
                    <RadioButton GroupName="Main" Content="_Decimal" Margin="0,2,0,0"
                                 IsChecked="{Binding FormatDecimal}"/>
                    <RadioButton GroupName="Main" Content="Si_gned Decimal" Margin="0,2,0,0"
                                 IsEnabled="{Binding IsFormatSignedDecimalAllowed}" IsChecked="{Binding FormatSignedDecimal}"/>
                    <RadioButton GroupName="Main" Content="_Binary" Margin="0,2,0,0"
                                 IsChecked="{Binding FormatBinary}"/>
                    <RadioButton GroupName="Main" Content="_ASCII (low or high) character" Margin="0,2,0,0"
                                 IsEnabled="{Binding IsFormatAsciiAllowed}" IsChecked="{Binding FormatAscii}"/>
                    <RadioButton GroupName="Main" Content="C64 PE_TSCII character" Margin="0,2,0,0"
                                 IsEnabled="{Binding IsFormatPetsciiAllowed}" IsChecked="{Binding FormatPetscii}"/>
                    <RadioButton GroupName="Main" Content="C64 Sc_reen character" Margin="0,2,0,0"
                                 IsEnabled="{Binding IsFormatScreenCodeAllowed}" IsChecked="{Binding FormatScreenCode}"/>
                </StackPanel>

                <Grid Grid.Column="1">
                    <Grid.ColumnDefinitions>
                        <ColumnDefinition Width="Auto"/>
                    </Grid.ColumnDefinitions>
                    <Grid.RowDefinitions>
                        <RowDefinition Height="Auto"/>
                        <RowDefinition Height="Auto"/>
                        <RowDefinition Height="Auto"/>
                        <RowDefinition Height="*"/>
                        <RowDefinition Height="Auto"/>
                    </Grid.RowDefinitions>
                    <RadioButton Name="formatSymbolButton" Grid.Column="0" Grid.Row="0" GroupName="Main"
                                 Content="_Symbol" IsChecked="{Binding FormatSymbol}"/>
                    <StackPanel Grid.Column="0" Grid.Row="1" Orientation="Horizontal" Margin="18,1,0,0">
                        <TextBox Name="symbolTextBox" Grid.Column="0" Grid.Row="1" Width="150"
                                 FontFamily="{StaticResource GeneralMonoFont}"
                                 Text="{Binding SymbolLabel, UpdateSourceTrigger=PropertyChanged}"/>
                        <TextBlock Text="(auto)" Foreground="Red" Margin="4,0,0,0"
                                   Visibility="{Binding IsSymbolAuto, Converter={StaticResource BoolToVis}}"/>
                        <TextBlock Text="(var)" Foreground="Red" Margin="4,0,0,0"
                                   Visibility="{Binding IsSymbolVar, Converter={StaticResource BoolToVis}}"/>
                    </StackPanel>
                    <StackPanel Grid.Column="0" Grid.Row="2" Orientation="Horizontal" Margin="18,4,0,0"
                                IsEnabled="{Binding IsPartPanelEnabled}">
                        <RadioButton GroupName="Part" Content="Low"
                                     IsChecked="{Binding FormatPartLow}"/>
                        <RadioButton GroupName="Part" Content="H_igh" Margin="10,0,0,0"
                                     IsChecked="{Binding FormatPartHigh}"/>
                        <RadioButton GroupName="Part" Content="Bank" Margin="10,0,0,0"
                                     IsChecked="{Binding FormatPartBank}"/>
                    </StackPanel>

                    <!-- show the symbol's value -->
                    <Grid Grid.Column="0" Grid.Row="3" Margin="18,8,0,0">
                        <Grid.ColumnDefinitions>
                            <ColumnDefinition Width="Auto"/>
                            <ColumnDefinition Width="Auto"/>
                        </Grid.ColumnDefinitions>
                        <Grid.RowDefinitions>
                            <RowDefinition Height="Auto"/>
                            <RowDefinition Height="Auto"/>
                        </Grid.RowDefinitions>
                        <TextBlock Grid.Column="0" Grid.Row="0" Text="Value:" Margin="0,0,16,0"/>
                        <TextBlock Grid.Column="1" Grid.Row="0"
                                   Text="{Binding SymbolValueHex, FallbackValue=$112233}"/>
                        <TextBlock Grid.Column="1" Grid.Row="1"
                                   Text="{Binding SymbolValueDecimal, FallbackValue=16777215}"/>
                    </Grid>

                    <CheckBox Grid.Column="0" Grid.Row="4" Content="Disregard operand address"
                              IsChecked="{Binding DisregardOperandAddress}"
                              IsEnabled="{Binding IsDisregardEnabled}"/>
                </Grid>
            </Grid>
        </GroupBox>

        <!-- Preview box spans the full window.  Most of the time this isn't necessary, but
        sometimes you get long symbols with complex expressions, and the window needs to be
        fairly wide.  So might as well just use it all. -->
        <DockPanel Grid.Column="0" Grid.Row="3" Grid.ColumnSpan="2" Margin="0,8">
            <TextBlock DockPanel.Dock="Left" Text="Preview:"/>
            <TextBox DockPanel.Dock="Left" IsReadOnly="True" Margin="8,1,0,0"
                     FontFamily="{StaticResource GeneralMonoFont}" Text="{Binding PreviewText}"/>
        </DockPanel>

        <!-- numeric reference box, for labels and project/platform symbols -->
        <GroupBox Grid.Column="0" Grid.Row="4" Margin="0,4,0,0" Padding="2,4" Header="Numeric Address Reference">
            <StackPanel>
                <TextBlock Text="Operand is not an 8, 16, or 24-bit address" TextWrapping="Wrap"
                           Visibility="{Binding ShowNarNotAddress, Converter={StaticResource BoolToVis}}"/>
                <!--<TextBlock Text="Operand does not reference the start of an instruction or data item"
                            TextWrapping="Wrap" IsEnabled="{Binding ShowNarNotStart}"/>-->
                <StackPanel Visibility="{Binding ShowNarEditLabel, Converter={StaticResource BoolToVis}}">
                    <TextBlock Text="Operand's address is inside project"/>
                    <StackPanel Visibility="{Binding ShowNarCurrentLabel, Converter={StaticResource BoolToVis}}">
                        <TextBlock Text="{Binding NarLabelOffsetText, FallbackValue=Current label (adjusted +1):}"/>
                        <TextBlock Margin="16,2,0,0" FontFamily="{StaticResource GeneralMonoFont}"
                                   Text="{Binding NarTargetLabel, FallbackValue=LABEL_67890123456789012345678901}"/>
                    </StackPanel>
                    <Button Width="150" Margin="0,8,0,0" HorizontalAlignment="Left"
                            Content="{Binding CreateEditLabelText, FallbackValue=Diddle Label}"
                            IsEnabled="{Binding IsDiddleLabelEnabled}" Click="EditLabel_Click"/>
                </StackPanel>

                <StackPanel Visibility="{Binding ShowNarExternalSymbol, Converter={StaticResource BoolToVis}}">
                    <TextBlock Text="Operand's address is outside project" Margin="0,0,0,2"/>
                    <StackPanel Visibility="{Binding ShowNarPlatformSymbol, Converter={StaticResource BoolToVis}}">
                        <TextBlock Text="Found a matching symbol in platform defs:"/>
                        <TextBlock Margin="16,2,0,0" FontFamily="{StaticResource GeneralMonoFont}"
                                   Text="{Binding NarPlatformSymbol, FallbackValue=PLATFORM_SYMBOL}"/>
                    </StackPanel>
                    <TextBlock Text="No matching project symbol"
                               Visibility="{Binding ShowNarNoProjectMatch, Converter={StaticResource BoolToVis}}"/>
                    <StackPanel Visibility="{Binding ShowNarProjectSymbol, Converter={StaticResource BoolToVis}}">
                        <TextBlock Text="Found matching project symbol:"/>
                        <TextBlock Margin="16,2,0,0" FontFamily="{StaticResource GeneralMonoFont}"
                                   Text="{Binding NarProjectSymbol, FallbackValue=PROJECT_SYMBOL}"/>
                    </StackPanel>

                    <Button Width="150" Margin="0,8,0,0" HorizontalAlignment="Left"
                            Content="{Binding CreateEditProjectSymbolText, FallbackValue=Diddle Project Symbol}"
                            Click="EditProjectSymbol_Click"/>
                    <Button Width="150" Margin="0,8,0,0" HorizontalAlignment="Left"
                            Content="Copy to Operand" IsEnabled="{Binding IsCopyToOperandEnabled}"
                            Click="CopyToOperandButton_Click"/>
                </StackPanel>

            </StackPanel>
        </GroupBox>

        <!-- local variable controls -->
        <GroupBox Grid.Column="1" Grid.Row="4" Margin="0,4,0,0" Padding="2,4" Header="Local Variable">
            <StackPanel>
                <TextBlock Text="Operand is not a zero page address or stack-relative constant" TextWrapping="Wrap"
                           Visibility="{Binding ShowLvNotApplicable, Converter={StaticResource BoolToVis}}"/>
                <TextBlock Text="No local variable tables have been defined before this point" TextWrapping="Wrap"
                           Visibility="{Binding ShowLvTableNotFound, Converter={StaticResource BoolToVis}}"/>
                <TextBlock Text="No matching local variable found" TextWrapping="Wrap"
                           Visibility="{Binding ShowLvNoMatchFound, Converter={StaticResource BoolToVis}}"/>
                <StackPanel Visibility="{Binding ShowLvMatchFound, Converter={StaticResource BoolToVis}}">
                    <TextBlock Text="{Binding LvMatchFoundText, FallbackValue=Match found (ZP address):}"/>
                    <TextBlock Margin="16,2,0,0" FontFamily="{StaticResource GeneralMonoFont}"
                               Text="{Binding LocalVariableLabel, FallbackValue=VAR_NAME_90123456789012345678901}"/>
                </StackPanel>
                <Button Width="150" Margin="0,8,0,0" HorizontalAlignment="Left"
                        Content="{Binding CreateEditLocalVariableText, FallbackValue=Diddle Local Variable}"
                        Visibility="{Binding ShowLvCreateEditButton, Converter={StaticResource BoolToVis}}"
                        Click="EditLocalVariableButton_Click"/>
            </StackPanel>
        </GroupBox>

        <DockPanel Grid.Column="0" Grid.Row="5" Grid.ColumnSpan="2" Margin="0,8,0,0" LastChildFill="False">
            <Button DockPanel.Dock="Right" Content="Cancel" Width="70" IsCancel="True" Margin="8,0,0,0"/>
            <Button Name="okButton" DockPanel.Dock="Right" Content="OK" Width="70" IsDefault="True"
                    IsEnabled="{Binding IsValid}" Click="OkButton_Click"/>
        </DockPanel>
    </Grid>
</Window>
