<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             x:Class="NFCLockDemoV2.MainPage"
             xmlns:viewModels="clr-namespace:NFCLockDemoV2.ViewModels"
             xmlns:converter="clr-namespace:NFCLockDemoV2.Converters"
             Shell.TabBarIsVisible="False"
             Shell.NavBarIsVisible="False"
             x:DataType="viewModels:MainViewModel">
    <!--<ContentPage.BindingContext>
        <viewModels:MainViewModel></viewModels:MainViewModel>
    </ContentPage.BindingContext>-->
    
    <ContentPage.Resources>
        <converter:PercentageConverter x:Key="PercentageConverter"></converter:PercentageConverter>
        <converter:OperationColorConverter x:Key="OperationColorConverter"></converter:OperationColorConverter>
    </ContentPage.Resources>

    <VerticalStackLayout
            Padding="16"
            Spacing="16">
        
            <!-- 圆形进度指示器和状态 -->
            <Grid RowDefinitions="Auto,Auto"
                HorizontalOptions="Center"
                Margin="0,200,0,0"
                >
                <ProgressBar 
                    Progress="{Binding ArcProgress, Converter={StaticResource PercentageConverter}}"
                    WidthRequest="200"
                    ProgressColor="#00BCD4"
                    HorizontalOptions="Center"
                    VerticalOptions="Center" />
                <Label Grid.Row="1"
                       Text="{Binding DeviceStatus}"
                       FontSize="18"
                       FontAttributes="Bold"
                       HorizontalOptions="Center"
                       VerticalOptions="Center"
                       HorizontalTextAlignment="Center" />
                <Label Grid.Row="1"
                       Text="{Binding OperationElapsed, StringFormat='耗时: {0}'}"
                       FontSize="12"
                       TextColor="Gray"
                       HorizontalOptions="Center"
                       VerticalOptions="End"
                       HorizontalTextAlignment="Center"
                       Margin="0,0,0,-20" />
            </Grid>

            <!-- 设备信息 -->
            <Label
                Text="{Binding DeviceId, StringFormat='设备 ID: {0}'}"
                FontSize="16"
                HorizontalOptions="Center" />
            <Label
                Text="{Binding DeviceRssi, StringFormat='RSSI: {0}'}"
                FontSize="16"
                HorizontalOptions="Center" />

            <!-- 密码输入框 -->
            <Entry
                Text="{Binding Password}"
                Placeholder="输入密码"
                Keyboard="Numeric"
                MaxLength="6"
                HorizontalOptions="Fill" />

            <!-- 开锁按钮 -->
            <Button
                Text="开锁"
                Command="{Binding ControlCommand}"
                CommandParameter="Unlock"
                BackgroundColor="{Binding Operation, Converter={StaticResource OperationColorConverter}, ConverterParameter=UNLOCK}"
                TextColor="Black"
                Clicked="UnlockClicked"
                HorizontalOptions="Fill" />

            <!-- 关锁按钮 -->
            <Button
                Text="关锁"
                Command="{Binding ControlCommand}"
                CommandParameter="Lock"
                BackgroundColor="{Binding Operation, Converter={StaticResource OperationColorConverter}, ConverterParameter=LOCK}"
                TextColor="Black"
                Clicked="LockClicked"
                HorizontalOptions="Fill" />

            <!--~1~ 设置密码按钮 @1@
            <Button
                Text="设置密码"
                Command=""
                BackgroundColor="{Binding Operation, Converter={StaticResource OperationColorConverter}, ConverterParameter=SET_PASSWORD}"
                TextColor="Black"
                HorizontalOptions="Fill" />-->
            
            <CollectionView ItemsSource="{Binding Devices}" 
                            HorizontalOptions="Fill"
                            HeightRequest="100">
                <CollectionView.ItemTemplate>
                    <DataTemplate x:DataType="x:String">
                        <Grid>
                            <Label Text="{Binding .}" TextDecorations="Underline" TextColor="Aqua">
                                <Label.GestureRecognizers>
                                    <TapGestureRecognizer Command="{Binding Source={RelativeSource AncestorType={x:Type viewModels:MainViewModel}}, Path=CopyCommand}" CommandParameter="{Binding .}"></TapGestureRecognizer>
                                </Label.GestureRecognizers>
                            </Label>
                        </Grid>
                    </DataTemplate>
                </CollectionView.ItemTemplate>
            </CollectionView>
            
        </VerticalStackLayout>

</ContentPage>
