<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             x:Class="Borepin.Page.MachinePage"
             xmlns:converters="clr-namespace:Borepin.Converter"
             xmlns:resource_text="clr-namespace:Borepin.Resources.Text" 
             xmlns:views="clr-namespace:Borepin.View"
             Title="{x:Static resource_text:TextResource.TITLE_Machine}">
    <ContentPage.Resources>
        <ResourceDictionary>
            <converters:AllTrueBoolConverter x:Key="AllTrueBoolConverter"/>
            <converters:InvertBoolConverter x:Key="InvertBoolConverter"/>
            <converters:MachineStateColorConverter x:Key="MachineStateColorConverter"/>
            <converters:MachineStateStringConverter x:Key="MachineStateStringConverter"/>
            <converters:IsNotNullBoolConverter x:Key="IsNotNullBoolConverter"/>
        </ResourceDictionary>
    </ContentPage.Resources>
    <NavigationPage.TitleView>
        <Label Text="{Binding MachineItem.State, Converter={StaticResource MachineStateStringConverter}}" FontAttributes="Bold" HorizontalOptions="End" HorizontalTextAlignment="Center" VerticalTextAlignment="Center" WidthRequest="150" Margin="7.5" VerticalOptions="FillAndExpand" FontSize="Small" BackgroundColor="{Binding MachineItem.State, Converter={StaticResource MachineStateColorConverter}}">
            <Label.IsVisible>
                <MultiBinding Converter="{StaticResource AllTrueBoolConverter}">
                    <Binding Path="IsBusy" Converter="{StaticResource InvertBoolConverter}"/>
                    <Binding Path="IsConnected" />
                </MultiBinding>
            </Label.IsVisible>
        </Label>
    </NavigationPage.TitleView>
    <ContentPage.Content>
        <ScrollView>
            <StackLayout Padding="20">
                <views:ConnectionStateView/>
                <StackLayout>
                    <StackLayout.IsVisible>
                        <MultiBinding Converter="{StaticResource AllTrueBoolConverter}">
                            <Binding Path="IsBusy" Converter="{StaticResource InvertBoolConverter}"/>
                            <Binding Path="IsConnected" />
                        </MultiBinding>
                    </StackLayout.IsVisible>
                    <Label Text="{Binding MachineItem.Name}" Style="{StaticResource LabelStyle_Title}"/>
                    <Label Text="{Binding MachineItem.Description}" Style="{StaticResource Style_Label_Text_Center}"/>
                    <StackLayout Orientation="Horizontal" IsVisible="{Binding MachineItem.CurrentUser, Converter={StaticResource IsNotNullBoolConverter}}">
                        <Label Text="{x:Static resource_text:TextResource.MachinePage_CurrentUser}" Style="{StaticResource Style_Label_Property_Title}"/>
                        <Label Text="{Binding MachineItem.CurrentUser.Username}" Style="{StaticResource Style_Label_Property_Text}"/>
                    </StackLayout>

                    <Button Text="{x:Static resource_text:TextResource.MachinePage_Use}" IsVisible="{Binding MachineItem.CanUse}" Command="{Binding UseMachineCommand}" Style="{StaticResource Style_Button_Primary}"/>
                    <Label Text="{x:Static resource_text:TextResource.MachinePage_CanNotUseByPermission}" IsVisible="{Binding MachineItem.CanNotUseByPermission}" Style="{StaticResource Style_Label_Text_Center}"/>
                    <Button Text="{x:Static resource_text:TextResource.MachinePage_GiveBack}" IsVisible="{Binding MachineItem.CanInUse}" Command="{Binding GiveBackMachineCommand}" Style="{StaticResource Style_Button_Primary}"/>

                    <Button Text="{x:Static resource_text:TextResource.MachinePage_Unlock}" Command="{Binding UnlockLockCommand}" Style="{StaticResource Style_Button_Primary}">
                        <Button.IsVisible>
                            <MultiBinding Converter="{StaticResource AllTrueBoolConverter}">
                                <Binding Path="MachineItem.CanInUse" Converter="{StaticResource InvertBoolConverter}"/>
                                <Binding Path="MachineItem.IsLock" />
                            </MultiBinding>
                        </Button.IsVisible>
                    </Button>
                    <Button Text="{x:Static resource_text:TextResource.MachinePage_Identify}" Command="{Binding IdentifyLockCommand}" Style="{StaticResource Style_Button_Primary}">
                        <Button.IsVisible>
                            <MultiBinding Converter="{StaticResource AllTrueBoolConverter}">
                                <Binding Path="MachineItem.CanInUse" Converter="{StaticResource InvertBoolConverter}"/>
                                <Binding Path="MachineItem.IsLock" />
                            </MultiBinding>
                        </Button.IsVisible>
                    </Button>


                    <Button VerticalOptions="End" Text="{x:Static resource_text:TextResource.MachinePage_OpenWiki}" IsVisible="{Binding MachineItem.Wiki, Converter={StaticResource IsNotNullBoolConverter}}" Command="{Binding OpenWikiCommand}" Style="{StaticResource Style_Button_Primary}"/>
                    <StackLayout Grid.Row="2" VerticalOptions="End" IsVisible="{Binding MachineItem.CanManage}">
                        <Label Text="{x:Static resource_text:TextResource.MachinePage_ManageMachine}" Style="{StaticResource Style_Label_Property_Title}"/>
                        <StackLayout Margin="10, 0, 0, 20" Orientation="Horizontal" IsVisible="{Binding MachineItem.LastUser, Converter={StaticResource IsNotNullBoolConverter}}">
                            <Label Text="{x:Static resource_text:TextResource.MachinePage_LastUser}" Style="{StaticResource Style_Label_Property_Title}"/>
                            <Label Text="{Binding MachineItem.LastUser.Username}" Style="{StaticResource Style_Label_Property_Text}" Margin="10, 0, 0, 0"/>
                        </StackLayout>
                        <Button Text="{x:Static resource_text:TextResource.MachinePage_ForceFree}" Command="{Binding ForceFreeMachineCommand}" Style="{StaticResource Style_Button_Primary}"/>
                        <Button Text="{x:Static resource_text:TextResource.MachinePage_ForceBlock}" Command="{Binding ForceBlockMachineCommand}" Style="{StaticResource Style_Button_Admin}"/>
                        <Button Text="{x:Static resource_text:TextResource.MachinePage_ForceDisable}" Command="{Binding ForceDisableMachineCommand}" Style="{StaticResource Style_Button_Admin}"/>
                    </StackLayout>
                </StackLayout>
            </StackLayout>
        </ScrollView>
    </ContentPage.Content>
</ContentPage>