using NFCLockDemoV2.ViewModels;

#if IOS && !MACCATALYST
using Foundation;
using NFCManager = NfcLock.Ios.Binding.NFCManager;
#endif

namespace NFCLockDemoV2;

public partial class MainPage : ContentPage
{
    private MainViewModel _mainMainViewModel;
    
#if IOS && !MACCATALYST
    private void SetNFCCallbacks()
    {
        NFCManager.Shared.SetDeviceDiscoveredCallback((NSString id, int isNew, int rssi) => {
            _mainMainViewModel.DeviceId = id;
            _mainMainViewModel.DeviceRssi = rssi.ToString();
            NFCManager.Shared.ShowTipsWith("Device detected");
        });
        NFCManager.Shared.SetOperationFinishedCallback(() =>
        {
            _mainMainViewModel.DeviceId = "--";
            _mainMainViewModel.DeviceRssi = "--";
            _mainMainViewModel.DeviceStatus = "NFC操作完成";
            _mainMainViewModel.Operation = null;
            _mainMainViewModel.ArcProgress = 0;
        });

        NFCManager.Shared.SetChargingCallback((NSString res, int level) =>
        {
            if (res == "OK")
            {
                _mainMainViewModel.UpdateMessage($"充电中({level}%)");
                _mainMainViewModel.ArcProgress = level;
                NFCManager.Shared.ShowTipsWith($"Charing ({level})%)");
            }
            else if (res == "UNAUTHORIZED")
            {
                NFCManager.Shared.EndScanWith("验证失败");
            }
            else
            {
                NFCManager.Shared.EndScanWith("充电失败");
            }
        });

        NFCManager.Shared.SetControllingCallback((NSString res, int progress, NSString ctrl) =>
        {
            System.Diagnostics.Debug.WriteLine($"res: {res}, progress: {progress}, ctrl: {ctrl}");

            if (res == "OK")
            {
                if (progress >= 100)
                {
                    string operation = _mainMainViewModel.Operation == OperationType.UNLOCK ? "Unlocked" : "Locked";
                    NFCManager.Shared.ShowTipsWith($"{operation}");
                    NFCManager.Shared.EndScan();
                }
                else
                {
                    string operation = _mainMainViewModel.Operation == OperationType.UNLOCK ? "Unlocking" : "Locking";
                    _mainMainViewModel.UpdateMessage($"{operation} ({progress}%)");
                    NFCManager.Shared.ShowTipsWith($"{operation} ({progress}%)");
                }
            }
            else if (res == "UNAUTHORIZED")
            {
                NFCManager.Shared.EndScanWith("验证失败");
            }
            else
            {
                NFCManager.Shared.EndScanWith("充电失败");
            }
        });
    }
#endif
    
    public MainPage(MainViewModel mainViewModel)
    {
        InitializeComponent();
        _mainMainViewModel = mainViewModel;
        BindingContext = _mainMainViewModel;
    }

    protected override void OnAppearing()
    {
        base.OnAppearing();
#if IOS && !MACCATALYST
        SetNFCCallbacks();
#endif
    }
    
    private void LockClicked(object? sender, EventArgs e)
    {
#if IOS && !MACCATALYST
        
        NFCManager.Shared.LockWith(_mainMainViewModel.Password);
        NFCManager.Shared.StartScanWith("Hold your iPhone near the device");
#endif
    }

    private void UnlockClicked(object? sender, EventArgs e)
    {
#if IOS && !MACCATALYST
        NFCManager.Shared.UnlockWith(_mainMainViewModel.Password);
        NFCManager.Shared.StartScanWith("Hold your iPhone near the device");
#endif
    }
}