<?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:local="clr-namespace:ZicMoove;assembly=ZicMoove"
             xmlns:converters="clr-namespace:ZicMoove.Converters;assembly=ZicMoove"
             xmlns:views="clr-namespace:ZicMoove.Views;assembly=ZicMoove"
             xmlns:behaviors="clr-namespace:ZicMoove.Behaviors;assembly=ZicMoove"
             xmlns:extensions="clr-namespace:ZicMoove.Extensions;assembly=ZicMoove"
             x:Class="ZicMoove.Pages.EditBillingLinePage"
             Style="{StaticResource PageStyle}">
  <ContentPage.Resources>
    <ResourceDictionary>
      <Style TargetType="Label">
        <Setter Property="Style" Value="{StaticResource ContentLabelStyle}" />
      </Style>
      <Style TargetType="Button">
        <Setter Property="Style" Value="{StaticResource ButtonStyle}" />
      </Style>
      <converters:EnumConverter x:Key="enumToInt" />
      <converters:BooleanToObjectConverter x:Key="boolToStyleImage" x:TypeArguments="Style">

        <converters:BooleanToObjectConverter.FalseObject>
          <Style TargetType="Image">
            <Setter Property="HeightRequest" Value="20" />
            <Setter Property="Source"
                    Value="{extensions:ImageResource ZicMoove.Images.Validation.error.png}" />
          </Style>
        </converters:BooleanToObjectConverter.FalseObject>

        <converters:BooleanToObjectConverter.TrueObject>
          <Style TargetType="Image">
            <Setter Property="HeightRequest" Value="20" />
            <Setter Property="Source"
                    Value="{extensions:ImageResource ZicMoove.Images.Validation.success.png}" />
          </Style>
        </converters:BooleanToObjectConverter.TrueObject>

      </converters:BooleanToObjectConverter>

    </ResourceDictionary>
  </ContentPage.Resources>
  <ScrollView>

    <StackLayout x:Name="mainStackLayout">
      <StackLayout Orientation="Horizontal">
        <Label Text="Description de la ligne de facture"
               Style="{StaticResource InputLabelStyle}"></Label>
        <Image  x:Name="descriptionSuccessErrorImage"
             Style="{Binding Source={x:Reference descriptionLenValidator}, Path=IsValid, Converter={StaticResource boolToStyleImage}}" />
      </StackLayout>
          <Editor HorizontalOptions="FillAndExpand" VerticalOptions="FillAndExpand" Text="{Binding Description, Mode=TwoWay}">
            <Editor.Behaviors>
              <behaviors:EditorMaxLengthValidator x:Name="descriptionLenValidator" MaxLength="12" MinLength="3" />
            </Editor.Behaviors>
          </Editor>
      <Label Text="{Binding Source={x:Reference descriptionLenValidator}, Path=Error}"
               Style="{StaticResource ErrorLabelStyle}"></Label>

      <StackLayout Orientation="Horizontal">
        <Label Text="Durée de la prestation"
               Style="{StaticResource InputLabelStyle}">
        </Label>
        <Image  x:Name="durationSuccessErrorImage"
                  Style="{Binding Source={x:Reference durationValidator}, 
                                Path=IsValid, 
                                Converter={StaticResource boolToStyleImage}}" />
      </StackLayout>
      <StackLayout Orientation="Horizontal">
        <Entry Placeholder="Durée" Keyboard="Numeric" Style="{StaticResource BigEntry}"
              Text="{Binding DurationValue, Mode=TwoWay, StringFormat='{0}'}" >
          <Entry.Behaviors>
            <behaviors:DecimalValidatorBehavior x:Name="durationValidator" />
          </Entry.Behaviors>
        </Entry>
        <views:EnumPicker x:Name="picker" Style="{StaticResource PickerStyle}"
                                 EnumSource="{Binding DurationUnit}"
                Title="Unité de temps"
                SelectedItem="{Binding DurationUnit, Mode=TwoWay}">
        </views:EnumPicker>
        
      </StackLayout>
      <StackLayout Orientation="Horizontal">
      <Label Text="Quantité facturée" Style="{StaticResource InputLabelStyle}"></Label>
        <Image  x:Name="countSuccessErrorImage"
                 Style="{Binding Source={x:Reference countValidator}, 
                                Path=IsValid, 
                                Converter={StaticResource boolToStyleImage}}" />
      </StackLayout>
      
      <Entry Text="{Binding Count, Mode=TwoWay}" Placeholder="Quantité" Keyboard="Numeric"
             Style="{StaticResource BigEntry}">
        <Entry.Behaviors>
          <behaviors:IntegerEntryBehavior x:Name="countValidator" Min="0" Max="10" />
        </Entry.Behaviors>
      </Entry>
        <StackLayout Orientation="Horizontal">
          <Label Text="{Binding Source={x:Reference countValidator}, Path=Error}"
             Style="{StaticResource ErrorLabelStyle}"></Label>
        </StackLayout>
      
      <StackLayout Orientation="Horizontal">
      <Label Text="Prix unitaire" Style="{StaticResource InputLabelStyle}">
      </Label>
      <Image  x:Name="unitaryCostSuccessErrorImage"
                Style="{Binding Source={x:Reference unitCostValidator}, 
                                Path=IsValid, 
                                Converter={StaticResource boolToStyleImage}}" />
      </StackLayout>
      <StackLayout Orientation="Horizontal">
        <Entry Text="{Binding UnitaryCostText, Mode=TwoWay}" Placeholder="Prix"
               Keyboard="Numeric" Style="{StaticResource BigEntry}">
          <Entry.Behaviors>
            <behaviors:DecimalValidatorBehavior x:Name="unitCostValidator" />
          </Entry.Behaviors>
        </Entry>
        <Label Text="€" Style="{StaticResource BigLabelStyle}" />
      </StackLayout>
      
      <StackLayout Orientation="Horizontal">
        <Button Text="Términé"
                Command="{Binding ValidateCommand}"
                Clicked="OnValidateClicked"
                IsEnabled="{Binding ModelState.IsValid}"></Button>
        <Button Text="Supprimer"
                    Command="{Binding RemoveCommand}"
                    Clicked="OnDeleteClicked"></Button>
      </StackLayout>
    </StackLayout>
  </ScrollView>
</ContentPage>
