<UserControl x:Class="TradeBlotter.Views.BlotterView"
      xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
      xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
      xmlns:scm="clr-namespace:System.ComponentModel;assembly=WindowsBase"
      xmlns:telerik="http://schemas.telerik.com/2008/xaml/presentation"
      xmlns:dc="clr-namespace:TradeBlotter.UIUtils"
      xmlns:local="clr-namespace:TradeBlotter.UIUtils"
		  xmlns:vw="clr-namespace:TradeBlotter.Views"						 
      xmlns:wpfx="http://schemas.xceed.com/wpf/xaml/toolkit"
	>
  <UserControl.Resources>
    <CollectionViewSource
      x:Key="Trades" 
      Source="{Binding Path=AllTrades,Mode=TwoWay}"
      >
    </CollectionViewSource>

    <GroupStyle x:Key="TradeGroupStyle">
      <GroupStyle.HeaderTemplate>
        <DataTemplate>
          <TextBlock 
            x:Name="txt" 
            Background="{StaticResource Brush_HeaderBackground}"
            FontWeight="Bold"
            Foreground="white"
            Margin="1"
            Padding="4,2,0,2"
            Text="People" 
            />
          <DataTemplate.Triggers>
            <DataTrigger Binding="{Binding Path=Name}" Value="True">
              <Setter TargetName="txt" Property="Text" Value="Companies" />
            </DataTrigger>
          </DataTemplate.Triggers>
        </DataTemplate>
      </GroupStyle.HeaderTemplate>
    </GroupStyle>
    <Style x:Key="TradeItemStyle" TargetType="{x:Type telerik:RadGridView}">
    </Style>
  </UserControl.Resources>
  <wpfx:BusyIndicator Name="BusyBar" IsBusy="{Binding Path=BusyIndicator}" BusyContent="{Binding Path=BusyContent}">
  <Grid  Background="LightGray">  
  <DockPanel>
    <Grid DockPanel.Dock="Bottom" Margin="0,2,4,2">
    <Grid.ColumnDefinitions>
      <ColumnDefinition Width="Auto" />
      <ColumnDefinition Width="6" />
      <ColumnDefinition Width="Auto" />
    </Grid.ColumnDefinitions>
    <Grid.RowDefinitions>
      <RowDefinition Height="Auto" />
      <RowDefinition Height="3" />
      <RowDefinition Height="Auto" />
      <RowDefinition Height="3" />
      <RowDefinition Height="Auto" />
    </Grid.RowDefinitions>
       <StackPanel Grid.Row="0" Grid.Column="0" HorizontalAlignment="Left" Orientation="Horizontal" VerticalAlignment="Center">
        <TextBlock Margin="5,0,5,0" Text="Exposure:" /><ContentPresenter Content="{Binding Path=TotalExposure}" ContentStringFormat="c"/>
        <TextBlock Margin="5,0,5,0" Text="|" />
        <TextBlock Margin="5,0,5,0" Text="MV:" /><ContentPresenter Content="{Binding Path=TotalMarketValue}" ContentStringFormat="c"/>
        <TextBlock Margin="5,0,5,0" Text="|" />
        <TextBlock Margin="5,0,5,0" Text="Gain/Loss (Open):" /><ContentPresenter Content="{Binding Path=TotalGainLoss}" ContentStringFormat="c"/>
        <TextBlock Margin="5,0,5,0" Text="|" />
        <TextBlock Margin="5,0,5,0" Text="Return (Open):" /><ContentPresenter Content="{Binding Path=TotalReturn}" ContentStringFormat="c"/>
        <TextBlock Margin="5,0,5,0" Text="|" />
        <TextBlock Margin="5,0,5,0" Text="Annual Dividend Yield (%):" /><ContentPresenter Content="{Binding Path=AnnualDividendYieldPcnt}" ContentStringFormat="c"/>
        <TextBlock Margin="5,0,5,0" Text="|" />
        <TextBlock Margin="5,0,5,0" Text="Annual Dividend Yield ($):" /><ContentPresenter Content="{Binding Path=AnnualDividendYieldAmt}" ContentStringFormat="c"/>
     </StackPanel>
       <StackPanel Grid.Row="2" Grid.Column="0" HorizontalAlignment="Left" Orientation="Horizontal" VerticalAlignment="Center">
        <TextBlock Margin="5,0,5,0" Text="Total Gain/Loss:" /><ContentPresenter Content="{Binding Path=TotalGainLossAll}" ContentStringFormat="c"/>
        <TextBlock Margin="5,0,5,0" Text="|" />
        <TextBlock Margin="5,0,5,0" Text="Total Return:" /><ContentPresenter Content="{Binding Path=TotalReturnAll}" ContentStringFormat="c"/>
     </StackPanel>
      <StackPanel Grid.Row="4" Grid.Column="0" HorizontalAlignment="Left" Orientation="Horizontal" VerticalAlignment="Center">
        <Button Margin="5,0,5,0" Width="100" Content="Update Prices" HorizontalAlignment="Left" Command="{Binding Path=RefreshCommand}"></Button>
        <Button Margin="5,0,5,0" Width="65" Content="Delete" HorizontalAlignment="Left" Command="{Binding Path=DeleteCommand}"></Button>
        <Button Margin="5,0,5,0" Width="65" Content="Edit" HorizontalAlignment="Left" Command="{Binding Path=EditCommand}"></Button>
     </StackPanel>
     </Grid>
    <telerik:RadGridView CanUserSelect="True"  dc:GridDataChangeBehavior.GridDataChangeProperty="{Binding Path=GridChanged}" SelectionMode="Single" SelectedItem="{Binding SelectedItem, Mode=TwoWay}" ItemContainerStyle="{StaticResource TradeItemStyle}" Grid.Row="0" Grid.Column="2" AlternationCount="2" AlternateRowBackground="Bisque" ShowGroupFooters="True" ShowColumnFooters="True" ItemsSource="{Binding Path=AllTrades, ValidatesOnDataErrors=True}" AutoGenerateColumns="False" >
    <telerik:RadGridView.Columns>
        <telerik:GridViewDataColumn IsReadOnly="True" Header="TradeId" DataMemberBinding="{Binding Path=TradeId}" />
					<telerik:GridViewDataColumn IsReadOnly="True" Header="Symbol" DataMemberBinding="{Binding Path=Symbol}">
						<telerik:GridViewColumn.ToolTipTemplate>
						<DataTemplate >
							<StackPanel Orientation="Horizontal" >
								<TextBlock Background="LemonChiffon" MaxWidth="1000" TextWrapping="Wrap" Text="{Binding RelativeSource={RelativeSource FindAncestor,AncestorType={x:Type vw:BlotterView}},Path=DataContext.CompanyDescription}"/>
							</StackPanel>
						</DataTemplate>
						</telerik:GridViewColumn.ToolTipTemplate>
					</telerik:GridViewDataColumn>
          <telerik:GridViewDataColumn IsReadOnly="True" Header="Account" DataMemberBinding="{Binding Path=Account}" />
          <telerik:GridViewDataColumn IsReadOnly="True" Header="Trade Date" DataMemberBinding="{Binding Path=TradeDate,StringFormat='{}{0:MM/dd/yyyy}'}" />
          <telerik:GridViewDataColumn IsReadOnly="True" Header="Buy/Sell" DataMemberBinding="{Binding Path=BuySell}" />
          <telerik:GridViewDataColumn IsReadOnly="True" Header="Status" DataMemberBinding="{Binding Path=Status}" />
          <telerik:GridViewDataColumn IsReadOnly="True" Header="Shares" DataMemberBinding="{Binding Path=Shares,StringFormat='{}{0:0.000}'}" >
          <telerik:GridViewDataColumn.AggregateFunctions>
          <local:SumFunctionShares />
          </telerik:GridViewDataColumn.AggregateFunctions>
          </telerik:GridViewDataColumn>
          <telerik:GridViewDataColumn IsReadOnly="True" Header="Price" DataMemberBinding="{Binding Path=Price,StringFormat='{}{0:C3}'}" />
          <telerik:GridViewDataColumn IsReadOnly="True" Header="DaysHeld" DataMemberBinding="{Binding Path=DaysHeld}" />
          <telerik:GridViewDataColumn IsReadOnly="True" Header="Exposure" DataMemberBinding="{Binding Path=Exposure,StringFormat='{}{0:C}'}" >
            <telerik:GridViewDataColumn.AggregateFunctions>
            <local:SumFunctionExposure />
            </telerik:GridViewDataColumn.AggregateFunctions>
          </telerik:GridViewDataColumn>
          <telerik:GridViewDataColumn IsReadOnly="True" Header="Current Price" DataMemberBinding="{Binding Path=CurrentPrice,StringFormat='{}{0:C}'}" />
          <telerik:GridViewDataColumn IsReadOnly="True" Header="Timestamp" DataMemberBinding="{Binding Path=PriceDate}" />            
          <telerik:GridViewDataColumn IsReadOnly="True" Header="Market Value" DataMemberBinding="{Binding Path=MarketValue,StringFormat='{}{0:C}'}" >
            <telerik:GridViewDataColumn.AggregateFunctions>
            <local:SumFunctionMarketValue/>
            </telerik:GridViewDataColumn.AggregateFunctions>
          </telerik:GridViewDataColumn>
        <telerik:GridViewDataColumn IsReadOnly="True" Header="Weight" DataMemberBinding="{Binding Path=Weight,StringFormat='{}{0:P}'}" >
          <telerik:GridViewDataColumn.AggregateFunctions>
            <local:SumFunctionWeight/>
          </telerik:GridViewDataColumn.AggregateFunctions>
        </telerik:GridViewDataColumn>
