<Window xmlns="https://github.com/avaloniaui"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:vm="using:Minint.ViewModels"
        xmlns:controls="using:Minint.Controls"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        mc:Ignorable="d" d:DesignWidth="1024" d:DesignHeight="700"
        x:Class="Minint.Views.MainWindow"
        x:DataType="vm:MainWindowViewModel"
        Icon="/Assets/avalonia-logo.ico"
        Title="{Binding Editor.Title}"
        Width="1024" Height="700"
        ToolTip.ShowDelay="400">

    <Window.KeyBindings>
        <KeyBinding Gesture="Ctrl+N" Command="{Binding NewFileCommand}"/>
        <KeyBinding Gesture="Ctrl+O" Command="{Binding OpenFileCommand}"/>
        <KeyBinding Gesture="Ctrl+S" Command="{Binding SaveFileCommand}"/>
        <KeyBinding Gesture="Ctrl+Shift+S" Command="{Binding SaveFileAsCommand}"/>
        <KeyBinding Gesture="Ctrl+C" Command="{Binding Editor.CopySelectionCommand}"/>
        <KeyBinding Gesture="Ctrl+V" Command="{Binding Editor.PasteClipboardCommand}"/>
        <KeyBinding Gesture="Ctrl+G" Command="{Binding Editor.ToggleGridCommand}"/>
    </Window.KeyBindings>

    <Design.DataContext>
        <vm:MainWindowViewModel/>
    </Design.DataContext>

    <DockPanel>
        <!-- Menu bar -->
        <Menu DockPanel.Dock="Top">
            <MenuItem Header="_File">
                <MenuItem Header="_New" Command="{Binding NewFileCommand}" HotKey="Ctrl+N"/>
                <MenuItem Header="_Open…" Command="{Binding OpenFileCommand}" HotKey="Ctrl+O"/>
                <Separator/>
                <MenuItem Header="_Save" Command="{Binding SaveFileCommand}" HotKey="Ctrl+S"/>
                <MenuItem Header="Save _As…" Command="{Binding SaveFileAsCommand}" HotKey="Ctrl+Shift+S"/>
                <Separator/>
                <MenuItem Header="Export Document as _BMP…" Command="{Binding ExportBmpCommand}"
                          ToolTip.Tip="Export the active document as a 32-bit BMP file"/>
                <MenuItem Header="Export All as _GIF…" Command="{Binding ExportGifCommand}"
                          ToolTip.Tip="Export all documents as an animated GIF"/>
            </MenuItem>
            <MenuItem Header="_Edit">
                <MenuItem Header="_Copy" Command="{Binding Editor.CopySelectionCommand}"
                          HotKey="Ctrl+C"
                          ToolTip.Tip="Copy the selected region to clipboard"/>
                <MenuItem Header="_Paste" Command="{Binding Editor.PasteClipboardCommand}"
                          HotKey="Ctrl+V"
                          ToolTip.Tip="Paste clipboard fragment onto the canvas"/>
            </MenuItem>
            <MenuItem Header="_Image">
                <MenuItem Header="Adjust _Contrast…" Command="{Binding ApplyContrastCommand}"
                          ToolTip.Tip="Adjust contrast of the active document's palette"/>
                <MenuItem Header="Convert to _Grayscale" Command="{Binding ApplyGrayscaleCommand}"
                          ToolTip.Tip="Convert active document to grayscale"/>
                <Separator/>
                <MenuItem Header="Generate _Pattern…" Command="{Binding GeneratePatternCommand}"
                          ToolTip.Tip="Generate a new document with a parametric pattern"/>
            </MenuItem>
            <MenuItem Header="_View">
                <MenuItem Header="Pixel _Grid" ToggleType="CheckBox"
                          IsChecked="{Binding Editor.ShowGrid, Mode=TwoWay}" HotKey="Ctrl+G"/>
            </MenuItem>
        </Menu>

        <!-- Toolbar -->
        <Border DockPanel.Dock="Top"
                BorderBrush="{DynamicResource SystemControlForegroundBaseMediumLowBrush}"
                BorderThickness="0,0,0,1" Padding="6,4">
            <StackPanel Orientation="Horizontal" Spacing="10">
                <RadioButton GroupName="Tool" Content="Brush"
                             ToolTip.Tip="Brush tool — draw with selected color"
                             IsChecked="{Binding Editor.ActiveTool, Converter={x:Static vm:ToolTypeConverters.IsBrush}}"
                             Command="{Binding Editor.SelectBrushCommand}"/>
                <RadioButton GroupName="Tool" Content="Eraser"
                             ToolTip.Tip="Eraser tool — erase to transparent"
                             IsChecked="{Binding Editor.ActiveTool, Converter={x:Static vm:ToolTypeConverters.IsEraser}}"
                             Command="{Binding Editor.SelectEraserCommand}"/>
                <RadioButton GroupName="Tool" Content="Fill"
                             ToolTip.Tip="Fill tool — flood fill with selected color"
                             IsChecked="{Binding Editor.ActiveTool, Converter={x:Static vm:ToolTypeConverters.IsFill}}"
                             Command="{Binding Editor.SelectFillCommand}"/>
                <RadioButton GroupName="Tool" Content="Select"
                             ToolTip.Tip="Select tool — drag to select a region, then Copy/Paste"
                             IsChecked="{Binding Editor.ActiveTool, Converter={x:Static vm:ToolTypeConverters.IsSelect}}"
                             Command="{Binding Editor.SelectSelectToolCommand}"/>

                <Separator/>

                <TextBlock Text="Size:" VerticalAlignment="Center"/>
                <Slider Value="{Binding Editor.BrushRadius}" Minimum="0" Maximum="64"
                        TickFrequency="1" IsSnapToTickEnabled="True" Width="120"
                        VerticalAlignment="Center"/>
                <TextBlock Text="{Binding Editor.BrushRadius}" VerticalAlignment="Center"
                           Width="20" TextAlignment="Center"/>

                <Separator/>

                <TextBlock Text="Color:" VerticalAlignment="Center"/>
                <ColorPicker x:Name="ToolColorPicker"
                             Color="{Binding Editor.PreviewColor, Mode=TwoWay}"
                             IsAlphaVisible="True"
                             VerticalAlignment="Center"/>
            </StackPanel>
        </Border>

        <!-- Status bar -->
        <Border DockPanel.Dock="Bottom" Background="{DynamicResource SystemControlBackgroundChromeMediumLowBrush}"
                Padding="8,2">
            <TextBlock Text="{Binding StatusText}" FontSize="12"/>
        </Border>

        <!-- Main content: left panel, canvas, right panel -->
        <Grid ColumnDefinitions="200,*,200">

            <!-- Left panel: documents -->
            <Border Grid.Column="0" BorderBrush="{DynamicResource SystemControlForegroundBaseMediumLowBrush}"
                    BorderThickness="0,0,1,0" Padding="4">
                <DockPanel>
                    <TextBlock DockPanel.Dock="Top" Text="Documents" FontWeight="SemiBold" Margin="0,0,0,4"/>

                    <!-- Animation controls -->
                    <StackPanel DockPanel.Dock="Bottom" Orientation="Horizontal" Spacing="4" Margin="0,4,0,0">
                        <Button Content="▶" ToolTip.Tip="Play animation"
                                Command="{Binding Editor.PlayAnimationCommand}"
                                IsEnabled="{Binding Editor.IsNotPlaying}" Padding="6,2"/>
                        <Button Content="■" ToolTip.Tip="Stop animation"
                                Command="{Binding Editor.StopAnimationCommand}"
                                IsEnabled="{Binding Editor.IsPlaying}" Padding="6,2"/>
                    </StackPanel>

                    <!-- Frame delay -->
                    <StackPanel DockPanel.Dock="Bottom" Orientation="Horizontal" Spacing="4" Margin="0,4,0,0">
                        <TextBlock Text="Delay:" VerticalAlignment="Center" FontSize="12"/>
                        <NumericUpDown Value="{Binding Editor.ActiveDocument.FrameDelayMs}"
                                       Minimum="10" Maximum="10000" Increment="10"
                                       FormatString="0"
                                       Width="110" FontSize="12"
                                       ToolTip.Tip="Frame delay in milliseconds"
                                       VerticalAlignment="Center"
                                       Foreground="{DynamicResource SystemControlForegroundBaseHighBrush}"/>
                        <TextBlock Text="ms" VerticalAlignment="Center" FontSize="12"/>
                    </StackPanel>

                    <!-- Document management buttons -->
                    <StackPanel DockPanel.Dock="Bottom" Orientation="Horizontal" Spacing="2" Margin="0,4,0,0">
                        <Button Content="+" ToolTip.Tip="Add a new document (frame)"
                                Command="{Binding Editor.AddDocumentCommand}" Padding="6,2"/>
                        <Button Content="−" ToolTip.Tip="Remove selected document"
                                Command="{Binding Editor.RemoveDocumentCommand}" Padding="6,2"/>
                        <Button Content="▲" ToolTip.Tip="Move document up in the list"
                                Command="{Binding Editor.MoveDocumentUpCommand}" Padding="6,2"/>
                        <Button Content="▼" ToolTip.Tip="Move document down in the list"
                                Command="{Binding Editor.MoveDocumentDownCommand}" Padding="6,2"/>
                    </StackPanel>
                    <ListBox ItemsSource="{Binding Editor.Documents}"
                             SelectedItem="{Binding Editor.ActiveDocument}"
                             SelectionMode="Single">
                        <ListBox.ItemTemplate>
                            <DataTemplate>
                                <controls:EditableTextBlock Text="{Binding Name}"/>
                            </DataTemplate>
                        </ListBox.ItemTemplate>
                    </ListBox>
                </DockPanel>
            </Border>

            <!-- Center: canvas with scrollbars -->
            <Grid Grid.Column="1" RowDefinitions="*,Auto" ColumnDefinitions="*,Auto">
                <Border Grid.Row="0" Grid.Column="0" Background="#FF1E1E1E" ClipToBounds="True">
                    <controls:PixelCanvas x:Name="Canvas"
                                           SourceBitmap="{Binding Editor.CanvasBitmap}"
                                           ShowGrid="{Binding Editor.ShowGrid}"/>
                </Border>
                <ScrollBar x:Name="HScroll" Grid.Row="1" Grid.Column="0"
                           Orientation="Horizontal"/>
                <ScrollBar x:Name="VScroll" Grid.Row="0" Grid.Column="1"
                           Orientation="Vertical"/>
            </Grid>

            <!-- Right panel: layers -->
            <Border Grid.Column="2" BorderBrush="{DynamicResource SystemControlForegroundBaseMediumLowBrush}"
                    BorderThickness="1,0,0,0" Padding="4">
                <DockPanel>
                    <TextBlock DockPanel.Dock="Top" Text="Layers" FontWeight="SemiBold" Margin="0,0,0,4"/>
                    <StackPanel DockPanel.Dock="Bottom" Orientation="Horizontal" Spacing="2" Margin="0,4,0,0">
                        <Button Content="+" ToolTip.Tip="Add a new empty layer"
                                Command="{Binding Editor.AddLayerCommand}" Padding="6,2"/>
                        <Button Content="−" ToolTip.Tip="Remove selected layer"
                                Command="{Binding Editor.RemoveLayerCommand}" Padding="6,2"/>
                        <Button Content="▲" ToolTip.Tip="Move layer up (draw later, appears on top)"
                                Command="{Binding Editor.MoveLayerUpCommand}" Padding="6,2"/>
                        <Button Content="▼" ToolTip.Tip="Move layer down (draw earlier, appears below)"
                                Command="{Binding Editor.MoveLayerDownCommand}" Padding="6,2"/>
                        <Button Content="⧉" ToolTip.Tip="Duplicate selected layer with all pixels"
                                Command="{Binding Editor.DuplicateLayerCommand}" Padding="6,2"/>
                    </StackPanel>
                    <ListBox ItemsSource="{Binding Editor.Layers}"
                             SelectedItem="{Binding Editor.ActiveLayer}"
                             SelectionMode="Single">
                        <ListBox.ItemTemplate>
                            <DataTemplate>
                                <StackPanel Orientation="Horizontal" Spacing="4">
                                    <CheckBox IsChecked="{Binding IsVisible}" MinWidth="0"
                                              VerticalAlignment="Center"
                                              ToolTip.Tip="Toggle layer visibility"/>
                                    <controls:EditableTextBlock Text="{Binding Name}"
                                             VerticalAlignment="Center" MinWidth="60"/>
                                </StackPanel>
                            </DataTemplate>
                        </ListBox.ItemTemplate>
                    </ListBox>
                </DockPanel>
            </Border>

        </Grid>
    </DockPanel>
</Window>
