<?xml version="1.0" encoding="utf-8" ?>
<views:BasePage x:Class="Italia.DiciottoApp.Views.WalletPage"
                xmlns="http://xamarin.com/schemas/2014/forms"
                xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
                xmlns:views="clr-namespace:Italia.DiciottoApp.Views"
                xmlns:vm="clr-namespace:Italia.DiciottoApp.ViewModels"
                xmlns:conv="clr-namespace:Italia.DiciottoApp.Converters"
                xmlns:ios="clr-namespace:Xamarin.Forms.PlatformConfiguration.iOSSpecific;assembly=Xamarin.Forms.Core" 
                ios:Page.UseSafeArea="true"
                BackgroundColor="{StaticResource AppBkgndColor}"
                ControlTemplate="{StaticResource AppHeaderTemplate}" >

    <views:BasePage.BindingContext>
        <vm:WalletViewModel />
    </views:BasePage.BindingContext>

    <views:BasePage.Resources>
        <conv:DoubleToItCurrency x:Key="DoubleToItCurrency" />
    </views:BasePage.Resources>

    <views:BasePage.Content>
        <Grid VerticalOptions="FillAndExpand"
              HorizontalOptions="FillAndExpand"
              RowSpacing="0" ColumnSpacing="0" >

            <Grid.RowDefinitions>
                <RowDefinition Height="50"/>
                <RowDefinition Height="Auto"/>
                <RowDefinition Height="*"/>
            </Grid.RowDefinitions>

            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="*" />
                <ColumnDefinition Width="*" />
                <ColumnDefinition Width="*" />
            </Grid.ColumnDefinitions>

            <BoxView Grid.Row="0" Grid.Column="0" Grid.ColumnSpan="3"
                     BackgroundColor="{StaticResource TabsBkgndColor}" />

            <BoxView Grid.Row="0" Grid.Column="{Binding WalletTabIndex}"
                     HeightRequest="5"
                     VerticalOptions="End"
                     BackgroundColor="White" />

            <StackLayout Grid.Row="0" Grid.Column="0" BackgroundColor="Transparent">
                <StackLayout.GestureRecognizers>
                    <TapGestureRecognizer Tapped="OnAllTabTapped" />
                </StackLayout.GestureRecognizers>
                <Label Text="TUTTI"
                       VerticalOptions="CenterAndExpand"
                       Style="{StaticResource TabLabelStyle}" />
            </StackLayout>

            <StackLayout Grid.Row="0" Grid.Column="1" BackgroundColor="Transparent">
                <StackLayout.GestureRecognizers>
                    <TapGestureRecognizer Tapped="OnAvailableTabTapped" />
                </StackLayout.GestureRecognizers>
                <Label Text="DA SPENDERE"
                       VerticalOptions="CenterAndExpand"
                       Style="{StaticResource TabLabelStyle}" />
            </StackLayout>

            <StackLayout Grid.Row="0" Grid.Column="2" BackgroundColor="Transparent">
                <StackLayout.GestureRecognizers>
                    <TapGestureRecognizer Tapped="OnSpentTabTapped" />
                </StackLayout.GestureRecognizers>
                <Label Text="GIÀ SPESI"
                       VerticalOptions="CenterAndExpand"
                       Style="{StaticResource TabLabelStyle}" />
            </StackLayout>

            <!-- Content header -->
            <Label Grid.Row="1" Grid.ColumnSpan="3" Margin="16,16,16,0"
                   VerticalOptions="Center" 
                   HorizontalOptions="Start"
                   Style="{StaticResource RegularLabelStyle}"
                   TextColor="#FFAAAAAA" FontSize="14"
                   Text="{Binding ContentHeader}" />

            <!-- All Voucher list -->
            <ListView Grid.Row="2" Grid.ColumnSpan="3" Margin="0,0,16,0"
                      ItemsSource="{Binding Vouchers, Mode=OneWay}"
                      HasUnevenRows="true"
                      IsVisible="{Binding VoucherListIsVisible}"
                      ItemTapped="OnListViewItemTapped">
                <ListView.ItemTemplate>
                    <DataTemplate>
                        <ViewCell>
                            <StackLayout Padding="0" Spacing="0">
                                
                                <!-- Available -->
                                <StackLayout Padding="0" Spacing="0"
                                             Orientation="Horizontal"
                                             IsVisible="{Binding Spent, Converter={StaticResource NegativeConverter}}">

                                    <Image Margin="16,0,0,0"
                                           Source="{Binding Category.ImageSource}"
                                           HeightRequest="22" WidthRequest="22"
                                           VerticalOptions="Center" />

                                    <StackLayout Padding="0" Spacing="0"
                                                 Margin="32,0,16,0"
                                                 VerticalOptions="Center"
                                                 HorizontalOptions="StartAndExpand">

                                        <Label HorizontalOptions="Start" VerticalOptions="Center"
                                               Style="{StaticResource BoldLabelStyle}"
                                               TextColor="#FF1168C9" FontSize="16"
                                               Text="{Binding Category.Titolo}" />

                                        <Label HorizontalOptions="Start" VerticalOptions="Center"
                                               Style="{StaticResource RegularLabelStyle}"
                                               TextColor="#FF1168C9" FontSize="14"
                                               Text="{Binding Product.Titolo}" />

                                        <Label IsVisible="{Binding HasShop}"
                                               HorizontalOptions="Start" VerticalOptions="Center"
                                               Style="{StaticResource RegularLabelStyle}"
                                               TextColor="#FF1168C9" FontSize="14"
                                               Text="{Binding Shop.Title}" />
                                    </StackLayout>

                                    <Label Margin="8,0"
                                           HorizontalOptions="End" VerticalOptions="Center"
                                           Style="{StaticResource BoldLabelStyle}"
                                           TextColor="#FF1168C9" FontSize="24"
                                           LineBreakMode="NoWrap"
                                           Text="{Binding RequestedValue, Converter={StaticResource DoubleToItCurrency}}" />
                                    <Image Margin="0,0,6,0"
                                           HorizontalOptions="End" VerticalOptions="CenterAndExpand"
                                           Source="forward"/>
                                </StackLayout>
                                
                                <!-- Spent -->
                                <StackLayout Padding="0" Spacing="0"
                                             Orientation="Horizontal"
                                             IsVisible="{Binding Spent}">

                                    <Image Margin="16,0,0,0"
                                           Source="{Binding Category.BlackImageSource}"
                                           HeightRequest="22" WidthRequest="22"
                                           VerticalOptions="Center"
                                           Opacity=".7"/>

                                    <StackLayout Padding="0" Spacing="0"
                                                 Margin="32,0,16,0"
                                                 VerticalOptions="Center"
                                                 HorizontalOptions="StartAndExpand">

                                        <Label HorizontalOptions="Start" VerticalOptions="Center"
                                               Style="{StaticResource BoldLabelStyle}"
                                               TextColor="#FF596672" FontSize="16"
                                               Text="{Binding Category.Titolo}" />

                                        <Label HorizontalOptions="Start" VerticalOptions="Center"
                                               Style="{StaticResource RegularLabelStyle}"
                                               TextColor="#FF596672" FontSize="14"
                                               Text="{Binding Product.Titolo}" />
                                        
                                        <Label IsVisible="{Binding HasShop}"
                                               HorizontalOptions="Start" VerticalOptions="Center"
                                               Style="{StaticResource RegularLabelStyle}"
                                               TextColor="{StaticResource AppLightGrayColor}" FontSize="14"
                                               Text="{Binding Shop.Title}" />
                                    </StackLayout>

                                    <Grid Margin="8,0"
                                          HorizontalOptions="Start" VerticalOptions="Center" >
                                        
                                        <Label HorizontalOptions="End" VerticalOptions="Center"
                                               Style="{StaticResource BoldLabelStyle}"
                                               TextColor="#FF596672" FontSize="24"
                                               LineBreakMode="NoWrap"
                                               Text="{Binding ValidatedValue, Converter={StaticResource DoubleToItCurrency}}" />

                                        <BoxView HeightRequest="1" BackgroundColor="#FF596672" VerticalOptions="Center"/>
                                    </Grid>

                                    <Image Margin="0,0,6,0"
                                           HorizontalOptions="End" VerticalOptions="CenterAndExpand"
                                           Source="forward"/>
                                </StackLayout>
                            </StackLayout>
                        </ViewCell>
                    </DataTemplate>
                </ListView.ItemTemplate>
            </ListView>
        </Grid>
    </views:BasePage.Content>
</views:BasePage>