<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             xmlns:d="http://xamarin.com/schemas/2014/forms/design"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
             xmlns:vm="clr-namespace:Navigator.ViewModels"     
             ControlTemplate="{StaticResource ActivityIndicatorTemplate}"
             mc:Ignorable="d"
             Title="{Binding Title}"
             x:Class="Navigator.Views.GainLossPage">

  <ContentPage.BindingContext>
    <vm:GainLossViewModel />
  </ContentPage.BindingContext>
  
  <ContentPage.Resources>
    <ResourceDictionary>
      <Style x:Key="NameLabel" TargetType="Label">
        <Setter Property="FontSize" Value="14"/>
        <Setter Property="FontAttributes" Value="Bold" />
        <Setter Property="TextColor" Value="Black" />
      </Style>

      <Style x:Key="CompanyLabel" TargetType="Label">
        <Setter Property="FontSize" Value="14" />
        <Setter Property="FontAttributes" Value="Bold" />
        <Setter Property="TextColor" Value="Black" />
      </Style>

      <Style x:Key="LabelStyle" TargetType="Label">
        <Setter Property="FontSize" Value="12" />
        <Setter Property="LineBreakMode" Value="NoWrap"/>
      </Style>

      <Style x:Key="horizontalStackLayout" TargetType="StackLayout">
        <Setter Property="Orientation" Value="Horizontal" />
      </Style>      
      
      <Style x:Key="verticalStackLayout" TargetType="StackLayout">
        <Setter Property="Orientation" Value="Vertical" />
      </Style>

      <Style x:Key="detailsStackLayout" TargetType="StackLayout">
        <Setter Property="HorizontalOptions" Value="FillAndExpand" />
        <Setter Property="VerticalOptions" Value="FillAndExpand" />
        <Setter Property="Spacing" Value="2" />
      </Style>

      <Style x:Key="imageStyle" TargetType="Image">
        <Setter Property="WidthRequest" Value="8" />
        <Setter Property="HeightRequest" Value="8" />
      </Style>

      <Style x:Key="profileImageStyle" TargetType="Image">
        <Setter Property="WidthRequest" Value="80" />
        <Setter Property="HeightRequest" Value="60" />
        <Setter Property="Aspect" Value="AspectFit" />
      </Style>

      <Style x:Key="profileImageFrameStyle" TargetType="Frame">
        <Setter Property="CornerRadius" Value="5" />
        <Setter Property="Margin" Value="0" />
        <Setter Property="Padding" Value="0" />
        <Setter Property="IsClippedToBounds" Value="True" />
        <Setter Property="MinimumWidthRequest" Value="50" />
      </Style>

      <Style x:Key="FrameCardViewStyle" TargetType="Frame">
        <Setter Property="CornerRadius" Value="5" />
        <Setter Property="Margin" Value="5" />
        <Setter Property="Padding" Value="5" />
        <Setter Property="HorizontalOptions" Value="FillAndExpand" />
        <Setter Property="OutlineColor" Value="LightGray" />
        <Setter Property="BorderColor" Value="LightGray" />
        <Setter Property="HasShadow" Value="True" />
      </Style>

      <Style x:Key="itemsCardViewStackLayoutStyle" TargetType="StackLayout">
        <Setter Property="Margin" Value="5" />
        <Setter Property="HorizontalOptions" Value="FillAndExpand" />
        <Setter Property="Spacing" Value="8" />
      </Style>

      <Style x:Key="itemsInsideCardViewStackLayoutStyle" TargetType="StackLayout">
        <Setter Property="Orientation" Value="Horizontal" />
        <Setter Property="Spacing" Value="8" />
      </Style>
    </ResourceDictionary>
  </ContentPage.Resources>


  <!--<ActivityIndicator IsRunning="{Binding IsBusy}" />-->

  <ContentPage.Content>
    <Grid BackgroundColor="White" Margin="2">
      <Grid.ColumnDefinitions>
        <ColumnDefinition Width="Auto" />
        <ColumnDefinition Width="*" />
      </Grid.ColumnDefinitions>
      <Grid.RowDefinitions>
        <RowDefinition Height="20*" />
        <RowDefinition Height="45*" />
      </Grid.RowDefinitions>

      <Frame Grid.Row="0" Style="{StaticResource FrameCardViewStyle}">
        <StackLayout  Orientation="Vertical">
          <Label Text="Account" HorizontalTextAlignment="Left" FontSize="15"></Label>
          <ListView x:Name="listView" SeparatorVisibility="Default" ItemsSource="{Binding Accounts}" SelectedItem="{Binding ListViewSelectedItem}" >
            <ListView.ItemTemplate>
              <DataTemplate>
                <TextCell Text="{Binding .}"/>
              </DataTemplate>
            </ListView.ItemTemplate>
          </ListView>

        </StackLayout>
      </Frame>

