<Window
    x:Class="FileTime.Avalonia.Views.MainWindow"
    xmlns="https://github.com/avaloniaui"
    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:vm="using:FileTime.Avalonia.ViewModels"
	xmlns:local="using:FileTime.Avalonia.Views"
	xmlns:models="using:FileTime.Avalonia.Models"
    Title="FileTime"
    d:DesignHeight="450"
    d:DesignWidth="800"
    Icon="/Assets/filetime.ico"
	InputElement.KeyDown="OnKeyDown"
	TransparencyLevelHint="Blur"
	Background="Transparent"
	ExtendClientAreaToDecorationsHint="True"
	Opened="OnWindowOpened"
	Closed="OnWindowClosed"
	x:DataType="vm:MainPageViewModel"
	x:CompileBindings="True"
    mc:Ignorable="d">

	<Grid Background="{DynamicResource AppBackgroundBrush}">
		<Grid>
			<Grid ColumnDefinitions="250,*" RowDefinitions="Auto,*" IsVisible="{Binding Loading, Converter={StaticResource BoolInverter}}">

				<Grid PointerPressed="HeaderPointerPressed">
					<Rectangle Fill="#01000000"/>
					<TextBlock Margin="15,10" Text="FileTime"/>
				</Grid>

				<Grid Grid.Column="1" PointerPressed="HeaderPointerPressed">
					<Rectangle Fill="#01000000"/>

					<StackPanel Margin="20,10" Orientation="Horizontal">
						<local:PathPresenter DataContext="{Binding AppState.SelectedTab.CurrentLocation.Container.FullName}"/>
						<TextBlock
							Text="{Binding AppState.SelectedTab.SelectedItem.Item.Name}" Foreground="{StaticResource AccentForegroundBrush}" />
					</StackPanel>
				</Grid>

				<Grid Grid.Row="1" RowDefinitions="Auto,Auto">

					<Border CornerRadius="10" Background="{DynamicResource ContainerBackgroundBrush}" Padding="10" Margin="10">
						<Grid RowDefinitions="Auto,Auto">

							<TextBlock
								Margin="0,0,0,10"
								Text="Drives" />

							<ItemsRepeater
								Grid.Row="1"
								Items="{Binding RootDriveInfos}">
								<ItemsRepeater.ItemTemplate>
									<DataTemplate>
										<Grid Classes="RootDriveInfo" PointerPressed="OnRootDrivePointerPressed" Cursor="Hand">
											<Grid Margin="0,5" ColumnDefinitions="Auto,*,Auto" RowDefinitions="Auto,Auto">
												<Image
													Grid.RowSpan="2"
													Width="20"
													Height="20"
													HorizontalAlignment="Left"
													VerticalAlignment="Center"
													Source="{SvgImage /Assets/material/folder.svg}" />

												<StackPanel
													Grid.Column="1"
													VerticalAlignment="Center"
													HorizontalAlignment="Stretch"
													Orientation="Horizontal">

													<TextBlock
														Margin="5,0,0,0"
														VerticalAlignment="Center"
														Text="{Binding FullName}" />

													<TextBlock
														Margin="5,0,0,0"
														VerticalAlignment="Center"
														Classes="ExtraSmallText"
														Text="{Binding Label}" IsVisible="{Binding Label,Converter={StaticResource IsNotEmptyConverter}}" />
												</StackPanel>

												<StackPanel
													HorizontalAlignment="Right"
													Grid.Column="2"
													Orientation="Horizontal"
													VerticalAlignment="Center">

													<TextBlock Classes="SmallText" VerticalAlignment="Center" Text="{Binding Free, Converter={StaticResource FormatSizeConverter}, ConverterParameter=0}">
													</TextBlock>

													<TextBlock Classes="SmallText" VerticalAlignment="Center" Text=" / ">
													</TextBlock>

													<TextBlock Classes="SmallText" VerticalAlignment="Center" Text="{Binding Size, Converter={StaticResource FormatSizeConverter}, ConverterParameter=0}">
													</TextBlock>
												</StackPanel>

												<ProgressBar
													Margin="5,0,0,0"
													Grid.Column="1"
													Grid.ColumnSpan="2"
													Grid.Row="1"
													MinWidth="100"
													HorizontalAlignment="Stretch"
													Maximum="100"
													Value="{Binding UsedPercentage}" />
											</Grid>
										</Grid>
									</DataTemplate>
								</ItemsRepeater.ItemTemplate>
							</ItemsRepeater>
						</Grid>
					</Border>

					<Border Grid.Row="1" CornerRadius="10" Background="{DynamicResource ContainerBackgroundBrush}" Padding="0,10" Margin="10">
						<Grid RowDefinitions="Auto,Auto">

							<TextBlock
								Margin="10,0,10,10"
								Text="Places" />

							<ItemsRepeater
								Grid.Row="1"
								Items="{Binding Places}">
								<ItemsRepeater.ItemTemplate>
									<DataTemplate>
										<Grid Classes="PlacesItem" PointerPressed="OnPlacePointerPressed" Cursor="Hand">
											<StackPanel Orientation="Horizontal" Margin="10,5" HorizontalAlignment="Stretch">
												<Image
													Width="20"
													Height="20"
													VerticalAlignment="Center"
													Source="{Binding Container,Converter={StaticResource ItemToImageConverter}}" />

												<TextBlock
													Margin="5,0,0,0"
													VerticalAlignment="Center"
													Text="{Binding Name}" />
											</StackPanel>
										</Grid>
									</DataTemplate>
								</ItemsRepeater.ItemTemplate>
							</ItemsRepeater>
						</Grid>
					</Border>
				</Grid>

				<Grid Grid.Column="1" Grid.Row="1" RowDefinitions="Auto,40,*,Auto">
					<Grid>
						<ItemsControl Items="{Binding AppState.TimelineCommands}">
							<ItemsControl.ItemsPanel>
								<ItemsPanelTemplate>
									<StackPanel Orientation="Horizontal" />
								</ItemsPanelTemplate>
							</ItemsControl.ItemsPanel>
							<ItemsControl.ItemTemplate>
								<DataTemplate>
									<Border MaxHeight="200" Background="{DynamicResource ContainerBackgroundColor}" CornerRadius="10" Padding="5" Margin="0,0,10,0">
										<ScrollViewer>
											<ItemsControl Items="{Binding ParallelCommands}">
												<ItemsControl.ItemTemplate>
													<DataTemplate>
														<Border BorderThickness="1" Classes="TimelineCommand" Classes.SelectedTimelineCommand="{Binding IsSelected}">
															<StackPanel>
																<TextBlock Text="{Binding Name}"/>
																<ProgressBar Margin="0,5,0,0" Maximum="100" Value="{Binding Progress}"/>
															</StackPanel>
														</Border>
													</DataTemplate>
												</ItemsControl.ItemTemplate>
											</ItemsControl>
										</ScrollViewer>
									</Border>
								</DataTemplate>
							</ItemsControl.ItemTemplate>
						</ItemsControl>
					</Grid>

					<ItemsControl
							Grid.Row="1"
							Items="{Binding AppState.Tabs}">
						<ItemsControl.ItemsPanel>
							<ItemsPanelTemplate>
								<StackPanel Orientation="Horizontal" />
							</ItemsPanelTemplate>
						</ItemsControl.ItemsPanel>
						<ItemsControl.ItemTemplate>
							<DataTemplate>
								<Grid RowDefinitions="Auto,1">
									<StackPanel Orientation="Horizontal" Margin="20,0,20,0">

										<TextBlock
											VerticalAlignment="Center" Text="{Binding TabNumber,StringFormat=({0})}" />

										<TextBlock
											VerticalAlignment="Center" Margin="5,0,0,0" Text="{Binding CurrentLocation.Container.Name}" />

										<!--local:PathPresenter Margin="5,0,0,0" DataContext="{Binding CurrentLocation.Container.FullName}"/-->
									</StackPanel>

									<Rectangle Fill="{DynamicResource ForegroundBrush}" Grid.Row="1" IsVisible="{Binding IsSelected}"/>
								</Grid>
							</DataTemplate>
						</ItemsControl.ItemTemplate>
					</ItemsControl>

					<Grid
						Grid.Row="2"
						Margin="20,0,0,0">
						<Grid>
							<Grid.ColumnDefinitions>
								<ColumnDefinition Width="15*" />
								<ColumnDefinition Width="10" />
								<ColumnDefinition Width="40*" />
								<ColumnDefinition Width="10" />
								<ColumnDefinition Width="45*" />
							</Grid.ColumnDefinitions>

							<Grid>
								<ListBox
									x:CompileBindings="False"
									Classes="ContentListView"
									IsEnabled="False"
									Items="{Binding AppState.SelectedTab.Parent.Items^}">
									<ListBox.ItemTemplate>
										<DataTemplate>
											<local:ItemView ShowAttributes="False"/>
										</DataTemplate>
									</ListBox.ItemTemplate>
								</ListBox>
							</Grid>

							<Rectangle
								Grid.Column="1"
								Width="1"
								Margin="0,10,0,10"
								HorizontalAlignment="Center"
								VerticalAlignment="Stretch"
								Fill="{DynamicResource ContentSeparatorBrush}" />

							<Grid Grid.Column="2">
								<ListBox
									x:Name="CurrentItems"
									x:CompileBindings="False"
									IsTabStop="True"
									Items="{Binding AppState.SelectedTab.CurrentLocation.Items^}"
									ScrollViewer.HorizontalScrollBarVisibility="Disabled"
									ScrollViewer.VerticalScrollBarVisibility="Hidden"
									SelectedItem="{Binding AppState.SelectedTab.SelectedItem, Mode=TwoWay}"
									Classes="ContentListView">
									<ListBox.ItemTemplate>
										<DataTemplate>
											<local:ItemView/>
										</DataTemplate>
									</ListBox.ItemTemplate>
								</ListBox>

								<TextBlock
									x:CompileBindings="False"
									x:Name="CurrentEmpty"
									Margin="10"
									HorizontalAlignment="Center"
									FontWeight="Bold"
									Foreground="{DynamicResource ErrorBrush}"
									IsVisible="{Binding AppState.SelectedTab.CurrentLocation.Items^.Count, Converter={StaticResource EqualityConverter}, ConverterParameter=0}">
									Empty
								</TextBlock>
							</Grid>

							<Rectangle
								Grid.Column="3"
								Width="1"
								Margin="0,10,0,10"
								HorizontalAlignment="Center"
								VerticalAlignment="Stretch"
								Fill="{DynamicResource ContentSeparatorBrush}" />

							<Grid Grid.Column="4">
								<Grid IsVisible="{Binding AppState.SelectedTab.ChildContainer,Converter={StaticResource IsNotNullConverter}}">
									<ListBox
										Classes="ContentListView"
										IsEnabled="False"
										x:Name="ChildItems"
										x:CompileBindings="False"
										Items="{Binding AppState.SelectedTab.ChildContainer.Items^}"
										IsVisible="{Binding AppState.SelectedTab.ChildContainer.Items^.Count, Converter={StaticResource NotEqualsConverter}, ConverterParameter=0}">
										<ListBox.ItemTemplate>
											<DataTemplate>
												<local:ItemView/>
											</DataTemplate>
										</ListBox.ItemTemplate>
									</ListBox>

									<Grid
										x:CompileBindings="False" 
										IsVisible="{Binding AppState.SelectedTab.ChildContainer.Items^.Count, Converter={StaticResource EqualityConverter}, ConverterParameter=0}">

										<TextBlock
											x:Name="ChildEmpty"
											Margin="10"
											HorizontalAlignment="Center"
											FontWeight="Bold"
											Foreground="{DynamicResource ErrorBrush}"
											IsVisible="{Binding AppState.SelectedTab.ChildContainer.Exceptions.Count, Converter={StaticResource EqualityConverter}, ConverterParameter=0}">
											Empty
										</TextBlock>

										<Grid
											RowDefinitions="Auto, Auto"
											IsVisible="{Binding AppState.SelectedTab.ChildContainer.Exceptions.Count, Converter={StaticResource NotEqualsConverter}, ConverterParameter=0}">

											<TextBlock
												Margin="0,0,0,10"
												HorizontalAlignment="Center"
												TextWrapping="Wrap"
												Text="There were some errors while opening container."
												Foreground="{DynamicResource ErrorBrush}" />

											<ItemsRepeater Grid.Row="1" Items="{Binding AppState.SelectedTab.ChildContainer.Exceptions}">
												<ItemsRepeater.ItemTemplate>
													<DataTemplate>
														<TextBlock Text="{Binding, Converter={StaticResource ExceptionToStringConverter}}"/>
													</DataTemplate>
												</ItemsRepeater.ItemTemplate>
											</ItemsRepeater>
										</Grid>
									</Grid>
								</Grid>

								<Grid IsVisible="{Binding AppState.SelectedTab.ElementPreview, Converter={StaticResource IsNotNullConverter}}">
									<TextBlock HorizontalAlignment="Center" Text="Don't know how to preview this file" IsVisible="{Binding AppState.SelectedTab.ElementPreview.Mode, Converter={StaticResource EqualityConverter}, ConverterParameter={x:Static models:ElementPreviewMode.Unknown}}"/>
									<TextBlock HorizontalAlignment="Center" Text="Empty" IsVisible="{Binding AppState.SelectedTab.ElementPreview.Mode, Converter={StaticResource EqualityConverter}, ConverterParameter={x:Static models:ElementPreviewMode.Empty}}"/>
									<ScrollViewer IsVisible="{Binding AppState.SelectedTab.ElementPreview.Mode, Converter={StaticResource EqualityConverter}, ConverterParameter={x:Static models:ElementPreviewMode.Text}}">
										<TextBox
											IsReadOnly="True"
											Text="{Binding AppState.SelectedTab.ElementPreview.TextContent}" />
									</ScrollViewer>
								</Grid>
							</Grid>
						</Grid>

						<ItemsRepeater Items="{Binding AppState.PopupTexts}" Margin="0,0,0,20" HorizontalAlignment="Center" VerticalAlignment="Bottom" IsVisible="{Binding AppState.PopupTexts.Count,Converter={StaticResource NotEqualsConverter}, ConverterParameter=0}">
							<ItemsRepeater.Styles>
								<Style Selector="TextBlock">
									<Style.Animations>
										<Animation Duration="0:0:1">
											<KeyFrame Cue="0%">
												<Setter Property="Opacity" Value="0.0"/>
											</KeyFrame>
											<KeyFrame Cue="100%">
												<Setter Property="Opacity" Value="1.0"/>
											</KeyFrame>
										</Animation>
									</Style.Animations>
								</Style>
							</ItemsRepeater.Styles>
							<ItemsRepeater.ItemTemplate>
								<DataTemplate>
									<TextBlock Text="{Binding}" HorizontalAlignment="Center"/>
								</DataTemplate>
							</ItemsRepeater.ItemTemplate>
						</ItemsRepeater>
					</Grid>

					<Grid Grid.Row="3">
						<Grid IsVisible="{Binding AppState.ViewMode, Converter={StaticResource EqualityConverter}, ConverterParameter=RapidTravel}">
							<Grid.RowDefinitions>
								<RowDefinition Height="1" />
								<RowDefinition Height="Auto" />
							</Grid.RowDefinitions>

							<Rectangle
								Height="1"
								Margin="10,0"
								HorizontalAlignment="Stretch"
								VerticalAlignment="Center"
								Fill="{DynamicResource ContentSeparatorBrush}" />

							<StackPanel
								Grid.Row="1"
								Margin="30,10,10,10"
								Orientation="Horizontal">

								<TextBlock
									Margin="0,0,30,0"
									Text="Rapid travel mode" />

								<TextBlock Text="Filter " />

								<TextBlock Text="{Binding AppState.RapidTravelText}" />
							</StackPanel>
						</Grid>

						<Grid IsVisible="{Binding AppState.NoCommandFound}">
							<Grid.RowDefinitions>
								<RowDefinition Height="1" />
								<RowDefinition Height="Auto" />
							</Grid.RowDefinitions>

							<Rectangle
								Height="1"
								Margin="10,0"
								HorizontalAlignment="Stretch"
								VerticalAlignment="Center"
								Fill="{DynamicResource ContentSeparatorBrush}" />

							<TextBlock
								Grid.Row="1"
								Margin="10"
								HorizontalAlignment="Center"
								Foreground="{DynamicResource ErrorBrush}"
								Text="No command found" />
						</Grid>

						<Grid IsVisible="{Binding AppState.PossibleCommands.Count, Converter={StaticResource NotEqualsConverter}, ConverterParameter=0}">
							<Grid.RowDefinitions>
								<RowDefinition Height="1" />
								<RowDefinition Height="Auto" />
							</Grid.RowDefinitions>

							<Rectangle
								Height="1"
								Margin="10,0"
								HorizontalAlignment="Stretch"
								VerticalAlignment="Center"
								Fill="{DynamicResource ContentSeparatorBrush}" />

							<ItemsRepeater
								Grid.Row="1"
								Items="{Binding AppState.PossibleCommands}">
								<ItemsRepeater.ItemTemplate>
									<DataTemplate>
										<Grid>
											<Grid.ColumnDefinitions>
												<ColumnDefinition Width="200" />
												<ColumnDefinition Width="*" />
											</Grid.ColumnDefinitions>

											<TextBlock Text="{Binding KeysDisplayText}" />
											<TextBlock Grid.Column="1" Text="{Binding Command, Converter={StaticResource CommandToCommandNameConverter}}" />
										</Grid>
									</DataTemplate>
								</ItemsRepeater.ItemTemplate>
							</ItemsRepeater>
						</Grid>
					</Grid>
				</Grid>
			</Grid>

			<Border
				Background="{DynamicResource BarelyTransparentBackgroundColor}"
				HorizontalAlignment="Stretch"
				VerticalAlignment="Stretch"
				IsVisible="{Binding AppState.Inputs, Converter={StaticResource IsNotNullConverter}}">
				<Border
					Background="{DynamicResource ContainerBackgroundBrush}"
					Padding="20"
					HorizontalAlignment="Center"
					VerticalAlignment="Center">
					<Grid RowDefinitions="Auto,Auto">

						<ItemsControl
							x:Name="InputList"
							Items="{Binding AppState.Inputs}">
							<ItemsControl.ItemTemplate>
								<DataTemplate>
									<Grid MinWidth="500" ColumnDefinitions="250,*" Margin="10,5">
										<TextBlock
											HorizontalAlignment="Left"
											VerticalAlignment="Center"
											Text="{Binding InputElement.Text}" />
										<TextBox
											PasswordChar="{Binding PasswordChar}"
											AttachedToVisualTree="InputText_AttachedToVisualTree"
											IsTabStop="True"
											Grid.Column="1"
											GotFocus="InputText_GotFocus"
											LostFocus="InputText_LostFocus"
											KeyDown="InputText_KeyDown"
											Text="{Binding Value, Mode=TwoWay}" />
									</Grid>
								</DataTemplate>
							</ItemsControl.ItemTemplate>
						</ItemsControl>
						<StackPanel
							Grid.Row="1"
							Orientation="Horizontal"
							Margin="0,10,0,0">
							<Button
								HorizontalContentAlignment="Center"
								Width="80"
								Command="{Binding ProcessInputsCommand}"
								Content="Ok" />
							<Button
								HorizontalContentAlignment="Center"
								Width="80"
								Margin="10,0,0,0"
								Command="{Binding CancelInputsCommand}"
								Content="Cancel" />
						</StackPanel>
					</Grid>
				</Border>
			</Border>

			<Border
				Background="{DynamicResource BarelyTransparentBackgroundColor}"
				HorizontalAlignment="Stretch"
				VerticalAlignment="Stretch"
				IsVisible="{Binding AppState.MessageBoxText, Converter={StaticResource IsNotNullConverter}}">
				<Border
					Background="{DynamicResource ContainerBackgroundBrush}"
					HorizontalAlignment="Center"
					VerticalAlignment="Center"
					Padding="20">
					<Grid RowDefinitions="Auto,Auto">

						<TextBlock Text="{Binding AppState.MessageBoxText}"/>
						<StackPanel
							Grid.Row="1"
							Orientation="Horizontal"
							Margin="0,10,0,0">
							<Button
								HorizontalContentAlignment="Center"
								Width="80"
								Command="{Binding ProcessMessageBoxCommand}"
								Content="Yes" />
							<Button
								HorizontalContentAlignment="Center"
								Width="80"
								Margin="10,0,0,0"
								Command="{Binding CancelMessageBoxCommand}"
								Content="No" />
						</StackPanel>
					</Grid>
				</Border>
			</Border>

			<Border
				Background="{DynamicResource TransparentContainerBackgroundBrush}"
				Margin="20"
				HorizontalAlignment="Center"
				IsVisible="{Binding AppState.IsAllShortcutVisible}">
				<Grid RowDefinitions="Auto, *" Margin="30,10">
					<TextBlock Text="Shortcuts" Margin="0,0,0,20"/>
					<ScrollViewer
							Grid.Row="1"
							HorizontalScrollBarVisibility="Disabled">
						<ItemsControl
							Items="{Binding KeyboardConfigurationService.AllShortcut}">
							<ItemsControl.ItemTemplate>
								<DataTemplate>
									<Grid Margin="0,5,10,5">
										<Grid.ColumnDefinitions>
											<ColumnDefinition Width="200" />
											<ColumnDefinition Width="*" />
										</Grid.ColumnDefinitions>

										<TextBlock Text="{Binding KeysDisplayText}" />
										<TextBlock Grid.Column="1" Text="{Binding Command, Converter={StaticResource CommandToCommandNameConverter}}" />
									</Grid>
								</DataTemplate>
							</ItemsControl.ItemTemplate>
						</ItemsControl>
					</ScrollViewer>
				</Grid>
			</Border>
		</Grid>

		<Grid IsVisible="{Binding Loading}">
			<StackPanel HorizontalAlignment="Center" VerticalAlignment="Center">
				<Image Source="/Assets/filetime.ico" Width="128" Height="128"/>
				<TextBlock Text="Loading..." HorizontalAlignment="Center" Margin="50"/>
			</StackPanel>
		</Grid>
	</Grid>
</Window>
