<UserControl x:Class="Microscopic_Traffic_Simulator.Views.MainWindowView.SimulationControlUserControl"
             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:converters="clr-namespace:Microscopic_Traffic_Simulator.Converters"
             xmlns:sys="clr-namespace:System;assembly=mscorlib"
             mc:Ignorable="d" 
             d:DesignHeight="100" d:DesignWidth="1000" Loaded="UserControl_Loaded"
             Unloaded="UserControl_Unloaded">
    <UserControl.Resources>
        <converters:FromSimulationSpeedToString x:Key="fromSimulationSpeedToString"/>
        <converters:FromTimeSpanToHoursString x:Key="fromTimeSpanToHoursString"/>
        <converters:FromSimulationStepsToPauseToString x:Key="fromSimulationStepsToPauseToString"/>
    </UserControl.Resources>
    <WrapPanel Orientation="Horizontal">
        <WrapPanel.Resources>
            <Style x:Key="TextBoxAligned" TargetType="TextBox">
                <Setter Property="Margin" 
                        Value="4">
                </Setter>
                <Setter Property="VerticalAlignment" 
                        Value="Center">
                </Setter>
            </Style>
            <Style x:Key="ControlsPanel" TargetType="StackPanel">
                <Setter Property="Orientation"
                        Value="Horizontal">
                </Setter>
                <Setter Property="Background"
                        Value="#FFF0F0F0">
                </Setter>
            </Style>
            <Style x:Key="LabelsAlignment" TargetType="Label">
                <Setter Property="VerticalAlignment" Value="Center"/>
            </Style>
        </WrapPanel.Resources>
        <Border>
            <StackPanel Orientation="Horizontal">
                <Button Command="{Binding Path=SwitchToConstructionModeCommand}"
                        Click="SwitchToConstructionModeButton_Click">
                    <StackPanel>
                        <Image Source="{StaticResource Road}"
                               Style="{StaticResource ImageEnabled}"></Image>
                    </StackPanel>
                </Button>
                <Border>
                    <Rectangle Style="{StaticResource rectangleAsDivider}"></Rectangle>
                </Border>
                <Button Command="{Binding Path=PauseCommand}">
                    <StackPanel>
                        <Image Source="{StaticResource Pause}"
                               Style="{StaticResource ImageEnabled}"/>
                    </StackPanel>
                </Button>
                <Button Command="{Binding Path=ForwardStepCommand}"
                        Click="ForwardStepButton_Click">
                    <StackPanel>
                        <Image Source="{StaticResource ForwardStep}"
                               Style="{StaticResource ImageEnabled}"></Image>
                    </StackPanel>
                </Button>
                <Button Command="{Binding Path=ForwardCommand}"
                        Click="ForwardButton_Click">
                    <StackPanel>
                        <Image Source="{StaticResource Forward}"
                               Style="{StaticResource ImageEnabled}"/>
                    </StackPanel>
                </Button>
                <Border>
                    <Rectangle Style="{StaticResource rectangleAsDivider}"></Rectangle>
                </Border>
            </StackPanel>
        </Border>
        <Border>
            <StackPanel Style="{StaticResource ControlsPanel}">
                <StackPanel.Resources>
                    <Style x:Key="SeedLabels" TargetType="Label" BasedOn="{StaticResource LabelsAlignment}">
                        <Setter Property="Visibility" 
                                Value="Collapsed"/>
                        <Style.Triggers>
                            <DataTrigger Binding="{Binding ElementName=btnStop, Path=IsEnabled}" 
                                         Value="True">
                                <Setter Property="Visibility"
                                        Value="Visible"/>
                            </DataTrigger>
                        </Style.Triggers>
                    </Style>
                </StackPanel.Resources>
                <Label Style="{StaticResource SeedLabels}" Content="{DynamicResource Seed}">
                    <Label.Padding>
                        <Thickness Right="0" Left="5"></Thickness>
                    </Label.Padding>
                </Label>
                <Label Style="{StaticResource SeedLabels}" 
                       Content="{Binding Path=Seed, FallbackValue=1234}"/>
                <ToggleButton x:Name="btnCustomSeed"
                              IsChecked="{Binding Path=IsSeedSettingAutomatic}">
                    <StackPanel>
                        <Image Source="{StaticResource Dice}"
                               Style="{StaticResource ImageEnabled}"/>
                    </StackPanel>
                    <ToggleButton.Style>
                        <Style TargetType="ToggleButton">
                            <Setter Property="Visibility" Value="Collapsed"></Setter>
                            <Style.Triggers>
                                <DataTrigger Binding="{Binding ElementName=btnStop, Path=IsEnabled}" 
                                             Value="False">
                                    <Setter Property="Visibility" Value="Visible"></Setter>
                                </DataTrigger>
                            </Style.Triggers>
                        </Style>
                    </ToggleButton.Style>
                </ToggleButton>
                <ToggleButton x:Name="btnUsePreviousSeed"
                              IsChecked="{Binding Path=IsPreviousSeedUsed}"
                              IsEnabled="{Binding Path=IsPreviousSeedAvailable}">
                    <StackPanel>
                        <Image Source="{StaticResource PreviousSeed}"
                               Style="{StaticResource ImageEnabled}"/>
                    </StackPanel>
                    <ToggleButton.Style>
                        <Style TargetType="ToggleButton">
                            <Setter Property="Visibility" Value="Collapsed"></Setter>
                            <Style.Triggers>
                                <MultiDataTrigger>
                                    <MultiDataTrigger.Conditions>
                                        <Condition Binding="{Binding ElementName=btnCustomSeed, Path=Visibility}" 
                                                   Value="Visible"/>
                                        <Condition Binding="{Binding ElementName=btnCustomSeed, Path=IsChecked}" 
                                                   Value="False"/>
                                    </MultiDataTrigger.Conditions>
                                    <MultiDataTrigger.Setters>
                                        <Setter Property="Visibility"
                                                Value="Visible"/>
                                    </MultiDataTrigger.Setters>
                                </MultiDataTrigger>                                
                            </Style.Triggers>
                        </Style>
                    </ToggleButton.Style>
                </ToggleButton>
                <TextBox Width="50px"
                         Text="{Binding Path=CustomSeed}">
                    <TextBox.Style>
                        <Style TargetType="TextBox"
                               BasedOn="{StaticResource TextBoxAligned}" >
                            <Setter Property="Visibility" Value="Collapsed"/>
                            <Style.Triggers>
                                <MultiDataTrigger>
                                    <MultiDataTrigger.Conditions>
                                        <Condition Binding="{Binding ElementName=btnCustomSeed, Path=Visibility}" 
                                                   Value="Visible"/>
                                        <Condition Binding="{Binding ElementName=btnCustomSeed, Path=IsChecked}"
                                                   Value="False"/>
                                        <Condition Binding="{Binding ElementName=btnUsePreviousSeed, Path=IsChecked}" 
                                                   Value="False"/>
                                    </MultiDataTrigger.Conditions>
                                    <MultiDataTrigger.Setters>
                                        <Setter Property="Visibility" Value="Visible"/>
                                    </MultiDataTrigger.Setters>
                                </MultiDataTrigger>
                            </Style.Triggers>
                        </Style>
                    </TextBox.Style>
                </TextBox>
                <Border>
                    <Rectangle Style="{StaticResource rectangleAsDivider}"/>
                </Border>
            </StackPanel>
        </Border>
        <Border>
            <StackPanel Style="{StaticResource ControlsPanel}">
                <StackPanel.Resources>
                    <Style x:Key="TextBoxVisibility" TargetType="TextBox" 
                               BasedOn="{StaticResource TextBoxAligned}">
                        <Setter Property="Visibility" Value="Collapsed"/>
                        <Style.Triggers>
                            <DataTrigger Binding="{Binding ElementName=btnAlarm, 
                                            Path=IsChecked}" Value="True">
                                <Setter Property="Visibility" Value="Visible"/>
                            </DataTrigger>
                        </Style.Triggers>
                    </Style>
                    <Style x:Key="TickButtonVisibility" TargetType="Button">
                        <Setter Property="Visibility" Value="Collapsed"/>
                        <Style.Triggers>
                            <DataTrigger Binding="{Binding ElementName=btnAlarm, 
                                            Path=IsChecked}" Value="True">
                                <Setter Property="Visibility" Value="Visible"/>
                            </DataTrigger>
                        </Style.Triggers>
                    </Style>
                    <Style x:Key="CrossButtonVisibility" 
                                   TargetType="Button">
                        <Setter Property="Visibility" Value="Visible"></Setter>
                    </Style>
                    <x:Static x:Key="UlongMax" Member="sys:UInt64.MaxValue"/>
                    <x:Static x:Key="TimeSpanMax" Member="sys:TimeSpan.MaxValue"/>
                </StackPanel.Resources>
                <ToggleButton x:Name="btnAlarm">
                    <StackPanel>
                        <Image Source="{StaticResource Clock}"
                               Style="{StaticResource ImageEnabled}"></Image>
                    </StackPanel>
                </ToggleButton>
                <Label Style="{StaticResource LabelsAlignment}" Content="{DynamicResource ModelTime}">                    
                    <Label.Padding>
                        <Thickness Right="0" Left="5"></Thickness>
                    </Label.Padding>
                </Label>
                <Label Style="{StaticResource LabelsAlignment}" Content="{Binding ModelTime, Converter={StaticResource fromTimeSpanToHoursString}, 
                        FallbackValue=0:00:00.000}"/>
                <Label Style="{StaticResource LabelsAlignment}" 
                       Content="{Binding Path=ModelTimeToPause, 
                            Converter={StaticResource fromTimeSpanToHoursString}}"
                       Foreground="Orange">
                </Label>
                <Button Command="{Binding Path=ResetModelTimeAlarmCommand}">
                    <StackPanel>
                        <Image Source="{StaticResource Cross}"
                               Style="{StaticResource ImageEnabled}"/>
                    </StackPanel>
                    <Button.Style>
                        <Style TargetType="Button"
                                       BasedOn="{StaticResource CrossButtonVisibility}">
                            <Style.Triggers>
                                <DataTrigger Binding="{Binding Path=ModelTimeToPause}"
                                             Value="{StaticResource TimeSpanMax}">
                                    <Setter Property="Visibility"
                                            Value="Collapsed">
                                    </Setter>
                                </DataTrigger>
                            </Style.Triggers>
                        </Style>
                    </Button.Style>
                </Button>
                <TextBox x:Name="txtModelTimeAlarm" 
                         Width="100px"
                         Style="{StaticResource TextBoxVisibility}"
                         Text="{Binding Path=ModelTimeToPauseToSet, Converter={StaticResource fromTimeSpanToHoursString}}">
                </TextBox>
                <Button Command="{Binding Path=ApplyTimeAlarmCommand}"
                        Style="{StaticResource TickButtonVisibility}"
                        Click="ApplyAlarmButton_Click">
                    <StackPanel>
                        <Image Source="{StaticResource Tick}"
                               Style="{StaticResource ImageEnabled}"/>
                    </StackPanel>
                </Button>
                <Label Style="{StaticResource LabelsAlignment}" Content="{DynamicResource SimulationSteps}">
                    <Label.Padding>
                        <Thickness Right="0" Left="5"></Thickness>                        
                    </Label.Padding>
                </Label>
                <Label Style="{StaticResource LabelsAlignment}" Content="{Binding Path=SimulationSteps, FallbackValue=0}"/>
                <Label Style="{StaticResource LabelsAlignment}" Content="{Binding Path=SimulationStepsToPause, 
                            Converter={StaticResource fromSimulationStepsToPauseToString}}"
                       Foreground="Orange">
                </Label>
                <Button Command="{Binding Path=ResetSimulationStepsAlarmCommand}">
                    <StackPanel>
                        <Image Source="{StaticResource Cross}"
                               Style="{StaticResource ImageEnabled}"/>
                    </StackPanel>
                    <Button.Style>
                        <Style TargetType="Button"
                               BasedOn="{StaticResource CrossButtonVisibility}">
                            <Style.Triggers>
                                <DataTrigger Binding="{Binding Path=SimulationStepsToPause}"
                                             Value="{StaticResource UlongMax}">
                                    <Setter Property="Visibility"
                                            Value="Collapsed">
                                    </Setter>
                                </DataTrigger>
                            </Style.Triggers>
                        </Style>
                    </Button.Style>
                </Button>
                <TextBox x:Name="txtSimStepsAlarm" 
                         Width="100px"
                         Style="{StaticResource TextBoxVisibility}"
                         Text="{Binding Path=SimulationStepsToPauseToSet, Converter={StaticResource fromSimulationStepsToPauseToString}}">
                </TextBox>
                <Button Command="{Binding Path=ApplySimulationStepsAlarmCommand}"
                        Style="{StaticResource TickButtonVisibility}"
                        Click="ApplyAlarmButton_Click">
                    <StackPanel>
                        <Image Source="{StaticResource Tick}"
                               Style="{StaticResource ImageEnabled}"/>
                    </StackPanel>
                </Button>
                <Border>
                    <Rectangle Style="{StaticResource rectangleAsDivider}"></Rectangle>
                </Border>
            </StackPanel>
        </Border>
        <Border>
            <StackPanel Style="{StaticResource ControlsPanel}">
                <Label Style="{StaticResource LabelsAlignment}" Content="{DynamicResource SimulationSpeed}"/>
                <Button Command="{Binding Path=SlowerCommand}">
                    <StackPanel>
                        <Image Source="{StaticResource Slower}"
                               Style="{StaticResource ImageEnabled}"/>
                    </StackPanel>
                </Button>
                <Label Content="{Binding Path=SimulationSpeed, Mode=OneWay,
                Converter={StaticResource fromSimulationSpeedToString}, FallbackValue=1x}"
                       IsEnabled="{Binding Path=IsNotMaxSimulationSpeed}"
                       Style="{StaticResource LabelsAlignment}">
                </Label>
                <Button Command="{Binding Path=FasterCommand}">
                    <StackPanel>
                        <Image Source="{StaticResource Faster}"
                               Style="{StaticResource ImageEnabled}"/>
                    </StackPanel>
                </Button>
                <TextBox Width="30px"                             
                         Style="{StaticResource TextBoxAligned}"
                         Text="{Binding Path=CustomSimulationSpeed, 
                Converter={StaticResource fromSimulationSpeedToString}, FallbackValue=1x}"
                         IsEnabled="{Binding Path=IsNotMaxSimulationSpeed}">
                </TextBox>
                <Button Command="{Binding Path=CustomSimulationSpeedCommand}">
                    <StackPanel>
                        <Image Source="{StaticResource Tick}"
                               Style="{StaticResource ImageEnabled}"/>
                    </StackPanel>
                </Button>
                <ToggleButton IsChecked="{Binding Path=IsMaxSimulationSpeed, 
                        Mode=OneWayToSource}">
                    <StackPanel>
                        <Image Source="{StaticResource Fastest}"
                               Style="{StaticResource ImageEnabled}"/>
                    </StackPanel>
                </ToggleButton>
                <Border>
                    <Rectangle Style="{StaticResource rectangleAsDivider}"></Rectangle>
                </Border>
            </StackPanel>
        </Border>
        <Border>
            <StackPanel Style="{StaticResource ControlsPanel}">
                <Label Content="{DynamicResource GraphicsSpeed}" Style="{StaticResource LabelsAlignment}"/>
                <Slider x:Name="sliderGraphicsTimer"
                        ValueChanged="sliderGraphicsTimer_ValueChanged"                            
                        Minimum="1"
                        Maximum="50"
                        Value="25"
                        IsSnapToTickEnabled="True"
                        Width="100"
                        VerticalAlignment="Center"></Slider>
                <Label Content="{Binding ElementName=sliderGraphicsTimer, Path=Value}"
                           ContentStringFormat="{}{0:#} fps" Style="{StaticResource LabelsAlignment}">
                </Label>
                <Border>
                    <Rectangle Style="{StaticResource rectangleAsDivider}"></Rectangle>
                </Border>
            </StackPanel>
        </Border>
        <Border>
            <StackPanel Orientation="Horizontal">
                <Button x:Name="btnStop"
                        Command="{Binding Path=StopCommand}">
                    <StackPanel>
                        <Image Source="{StaticResource Stop}"
                           Style="{StaticResource ImageEnabled}"></Image>
                    </StackPanel>
                </Button>
                <Button Command="{Binding Path=RestartCommand}">
                    <StackPanel>
                        <Image Source="{StaticResource Reset}"
                           Style="{StaticResource ImageEnabled}"></Image>
                    </StackPanel>
                </Button>
            </StackPanel>
        </Border>
    </WrapPanel>
</UserControl>
