using Android.App;
using Android.Content;
using Android.Content.PM;
using Android.Nfc;
using Android.OS;
using Com.Lvcheng.Lock.Shared.Nfc.Kmp;
using NFCLockDemoV2;
using NFCLockDemoV2.ViewModels;

namespace NFCLockAppV2;

[Activity(Theme = "@style/Maui.SplashTheme", MainLauncher = true, LaunchMode = LaunchMode.SingleTop, ConfigurationChanges = ConfigChanges.ScreenSize | ConfigChanges.Orientation | ConfigChanges.UiMode | ConfigChanges.ScreenLayout | ConfigChanges.SmallestScreenSize | ConfigChanges.Density, Exported = true)]
[IntentFilter(new[] { Intent.ActionMain }, Categories = new[] { Intent.CategoryLauncher })]
[IntentFilter(new[] { NfcAdapter.ActionNdefDiscovered, NfcAdapter.ActionTechDiscovered, NfcAdapter.ActionTagDiscovered }, Categories = new[] { Intent.CategoryDefault }, DataMimeType = "text/plain")]
public class MainActivity : MauiAppCompatActivity
{
    private AndroidNFCHelper nfcHelper;
    private MainViewModel _mainViewModel;
    protected override void OnCreate(Bundle savedInstanceState)
    {
        base.OnCreate(savedInstanceState);
        _mainViewModel = Microsoft.Maui.Controls.Application.Current.Handler.MauiContext.Services.GetService<MainViewModel>();
        nfcHelper = new AndroidNFCHelper(this, new AndroidNfcCallBack(_mainViewModel));
        _mainViewModel.OperationTask = () =>
        {
            switch (_mainViewModel.Operation)
            {
                case OperationType.LOCK:
                    nfcHelper.Lock(_mainViewModel.Password);
                    break;
                case OperationType.UNLOCK:
                    nfcHelper.Unlock(_mainViewModel.Password);
                    break;
                default:
                    break;
            }
        };
    }
    
    protected override void OnResume()
    {
        base.OnResume();
        nfcHelper.OnResume();
    }
    
    protected override void OnPause()
    {
        base.OnPause();
        nfcHelper.OnPause();
    }
}