using System.Collections.ObjectModel;
using System.Windows.Input;
using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.Input;

namespace NFCLockDemoV2.ViewModels;

public class MainViewModel : ObservableObject
{
    public Action? OperationTask;
    
    private ViewModels.OperationType? _operation;
    public ViewModels.OperationType? Operation
    {
        get => _operation;
        set => SetProperty(ref _operation, value);
    }

    private int _arcProgress;
    public int ArcProgress
    {
        get => _arcProgress;
        set => SetProperty(ref _arcProgress, value);
    }
    
    private string _deviceId = "--";
    public string DeviceId
    {
        get => _deviceId;
        set
        {
            if (SetProperty(ref _deviceId, value))
            {
                AddDevice(value);
            }
        }
    }

    private string _deviceStatus = "";
    public string DeviceStatus
    {
        get => _deviceStatus;
        set => SetProperty(ref _deviceStatus, value);
    }

    private string _deviceRssi = "--";
    public string DeviceRssi
    {
        get => _deviceRssi;
        set => SetProperty(ref _deviceRssi, value);
    }

    private string _password = "123456";
    public string Password
    {
        get => _password;
        set => SetProperty(ref _password, value);
    }

    private string _operationElapsed = "--";
    public string OperationElapsed
    {
        get => _operationElapsed;
        set => SetProperty(ref _operationElapsed, value);
    }
    
    public ICommand ControlCommand { get; set; }
    public ICommand CopyCommand { get; set; }
    
    public void UpdateMessage(string message)
    {
        DeviceStatus = message;
    }
    
    public ObservableCollection<string> Devices { get; set; }

    private void AddDevice(string deviceId)
    {
        if (deviceId != "--" && !Devices.Contains(deviceId))
        {
             Devices.Add(deviceId);
        }
    }

    private async Task CopyToDeviceClipboardAsync(string text)
    {
        await Clipboard.SetTextAsync(text);
        await Application.Current.MainPage.DisplayAlert("提示", $"已复制：{text}", "确定");
    }

    public MainViewModel()
    {
        Devices = new ObservableCollection<string>();
        ControlCommand = new AsyncRelayCommand<string>(val =>
        {
            OperationElapsed = "--";
            switch (val)
            {
                case "Lock":
                    Operation = ViewModels.OperationType.LOCK;
                    break;
                case "Unlock":
                    Operation = ViewModels.OperationType.UNLOCK;
                    break;
                case "SetPassword":
                    Operation = ViewModels.OperationType.SET_PASSWORD;
                    break;
                default:
                    Operation = null;
                    break;
            }
            OperationTask?.Invoke();
            return Task.CompletedTask;
        });
        
        CopyCommand = new AsyncRelayCommand<string>(async (text) =>
        {
            if (!string.IsNullOrEmpty(text))
            {
                await CopyToDeviceClipboardAsync(text);
            }
        });
    }
}