<?xml version="1.0" encoding="utf-8" ?>
<views:BasePage x:Class="Italia.DiciottoApp.Views.NewVoucherValuePage"
                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: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:NewVoucherValueViewModel />
    </views:BasePage.BindingContext>

    <views:BasePage.Content>
        <Grid x:Name="ContentGrid"              
              BackgroundColor="{StaticResource ContentBkgndColor}"
              VerticalOptions="FillAndExpand"
              HorizontalOptions="FillAndExpand"
              RowSpacing="0" ColumnSpacing="0" >

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

            <!-- Shop-->
            <Image Grid.Row="0"
                   WidthRequest="{Binding Width, Source={x:Reference ContentGrid}}"
                   HeightRequest="88"
                   Aspect="AspectFill"
                   Source="{Binding ShopBkgndImageSource}"
                   IsVisible="{Binding HasShop}" />

            <!-- Shop info -->
            <StackLayout Grid.Row="0" Padding="24,0" Spacing="0"
                         VerticalOptions="Center"
                         IsVisible="{Binding HasShop}">

                <Label Margin="0"
                       HorizontalOptions="Start"
                       Style="{StaticResource BoldLabelStyle}"
                       TextColor="White" FontSize="32"
                       Text="{Binding Shop.Title}" />

                <StackLayout Padding="0" Spacing="0"
                             Orientation="Horizontal"
                             IsVisible="{Binding HasShop}">

                    <Image HorizontalOptions="Start" VerticalOptions="Center"
                           Source="{Binding ShopKindImageSource}"/>

                    <Label Margin="11,0,0,0"
                           HorizontalOptions="Start"
                           VerticalOptions="Center"
                           Style="{StaticResource BoldItalicLabelStyle}"
                           TextColor="White" FontSize="14"
                           Text="{Binding ShopAddress}" />
                </StackLayout>
            </StackLayout>

            <!-- Category-->
            <StackLayout Grid.Row="1"
                         Padding="0" Spacing="0"
                         Orientation="Horizontal">

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

                <Label Margin="32,20,16,20"
                       HorizontalOptions="StartAndExpand" VerticalOptions="Center"
                       Style="{StaticResource RegularLabelStyle}"
                       TextColor="#FF30383D" FontSize="16"
                       Text="{Binding Categoria.Titolo}" />

                <Image Margin="0,0,22,0"
                       HorizontalOptions="End" VerticalOptions="CenterAndExpand"
                       Source="selected_black"/>
            </StackLayout>

            <!-- Product -->
            <StackLayout Grid.Row="2"
                         Padding="0" Spacing="0"
                         Orientation="Horizontal">

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

                <Label Margin="32,20,16,20"
                       HorizontalOptions="StartAndExpand" VerticalOptions="Center"
                       Style="{StaticResource RegularLabelStyle}"
                       TextColor="#FF30383D" FontSize="16"
                       Text="{Binding Prodotto.Titolo}" />

                <Image Margin="0,0,22,0"
                       HorizontalOptions="End" VerticalOptions="CenterAndExpand"
                       Source="selected_black"/>
            </StackLayout>

            <!-- IsOnline Message -->
            <Label Grid.Row="3" Margin="16,16,16,0"
                   VerticalOptions="CenterAndExpand" 
                   HorizontalOptions="StartAndExpand"
                   Style="{StaticResource RegularLabelStyle}"
                   TextColor="#FFAAAAAA" FontSize="14"
                   Text="{Binding IsOnlineMessage}" />

            <!-- IsOnline -->
            <StackLayout Grid.Row="4" Margin="16,20,16,0"
                         Padding="0" Spacing="0"
                         Orientation="Horizontal">

                <Label HorizontalOptions="StartAndExpand" VerticalOptions="Center"
                       Style="{StaticResource RegularLabelStyle}"
                       TextColor="#FF30383D" FontSize="16"
                       Text="Buono per acquisto online" />

                <Switch HorizontalOptions="Start" VerticalOptions="Center"
                        OnColor="#0066CC" ThumbColor="White" 
                        IsToggled="{Binding Shop.IsOnline}" />
            </StackLayout>

            <!-- Value Message -->
            <Label Grid.Row="5" Margin="16,16,16,0"
                   VerticalOptions="CenterAndExpand" 
                   HorizontalOptions="StartAndExpand"
                   Style="{StaticResource RegularLabelStyle}"
                   TextColor="#FFAAAAAA" FontSize="14"
                   Text="{Binding ValueMessage}" />

            <!-- Value -->
            <StackLayout Grid.Row="6" Margin="16,20,16,0"
                         Padding="0" Spacing="0"
                         Orientation="Horizontal"
                         VerticalOptions="StartAndExpand">

                <Label HorizontalOptions="Start" VerticalOptions="Center"
                       Style="{StaticResource RegularLabelStyle}"
                       TextColor="#FF30383D" FontSize="16"
                       Text="Valore" />

                <!-- Big problem with Android Keyboard (some don't have comma so we should set "Android=Default" -->
                <!-- Actually we decided to force everyone to install SwiftKey Keyboard -->
                <Entry x:Name="VoucherValueEntry" Margin="16,0"
                       Keyboard="{OnPlatform Default=Numeric, Android=Numeric}" 
                       MaxLength="6" WidthRequest="107"
                       TextChanged="OnEntryTextChanged"
                       Text="{Binding EntryValue}"/>

                <Image HorizontalOptions="End" VerticalOptions="CenterAndExpand"
                       Source="euro"/>

                <Button Margin="0,0,8,0"
                        HorizontalOptions="EndAndExpand" VerticalOptions="Center"
                        HeightRequest="40" WidthRequest="107"
                        Style="{StaticResource CommandButtonStyle}"
                        FontSize="16" Text="CREA"
                        IsEnabled="{Binding CreateVoucherButtonEnabled}"
                        Clicked="OnCreaButtonClicked">
                    <VisualStateManager.VisualStateGroups>
                        <VisualStateGroup x:Name="CommonStates">
                            <VisualState x:Name="Normal">
                                <VisualState.Setters>
                                    <Setter Property="BackgroundColor" Value="{StaticResource AppBkgndColor}" />
                                </VisualState.Setters>
                            </VisualState>
                            <VisualState x:Name="Disabled">
                                <VisualState.Setters>
                                    <Setter Property="BackgroundColor" Value="LightGray" />
                                </VisualState.Setters>
                            </VisualState>
                        </VisualStateGroup>
                    </VisualStateManager.VisualStateGroups>
                </Button>
                
            </StackLayout>

            <!-- CreateVoucherActionInProgress Message -->
            <Label Grid.Row="7" Margin="16,16,16,0"
                   VerticalOptions="StartAndExpand" 
                   HorizontalOptions="StartAndExpand"
                   Style="{StaticResource RegularLabelStyle}"
                   TextColor="Gray" FontSize="14"
                   Text="Creazione buono in corso..." 
                   IsVisible="{Binding CreateVoucherActionInProgress}"/>
            
            <!-- Error Message -->
            <Label Grid.Row="8" Margin="16,16,16,0"
                   VerticalOptions="StartAndExpand" 
                   HorizontalOptions="StartAndExpand"
                   Style="{StaticResource RegularLabelStyle}"
                   TextColor="Red" FontSize="14"
                   Text="{Binding ErrorMessage}" />
        </Grid>
    </views:BasePage.Content>
</views:BasePage>