<!--DataMemberBinding="{Binding Path=GainLoss,StringFormat='{}{0:C}'}"-->
        <telerik:GridViewDataColumn IsReadOnly="True" Header="Gain/Loss"  >
           <telerik:GridViewDataColumn.CellTemplate>
           <DataTemplate>
              <TextBlock Text="{Binding Path=GainLoss,StringFormat='{}{0:C}'}" Foreground="{Binding Path=GainLossColor}"/>
           </DataTemplate>
           </telerik:GridViewDataColumn.CellTemplate>
            <telerik:GridViewDataColumn.AggregateFunctions>
              <local:SumFunctionGainLoss />
            </telerik:GridViewDataColumn.AggregateFunctions>
          </telerik:GridViewDataColumn>
<!--DataMemberBinding="{Binding Path=Return,StringFormat='{}{0:P}'}"--> 
          <telerik:GridViewDataColumn IsReadOnly="True" Header="Return" >
           <telerik:GridViewDataColumn.CellTemplate>
           <DataTemplate>
              <TextBlock Text="{Binding Path=Return,StringFormat='{}{0:P}'}" Foreground="{Binding Path=ReturnColor}"/>
           </DataTemplate>
           </telerik:GridViewDataColumn.CellTemplate>
            <telerik:GridViewDataColumn.AggregateFunctions>
              <local:TotalReturnFunction />
            </telerik:GridViewDataColumn.AggregateFunctions>
          </telerik:GridViewDataColumn>
        <telerik:GridViewDataColumn IsReadOnly="True" Header="Todays Change"  >
           <telerik:GridViewDataColumn.CellTemplate>
           <DataTemplate>
              <TextBlock Text="{Binding Path=TodaysChange,StringFormat='{}{0:C}'}" Foreground="{Binding Path=TodaysChangeColor}"/>
           </DataTemplate>
           </telerik:GridViewDataColumn.CellTemplate>
            <telerik:GridViewDataColumn.AggregateFunctions>
              <local:SumFunctionTodaysChange />
            </telerik:GridViewDataColumn.AggregateFunctions>
        </telerik:GridViewDataColumn>
          <telerik:GridViewDataColumn IsReadOnly="True" Header="SellDate" DataMemberBinding="{Binding Path=SellDate}" />
          <telerik:GridViewDataColumn IsReadOnly="True" Header="SellPrice" DataMemberBinding="{Binding Path=SellPrice}" />
          <telerik:GridViewDataColumn IsReadOnly="True" Header="Commission" DataMemberBinding="{Binding Path=Commission,StringFormat='{}{0:C}'}" >
          </telerik:GridViewDataColumn>
    </telerik:RadGridView.Columns>
    </telerik:RadGridView>
  </DockPanel>
  </Grid>
  </wpfx:BusyIndicator>
</UserControl>
