<?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:Calculator.ViewModels"
             xmlns:control="clr-namespace:Calculator.Controls"
             x:Class="Calculator.Views.CalculatorMainPage"
             BackgroundImage="bg_p.png"
             Padding="0, 0, 0, 0">

  <ContentPage.BindingContext>
    <local:MainPageViewModel />
  </ContentPage.BindingContext>

  <ContentPage.Resources>
    <ResourceDictionary>
      <local:StringToColorConverter x:Key="StringToColorConverter"/>

      <Style x:Key="font1" TargetType="Label" >
        <Setter Property="TextColor" Value="#4E61AD" />
        <Setter Property="FontFamily" Value="Samsung sans" />
      </Style>
      <Style x:Key="font3" TargetType="Label" >
        <Setter Property="TextColor" Value="#7F4E61AD" />
        <Setter Property="FontFamily" Value="Samsung sans" />
      </Style>
      <Style x:Key="font4" TargetType="Label" >
        <Setter Property="TextColor" Value="#59B03A" />
        <Setter Property="FontFamily" Value="Samsung sans" />
      </Style>

      <Style x:Key="inputString" TargetType="Label" BasedOn="{StaticResource font1}">
        <Setter Property="LineBreakMode" Value="WordWrap" />
        <Setter Property="HorizontalTextAlignment" Value="End" />
      </Style>

      <Style x:Key="sumString" TargetType="Label" BasedOn="{StaticResource font3}">
        <Setter Property="VerticalTextAlignment" Value="Center" />
        <Setter Property="LineBreakMode" Value="NoWrap" />
        <Setter Property="HorizontalTextAlignment" Value="End" />
      </Style>

      <!-- Style is not setted to CustomRenderer. Same with Android. -->
      <Style x:Key="backButton" TargetType="Image">
        <Setter Property="HeightRequest" Value="47"/>
        <Setter Property="WidthRequest" Value="80"/>
        <Setter Property="HorizontalOptions" Value="End"/>
      </Style>

      <Style x:Key="keypadButtonA11" TargetType="Image">
        <Setter Property="BackgroundColor" Value="#0F000000"/>
      </Style>
      <Style x:Key="keypadButtonA12" TargetType="Image" BasedOn="{StaticResource keypadButtonA11}">
        <Setter Property="BackgroundColor" Value="#1F000000"/>
      </Style>
      <Style x:Key="keypadButtonA13" TargetType="Image" BasedOn="{StaticResource keypadButtonA11}">
        <Setter Property="BackgroundColor" Value="#2E000000"/>
      </Style>
      <Style x:Key="keypadButtonA14" TargetType="Image" BasedOn="{StaticResource keypadButtonA11}">
        <Setter Property="BackgroundColor" Value="#5B000000"/>
      </Style>
      <Style x:Key="keypadButtonB" TargetType="Image" BasedOn="{StaticResource keypadButtonA11}">
        <Setter Property="BackgroundColor" Value="#59B03A"/>
      </Style>

      <Style x:Key="keypadButtonL11" TargetType="Image">
        <Setter Property="BackgroundColor" Value="#0FFAFAFA"/>
      </Style>
      <Style x:Key="keypadButtonL12" TargetType="Image" BasedOn="{StaticResource keypadButtonL11}">
      </Style>
      <Style x:Key="keypadButtonL13" TargetType="Image" BasedOn="{StaticResource keypadButtonL11}">
      </Style>
      <Style x:Key="keypadButtonL14" TargetType="Image" BasedOn="{StaticResource keypadButtonL11}">
      </Style>
      <Style x:Key="keypadButtonL15" TargetType="Image" BasedOn="{StaticResource keypadButtonL11}">
      </Style>
      <Style x:Key="keypadButtonL16" TargetType="Image" BasedOn="{StaticResource keypadButtonL11}">
      </Style>
    </ResourceDictionary>
  </ContentPage.Resources>

  <Grid HorizontalOptions="FillAndExpand"
          VerticalOptions="FillAndExpand"
          RowSpacing="0"
          ColumnSpacing="0">
    <Grid.RowDefinitions>
      <RowDefinition Height="42*" />
      <RowDefinition Height="58*" />
    </Grid.RowDefinitions>

    <StackLayout Grid.Row="0"
                   HorizontalOptions="FillAndExpand"
                   VerticalOptions="FillAndExpand"
                   Padding="16,16,16,16"
                   BackgroundColor="#FAFAFA" >

      <Grid HorizontalOptions="FillAndExpand"
            VerticalOptions="FillAndExpand"
            RowSpacing="0"
            ColumnSpacing="0">

        <Grid.RowDefinitions>
          <RowDefinition Height="644*" />
          <RowDefinition Height="18*" />
          <RowDefinition Height="170*" />
          <RowDefinition Height="82*" />
          <RowDefinition Height="86*" />
        </Grid.RowDefinitions>

        <Label Grid.Row="0" Style="{StaticResource inputString}"
               FormattedText="{Binding ExpressionText}"/>

        <BoxView Grid.Row="1" />

        <Label Grid.Row="2" Style="{StaticResource sumString}"
               TextColor="{Binding ResultColor, Converter={StaticResource StringToColorConverter}}"
               FormattedText="{Binding ResultText}" />

        <BoxView Grid.Row="3" />

        <control:CommandButton Grid.Row="4" Style="{StaticResource backButton}"
                               Source="btn_delete.png" Command="{Binding RemoveLast}"/>

      </Grid>
    </StackLayout>

    <Grid Grid.Row="1"
          HorizontalOptions="FillAndExpand"
          VerticalOptions="FillAndExpand"
          RowSpacing="0"
          ColumnSpacing="0">

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

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



      <control:ElementButton Grid.Row="0" Grid.Column="0" Style="{StaticResource keypadButtonA13}"
                              Source="calculator_button_p_01.png"
                              BackgroundColor="#2E000000"
                              Command="{Binding Clear}"
                              CommandParameter="x"/>
      <control:ElementButton Grid.Row="0" Grid.Column="1" Style="{StaticResource keypadButtonA12}"
                              Source="calculator_button_p_02.png"
                              BackgroundColor="#1F000000"
                              Command="{Binding PressButton}"
                              CommandParameter="("/>
      <control:ElementButton Grid.Row="0" Grid.Column="2" Style="{StaticResource keypadButtonA11}"
                              Source="calculator_button_p_03.png"
                              BackgroundColor="#0F000000"
                              Command="{Binding PressButton}"
                              CommandParameter="%"/>
      <control:ElementButton Grid.Row="0" Grid.Column="3" Style="{StaticResource keypadButtonA14}"
                              Source="calculator_button_p_04.png"
                              BackgroundColor="#5B000000"
                              Command="{Binding PressButton}"
                              CommandParameter="/"/>

      <control:ElementButton Grid.Row="1" Grid.Column="0" Style="{StaticResource keypadButtonL13}"
                              Source="calculator_button_p_number_7.png"
                              BackgroundColor="#2EFAFAFA"
                              BlendingPressedColor="#3DB9CC"
                              Command="{Binding PressButton}"
                              CommandParameter="7"/>
      <control:ElementButton Grid.Row="1" Grid.Column="1" Style="{StaticResource keypadButtonL14}"
                              Source="calculator_button_p_number_8.png"
                              BackgroundColor="#3DFAFAFA"
                              BlendingPressedColor="#3DB9CC"
                              Command="{Binding PressButton}"
                              CommandParameter="8"/>
      <control:ElementButton Grid.Row="1" Grid.Column="2" Style="{StaticResource keypadButtonL15}"
                              Source="calculator_button_p_number_9.png"
                              BackgroundColor="#4CFAFAFA"
                              BlendingPressedColor="#3DB9CC"
                              Command="{Binding PressButton}"
                              CommandParameter="9"/>
      <control:ElementButton Grid.Row="1" Grid.Column="3" Style="{StaticResource keypadButtonA11}"
                              Source="calculator_button_p_05.png"
                              BackgroundColor="#0F000000"
                              Command="{Binding PressButton}"
                              CommandParameter="*"/>

      <control:ElementButton Grid.Row="2" Grid.Column="0" Style="{StaticResource keypadButtonL12}"
                              Source="calculator_button_p_number_4.png"
                              BackgroundColor="#1FFAFAFA"
                              BlendingPressedColor="#3DB9CC"
                              Command="{Binding PressButton}"
                              CommandParameter="4"/>
      <control:ElementButton Grid.Row="2" Grid.Column="1" Style="{StaticResource keypadButtonL13}"
                              Source="calculator_button_p_number_5.png"
                              BackgroundColor="#2EFAFAFA"
                              BlendingPressedColor="#3DB9CC"
                              Command="{Binding PressButton}"
                              CommandParameter="5"/>
      <control:ElementButton Grid.Row="2" Grid.Column="2" Style="{StaticResource keypadButtonL14}"
                              Source="calculator_button_p_number_6.png"
                              BackgroundColor="#3DFAFAFA"
                              BlendingPressedColor="#3DB9CC"
                              Command="{Binding PressButton}"
                              CommandParameter="6"/>
      <control:ElementButton Grid.Row="2" Grid.Column="3" Style="{StaticResource keypadButtonA12}"
                              Source="calculator_button_p_06.png"
                              BackgroundColor="#1F000000"
                              Command="{Binding PressButton}"
                              CommandParameter="-"/>

      <control:ElementButton Grid.Row="3" Grid.Column="0" Style="{StaticResource keypadButtonL11}"
                              Source="calculator_button_p_number_1.png"
                              BackgroundColor="#0FFAFAFA"
                              BlendingPressedColor="#3DB9CC"
                              Command="{Binding PressButton}"
                              CommandParameter="1"/>
      <control:ElementButton Grid.Row="3" Grid.Column="1" Style="{StaticResource keypadButtonL12}"
                              Source="calculator_button_p_number_2.png"
                              BackgroundColor="#1FFAFAFA"
                              BlendingPressedColor="#3DB9CC"
                              Command="{Binding PressButton}"
                              CommandParameter="2"/>
      <control:ElementButton Grid.Row="3" Grid.Column="2" Style="{StaticResource keypadButtonL13}"
                              Source="calculator_button_p_number_3.png"
                              BackgroundColor="#2EFAFAFA"
                              BlendingPressedColor="#3DB9CC"
                              Command="{Binding PressButton}"
                              CommandParameter="3"/>
      <control:ElementButton Grid.Row="3" Grid.Column="3" Style="{StaticResource keypadButtonA13}"
                              Source="calculator_button_p_07.png"
                              BackgroundColor="#2E000000"
                              Command="{Binding PressButton}"
                              CommandParameter="+"/>

      <control:ElementButton Grid.Row="4" Grid.Column="0" Style="{StaticResource keypadButtonL16}"
                              Source="calculator_button_p_number_0.png"
                              BackgroundColor="#00FAFAFA"
                              BlendingPressedColor="#3DB9CC"
                              Command="{Binding PressButton}"
                              CommandParameter="0"/>
      <control:ElementButton Grid.Row="4" Grid.Column="1" Style="{StaticResource keypadButtonL11}"
                              Source="calculator_button_p_number_10.png"
                              BackgroundColor="#0FFAFAFA"
                              BlendingPressedColor="#3DB9CC"
                              Command="{Binding PressButton}"
                              CommandParameter="."/>
      <control:ElementButton Grid.Row="4" Grid.Column="2" Style="{StaticResource keypadButtonL12}"
                              Source="calculator_button_p_number_11.png"
                              BackgroundColor="#1FFAFAFA"
                              BlendingPressedColor="#3DB9CC"
                              Command="{Binding Reverse}"/>
      <control:ElementButton Grid.Row="4" Grid.Column="3" Style="{StaticResource keypadButtonB}"
                              Source="calculator_button_p_08.png"
                              BackgroundColor="#59B03A"
                              BlendingPressedColor="#3DB9CC"
                              Command="{Binding Equal}"/>
    </Grid>
  </Grid>
</ContentPage>