﻿<Window x:Class="DriveDotNetGuiClient.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:i="clr-namespace:System.Windows.Interactivity;assembly=System.Windows.Interactivity"
        xmlns:ns="clr-namespace:DriveDotNetGuiClient"
        xmlns:localCnv="clr-namespace:DriveDotNetGuiClient.Converters"
        xmlns:localCmd="clr-namespace:DriveDotNetGuiClient.Commands"
        xmlns:controls="clr-namespace:DriveDotNetGuiClient.Controls"
        xmlns:local="clr-namespace:DriveDotNetGuiClient"
        Cursor="{Binding CursorState}"
        Title="Drive.Net Library" Height="810" Width="590" WindowStartupLocation="CenterScreen">

    <Window.Resources>
        <localCnv:PercentToIsIndeterminateConverter x:Key="PercentToIsIndeterminateConverter" />
        <localCmd:OpenFileBrowserCommand x:Key="OpenFileBrowserCommand" />

        <Style  TargetType="GroupBox">
            <Setter Property="Background" Value="#fff9f9f9" />
            <Setter Property="FontStyle" Value="Normal"/>
            <Setter Property="FontSize" Value="12" />
            <Setter Property="Margin" Value="4,4,4,0" />
        </Style>


    </Window.Resources>
    
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="180"/>
            <RowDefinition Height="55"/>
            <RowDefinition Height="65"/>
            <RowDefinition Height="230"/>
            <RowDefinition Height="*"/>
            <RowDefinition Height="35"/>
        </Grid.RowDefinitions>

        <GroupBox Header="Communication" Grid.Row="0" Name="Communicaion">
            <StackPanel Orientation="Vertical">
                <TabControl Name="Tabs" Height="120" BorderBrush="#69000000" SelectedItem="{Binding SelectedTab, Mode=OneWayToSource}" IsEnabled="{Binding ElementName=DisconnectButton,Path=IsEnabled}">
                <TabItem Header="UDP">
                <Grid>
                    <Grid.RowDefinitions>
                        <RowDefinition/>
                        <RowDefinition/>
                        <RowDefinition/>
                    </Grid.RowDefinitions>
                    <Grid.ColumnDefinitions>
                        <ColumnDefinition Width="50"/>
                        <ColumnDefinition Width="174*" />
                    </Grid.ColumnDefinitions>
            
                    <TextBlock Grid.Row="0" Grid.Column="0" Text="Drive IP:" Height="25" />
                    <TextBlock Grid.Row="1" Grid.Column="0" Text="Host IP:" Height="25" />
                    <TextBlock Grid.Row="2" Grid.Column="0" Text="Port:" Height="25" />
                    <ComboBox Grid.Column="1" Text="{Binding DriveIP, Mode=TwoWay}" Height="25" Grid.ColumnSpan="2" IsEditable="True" Margin="0,3" ItemsSource="{Binding LocatorResults}">
                        <i:Interaction.Triggers>
                            <i:EventTrigger EventName="DropDownOpened">
                                <i:InvokeCommandAction Command="{Binding Path=LocateUdpDevicesCommand, Mode=OneWay}"/>
                            </i:EventTrigger>
                            <i:EventTrigger EventName="DropDownClosed">
                                        <i:InvokeCommandAction Command="{Binding Path=SelectUdpDeviceCommand, Mode=OneWay}" CommandParameter="{Binding SelectedItem,
                                             RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type ComboBox }}}"/>
                            </i:EventTrigger>
                        </i:Interaction.Triggers>
                    </ComboBox>
                    <TextBox Grid.Row="1" Grid.Column="1" Text="{Binding HostIP, Mode=TwoWay}" Height="25" Grid.ColumnSpan="2" Margin="0,3" />
                    <TextBox Grid.Row="2" Grid.Column="1" Text="{Binding IpPortUDP, Mode=TwoWay}" Height="25" Grid.ColumnSpan="2" Margin="0,3" />
                </Grid>
                </TabItem>
                    <TabItem Header="USB" >
                <Grid>
                    <Grid.ColumnDefinitions>
                        <ColumnDefinition Width="70"/>
                        <ColumnDefinition/>
                    </Grid.ColumnDefinitions>
                    <TextBlock Grid.Row="0" Grid.Column="0" Text="COM Port:" Height="25" />
                            <ComboBox Grid.Row="0" Grid.Column="1" IsEditable="True" Text="{Binding UsbComPort, Mode=TwoWay}" Height="25" ItemsSource="{Binding LocatorResults}">
                    <i:Interaction.Triggers>
                        <i:EventTrigger EventName="DropDownOpened">
                                        <i:InvokeCommandAction Command="{Binding Path=LocateUsbDevicesCommand, Mode=OneWay}"/>
                        </i:EventTrigger>
                    </i:Interaction.Triggers>
                    </ComboBox>
            </Grid>
            </TabItem>
                <TabItem Header="RS232" >
                <Grid>
                    <Grid.RowDefinitions>
                        <RowDefinition/>
                        <RowDefinition/>
                    </Grid.RowDefinitions>
                    <Grid.ColumnDefinitions>
                        <ColumnDefinition Width="Auto"/>
                        <ColumnDefinition/>
                    </Grid.ColumnDefinitions>
                    <TextBlock Grid.Row="0" Grid.Column="0" Text="Baud Rate:" Height="25" />
                    <TextBlock Grid.Row="1" Grid.Column="0" Text="COM Port:" Height="25" />
                    <ComboBox Grid.Row="0" Grid.Column="1" ItemsSource="{Binding BuadRates}" Text="{Binding BuadRate}" Height="25" Margin="10,0,0,0" />
                            <ComboBox Grid.Row="1" Grid.Column="1"  IsEditable="True" Text="{Binding ComPort}" Height="25" Margin="10,0,0,0" ItemsSource="{Binding LocatorResults}">
                        <i:Interaction.Triggers>
                            <i:EventTrigger EventName="DropDownOpened">
                                        <i:InvokeCommandAction Command="{Binding Path=LocateRS232DevicesCommand, Mode=OneWay}"/>
                            </i:EventTrigger>
                        </i:Interaction.Triggers>
                    </ComboBox>
            </Grid>
            </TabItem>
                <TabItem Header="CAN" >
                        <StackPanel Orientation="Vertical">
                            <StackPanel Orientation="Horizontal">
                                <TextBlock Text="Manufacturer:" Height="25" Width="100"/>
                                <ComboBox ItemsSource="{Binding Manufacturers}" SelectedItem="{Binding SelectedManufacturer}" Height="25" Width="150"/>
                            </StackPanel>
                            <ContentPresenter Content="{Binding CANObject}"/>
                        </StackPanel>
      
            </TabItem>
            </TabControl>
       
            <StackPanel Orientation="Horizontal" Name="ConnectionButtons" HorizontalAlignment="Right">
                    <Button Height="25" Margin="0,5,10,0" Command="{Binding ConnectCommand}" Width="100" Name="DisconnectButton">Connect</Button>
                <Button Height="25" Margin="0,5,10,0" Command="{Binding DisconnectCommand}" Width="100" Name="ConnectButton">Disconnect</Button>
            </StackPanel>
                
            </StackPanel>
        </GroupBox>

        <GroupBox Header="Personality File" Grid.Row="1" Name="PersonalityModel">
            <DockPanel LastChildFill="True">
                <TextBlock Text="Path:" Height="25" Margin="10,0,10,0" DockPanel.Dock="Left"/>
                <Button Content="..." Height="25" Width="25" Command="{StaticResource OpenFileBrowserCommand}" CommandParameter="{Binding}" DockPanel.Dock="Right" IsEnabled="{Binding ElementName=DisconnectButton,Path=IsEnabled}"/>
                <TextBox Text="{Binding PersonalityPath}" Height="25" IsEnabled="{Binding ElementName=DisconnectButton,Path=IsEnabled}"/>
            </DockPanel>
        </GroupBox>

        <GroupBox Header="Terminal" Grid.Row="2" Name="Terminal">
            <DockPanel LastChildFill="True">
                <TextBlock Text="Command: " Height="25" Margin="10,0,10,0" DockPanel.Dock="Left"/>
                <Button Command="{Binding SendCommandCommand}" Height="25" DockPanel.Dock="Right" Width="50" IsEnabled="{Binding ElementName=ConnectButton,Path=IsEnabled}">Send</Button>
                <TextBox Text="{Binding Command, UpdateSourceTrigger=PropertyChanged}"  Height="25" IsEnabled="{Binding ElementName=ConnectButton,Path=IsEnabled}">
                    <TextBox.InputBindings>
                        <KeyBinding Key="Enter" Command="{Binding SendCommandCommand}"/>
                    </TextBox.InputBindings>
                </TextBox>
            </DockPanel>
        </GroupBox>

        <GroupBox Header="Library features" Grid.Row="3" Name="LibraryFeatures">
            <TabControl  BorderBrush="#69000000">
                <TabItem Header="Uploads and Downloads">
                    <StackPanel Orientation="Vertical">
                        <StackPanel Orientation="Horizontal" >
                            <ComboBox Margin="0,0,10,0" Width="250" ItemsSource="{Binding UploadsAndDownloadsActions}" SelectedItem="{Binding SelectedAction}" />
                            <Button Margin="0,0,10,0" Height="25" Width="100" Command="{Binding StartUploadOrDownloadCommand}">Start</Button>
                            <Button  Height="25" Width="100" Command="{Binding CancelUploadOrDownloadCommand}">Stop</Button>
                        </StackPanel>
                        <ContentPresenter Content="{Binding SettingsObject}" Height="130" Margin="0,10,0,0"/>
                    </StackPanel>
                </TabItem>
                <TabItem Header="Compile User Program">
                    <Grid>
                        <Grid.ColumnDefinitions>
                            <ColumnDefinition Width="Auto" />
                            <ColumnDefinition Width="10" />
                            <ColumnDefinition Width="*" />
                        </Grid.ColumnDefinitions>
                        <Grid.RowDefinitions>
                            <RowDefinition Height="30" />
                            <RowDefinition Height="30" />
                            <RowDefinition Height="30" />
                            <RowDefinition Height="30" />
                            <RowDefinition Height="*" />
                        </Grid.RowDefinitions>
                        <TextBlock Text="Program path:" Grid.Row="0" VerticalAlignment="Center"/>
                        <TextBlock Text="Compilation Mode:" Grid.Row="1" VerticalAlignment="Center"/>
                        <TextBlock Text="Compiler folder:" Grid.Row="2" VerticalAlignment="Center"/>
                        <TextBlock Text="Output folder:" Grid.Row="3" VerticalAlignment="Center"/>
                        <controls:PathAndBrowse Grid.Column="2" Grid.Row="0" SelectedPath="{Binding Path=ProgramPath}" BrowseType="SelectFile" FileExtension="Ehl files (*.ehl)|*.ehl|All files (*.*)|*.*" />
                        <ComboBox Grid.Row="1" Grid.Column="2" VerticalAlignment="Center" HorizontalAlignment="Left" SelectedIndex="{Binding CompilationMode}">
                            <ComboBoxItem Content="Debug"/>
                            <ComboBoxItem Content="Release"/>
                        </ComboBox>
                        <controls:PathAndBrowse Grid.Column="2" Grid.Row="2" SelectedPath="{Binding CompilerPath}" BrowseType="SelectFolder"/>
                        <controls:PathAndBrowse Grid.Column="2" Grid.Row="3" SelectedPath="{Binding OutputPath}" BrowseType="SelectFolder"/>

                        <Button Grid.Row="4" Grid.Column="2" Content="Compile" VerticalAlignment="Center" HorizontalAlignment="Right" Command="{Binding cmdCompile}"/>
                    </Grid>
                </TabItem>
            </TabControl>
        </GroupBox>


        <GroupBox Header="Logs" Grid.Row="4" Name="Logs">
            <DockPanel LastChildFill="True">
                <Button Command="{Binding ClearLogsCommand}" Height="22" DockPanel.Dock="Bottom">Clear Log</Button>
                <ListBox Name="Log" ItemsSource="{Binding Logs}" DockPanel.Dock="Top"/>
            </DockPanel>
        </GroupBox>

        <ProgressBar Grid.Row="5" Value="{Binding Percent}" IsIndeterminate="{Binding Percent,Converter={StaticResource PercentToIsIndeterminateConverter}}"/>
    
    </Grid>
</Window>