<!--//      <ActivityIndicator IsRunning="{Binding IsBusy}" />-->
      <RefreshView Grid.Row="1" IsRefreshing="{Binding IsRefreshing, Mode=TwoWay}" Command="{Binding RefreshCommand}">

        <CollectionView x:Name="collectionView" ItemsSource="{Binding GainLossSummary}" SelectionMode="Single" SelectedItem="{Binding GainLossSummaryItemDetailSelectedItem,Mode=TwoWay}">
          <CollectionView.ItemTemplate>
            <DataTemplate>
              <StackLayout Spacing="8" HorizontalOptions="EndAndExpand">
                <Frame Style="{StaticResource FrameCardViewStyle}">
                  <StackLayout Style="{StaticResource itemsCardViewStackLayoutStyle}">
                    <StackLayout Style="{StaticResource itemsInsideCardViewStackLayoutStyle}">
                      <Frame Style="{StaticResource profileImageFrameStyle}">
                        <Label Text="{Binding Symbol}" Style="{StaticResource NameLabel}" />
                      </Frame>
                      <StackLayout Style="{StaticResource detailsStackLayout}">

                        <StackLayout Style="{StaticResource horizontalStackLayout}">
                          <Label Text="{Binding CompanyName}" Style="{StaticResource CompanyLabel}" />
                        </StackLayout>

                        <StackLayout Style="{StaticResource horizontalStackLayout}">
                          <Label Text="Date :" Style="{StaticResource LabelStyle}" />
                          <Label Text="{Binding Date,StringFormat='{}{0:MM/dd/yyyy}'}" Style="{StaticResource LabelStyle}" />
                          <!--<Image Source="{local:ImageResource stop.png}" />-->
                          <Image Source="stop.png" HorizontalOptions="End" HeightRequest="14" IsVisible="{Binding HasStopLimit}"/>
                        </StackLayout>

                        <StackLayout Style="{StaticResource horizontalStackLayout}">
                          <Label Text="$ Change :" Style="{StaticResource LabelStyle}" />
                          <Label Text="{Binding Change,StringFormat='{}{0:C}'}" Style="{StaticResource LabelStyle}" />
                        </StackLayout>

                        <StackLayout Style="{StaticResource horizontalStackLayout}">
                          <Label Text="$ Change(%) :" Style="{StaticResource LabelStyle}" />
                          <Label Text="{Binding ChangePercent,StringFormat='{}{0:N}'}" Style="{StaticResource LabelStyle}" />
                        </StackLayout>

                        <StackLayout Style="{StaticResource horizontalStackLayout}">
                          <Label Text="Current G/L :" Style="{StaticResource LabelStyle}" />
                          <Label Text="{Binding CurrentGainLoss,StringFormat='{}{0:C}'}" Style="{StaticResource LabelStyle}" />
                        </StackLayout>

                        <StackLayout Style="{StaticResource horizontalStackLayout}">
                          <Label Text="Previous G/L :" Style="{StaticResource LabelStyle}" />
                          <Label Text="{Binding PreviousGainLoss,StringFormat='{}{0:C}'}" Style="{StaticResource LabelStyle}" />
                        </StackLayout>

                        <StackLayout Style="{StaticResource horizontalStackLayout}">
                          <Label Text="Exposure:" Style="{StaticResource LabelStyle}" />
                          <Label Text="{Binding Exposure,StringFormat='{}{0:C}'}" Style="{StaticResource LabelStyle}" />
                          <Label Text="Shares:" Style="{StaticResource LabelStyle}" />
                          <Label Text="{Binding Shares,StringFormat='{}{0:#.00}'}" Style="{StaticResource LabelStyle}" />
                          <Label Text="Lots:" Style="{StaticResource LabelStyle}" />
                          <Label Text="{Binding Lots,StringFormat='{}{0:N0}'}" Style="{StaticResource LabelStyle}" />
                        </StackLayout>

                        <StackLayout Style="{StaticResource horizontalStackLayout}">
                          <Label Text="Dividend Yield:" Style="{StaticResource LabelStyle}" />
                          <Label Text="{Binding DividendYield,StringFormat='{}{0:P}'}" Style="{StaticResource LabelStyle}" />
                          <Label Text="Annual Dividend:" Style="{StaticResource LabelStyle}" />
                          <Label Text="{Binding AnnualDividend,StringFormat='{}{0:C}'}" Style="{StaticResource LabelStyle}" />
                        </StackLayout>
                        
                        <StackLayout Style="{StaticResource horizontalStackLayout}">
                          <Label Text="Latest Price:" Style="{StaticResource LabelStyle}" />
                          <Label Text="{Binding LatestPrice.Close,StringFormat='{}{0:C}'}" Style="{StaticResource LabelStyle}" />
                          <Label Text="Change:" Style="{StaticResource LabelStyle}" />
                          <Label Text="{Binding PriceChange,StringFormat='{}{0:P}'}" Style="{StaticResource LabelStyle}" />
                        </StackLayout>

                        <StackLayout Style="{StaticResource horizontalStackLayout}">
                          <Label Text="{Binding ParityElementDescription}" Style="{StaticResource LabelStyle}" />
                        </StackLayout>

                      </StackLayout>
                    </StackLayout>
                  </StackLayout>
                </Frame>
              </StackLayout>
            </DataTemplate>
          </CollectionView.ItemTemplate>
        </CollectionView >
      </RefreshView>
    </Grid>
  </ContentPage.Content>
</ContentPage>