import 'package:flutter/material.dart';
import 'package:telemednet/screens/patientDashboard/registrationScreens/family_members_edit_screen.dart';

class FamilyMember {
  final String name;
  final String gender;
  final String dateOfBirth;
  final String relation;

  FamilyMember({
    required this.name,
    required this.gender,
    required this.dateOfBirth,
    required this.relation,
  });
}

class PatientFamilyMembersScreen extends StatefulWidget {
  const PatientFamilyMembersScreen({super.key});

  @override
  State<PatientFamilyMembersScreen> createState() =>
      _PatientFamilyMembersScreenState();
}

class _PatientFamilyMembersScreenState
    extends State<PatientFamilyMembersScreen> {
  List<FamilyMember> familyMembers = [
    FamilyMember(
      name: "Dhansh A S",
      gender: "Male",
      dateOfBirth: "18/12/2001",
      relation: "Father",
    ),
  ];

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text('Family members'),
        actions: [
          TextButton(
            onPressed: () {
              // Handle 'Done' action
            },
            child: const Text('Done', style: TextStyle(color: Colors.blue)),
          ),
        ],
      ),
      body: ListView.builder(
        itemCount: familyMembers.length,
        itemBuilder: (context, index) {
          return FamilyMemberCard(
            familyMember: familyMembers[index],
            onEdit: () => _editFamilyMember(index),
            onDelete: () => _deleteFamilyMember(index),
          );
        },
      ),
      floatingActionButton: FloatingActionButton(
        onPressed: _addFamilyMember,
        backgroundColor: Colors.blue,
        child: const Icon(Icons.add),
      ),
    );
  }

  void _addFamilyMember() {
    Navigator.push(
      context,
      MaterialPageRoute(builder: (context) => const FamilyMembersEditScreen()),
    ).then((newMember) {
      if (newMember != null) {
        setState(() {
          familyMembers.add(newMember);
        });
      }
    });
  }

  void _editFamilyMember(int index) {
    Navigator.push(
      context,
      MaterialPageRoute(
        builder: (context) =>
            FamilyMembersEditScreen(familyMember: familyMembers[index]),
      ),
    ).then((editedMember) {
      if (editedMember != null) {
        setState(() {
          familyMembers[index] = editedMember;
        });
      }
    });
  }

  void _deleteFamilyMember(int index) {
    setState(() {
      familyMembers.removeAt(index);
    });
  }
}

class FamilyMemberCard extends StatelessWidget {
  final FamilyMember familyMember;
  final VoidCallback onEdit;
  final VoidCallback onDelete;

  const FamilyMemberCard({
    super.key,
    required this.familyMember,
    required this.onEdit,
    required this.onDelete,
  });

  @override
  Widget build(BuildContext context) {
    return Card(
      margin: const EdgeInsets.symmetric(horizontal: 16, vertical: 8),
      child: Padding(
        padding: const EdgeInsets.all(16),
        child: Column(
          crossAxisAlignment: CrossAxisAlignment.start,
          children: [
            Text('Name: ${familyMember.name}',
                style: const TextStyle(fontWeight: FontWeight.bold)),
            const SizedBox(height: 4),
            Text('Gender: ${familyMember.gender}'),
            const SizedBox(height: 4),
            Text('Date of Birth: ${familyMember.dateOfBirth}'),
            const SizedBox(height: 4),
            Text('Relation: ${familyMember.relation}'),
            const SizedBox(height: 8),
            Row(
              mainAxisAlignment: MainAxisAlignment.end,
              children: [
                IconButton(
                  icon: const Icon(Icons.edit, color: Colors.blue),
                  onPressed: onEdit,
                ),
                IconButton(
                  icon: const Icon(Icons.delete, color: Colors.red),
                  onPressed: onDelete,
                ),
              ],
            ),
          ],
        ),
      ),
    );
  }
}
