using Justice.Helpers; using Justice.Models; using Justice.Services; using System; using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; namespace Justice.Views { public partial class SosPage : ContentPage { private readonly DatabaseHelper _databaseHelper; private readonly SosService _sosService; public SosPage() { InitializeComponent(); _databaseHelper = new DatabaseHelper(); _sosService = new SosService(); } private async void OnSendSosClicked(object sender, EventArgs e) { try { // Get the selected group var selectedGroup = GroupPicker.SelectedItem as string; if (string.IsNullOrEmpty(selectedGroup)) { await DisplayAlert("Error", "Please select a group.", "OK"); return; } // Fetch emergency contacts from the selected group var contacts = await _databaseHelper.GetAllAsync(); var groupContacts = contacts .Where(contact => contact.Group.Equals(selectedGroup, StringComparison.OrdinalIgnoreCase)) .ToList(); if (!groupContacts.Any()) { await DisplayAlert("Error", $"No contacts found in the group '{selectedGroup}'.", "OK"); return; } // Extract mobile numbers for the SOS alert var emergencyNumbers = groupContacts.Select(contact => contact.MobileNumber).ToList(); // Send the SOS alert await _sosService.SendSosAlertAsync(emergencyNumbers); } catch (Exception ex) { await DisplayAlert("Error", $"Failed to send SOS Alert: {ex.Message}", "OK"); } } } }