import 'package:flutter/material.dart';
import 'package:telemednet/screens/patientScreens/registrationScreens/family_members_edit_screen.dart';
import 'package:telemednet/data/models/patient.dart';
import '../../../controller/patient_controller.dart';
import 'package:flutter_slidable/flutter_slidable.dart';

class PatientFamilyMembersScreen extends StatefulWidget {
  final PatientController controller;
  const PatientFamilyMembersScreen({
    super.key,
    required this.controller,
  });

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

class _PatientFamilyMembersScreenState
    extends State<PatientFamilyMembersScreen> {
  bool isLoading = false;
  final int maxFamilyMembers = 5;

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text(
          'Family Members',
          style: TextStyle(fontSize: 20),
        ),
        leading: IconButton(
          icon: const Icon(Icons.arrow_back, color: Colors.black),
          onPressed: () {
            if (_validateFamilyMembers()) {
              Navigator.pop(context);
            }
          },
        ),
        actions: _buildAppBarActions(),
        elevation: 0,
      ),
      body: Padding(
        padding: const EdgeInsets.all(16.0),
        child: Column(
          children: [
            const SizedBox(height: 8),
            Expanded(
              child: ListView.builder(
                itemCount: widget.controller.model.familyMembers.length,
                itemBuilder: (context, index) {
                  return FamilyMemberCard(
                    familyMember: widget.controller.model.familyMembers[index],
                    onEdit: () => _editFamilyMember(index),
                    onDelete: () => _deleteFamilyMember(index),
                  );
                },
              ),
            ),
          ],
        ),
      ),
      floatingActionButton: FloatingActionButton(
        onPressed: _addFamilyMember,
        backgroundColor: Colors.blue,
        child: const Icon(Icons.add, color: Colors.white),
      ),
    );
  }

  List<Widget> _buildAppBarActions() {
    return [
      TextButton(
        onPressed: () {
          if (_validateFamilyMembers()) {
            Navigator.pop(context);
          }
        },
        child: const Text(
          'Done',
          style: TextStyle(color: Colors.blue, fontWeight: FontWeight.bold),
        ),
      ),
    ];
  }

  bool _validateFamilyMembers() {
    if (widget.controller.model.familyMembers.isEmpty) {
      _showValidationError('Please add at least one family member');
      return false;
    }

    final relations = widget.controller.model.familyMembers
        .map((member) => member.relation?.toLowerCase())
        .toList();
    if (relations.toSet().length != relations.length) {
      _showValidationError('Duplicate relations are not allowed');
      return false;
    }

    return true;
  }

  void _showValidationError(String message) {
    showDialog(
      context: context,
      builder: (context) => AlertDialog(
        title: const Row(
          children: [
            Icon(Icons.error_outline, color: Colors.red),
            SizedBox(width: 8),
            Text('Validation Error'),
          ],
        ),
        content: Text(message),
        actions: [
          TextButton(
            onPressed: () => Navigator.pop(context),
            child: const Text('OK'),
          ),
        ],
      ),
    );
  }

  void _addFamilyMember() {
    if (widget.controller.model.familyMembers.length >= maxFamilyMembers) {
      _showValidationError('Maximum $maxFamilyMembers family members allowed');
      return;
    }

    Navigator.push(
      context,
      MaterialPageRoute(
        builder: (context) => FamilyMembersEditScreen(
          controller: widget.controller,
        ),
      ),
    ).then((newMember) {
      if (newMember != null) {
        setState(() {
          widget.controller.addFamilyMember(newMember);
        });
      }
    });
  }

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

  void _deleteFamilyMember(int index) {
    if (widget.controller.model.familyMembers.length <= 1) {
      _showValidationError('At least one family member is required');
      return;
    }

    showDialog(
      context: context,
      builder: (context) => AlertDialog(
        title: const Text('Delete Family Member'),
        content:
            const Text('Are you sure you want to delete this family member?'),
        actions: [
          TextButton(
            onPressed: () => Navigator.pop(context),
            child: const Text('Cancel'),
          ),
          TextButton(
            onPressed: () {
              setState(() {
                widget.controller.deleteFamilyMember(index);
              });
              Navigator.pop(context);
            },
            child: const Text('Delete', style: TextStyle(color: Colors.red)),
          ),
        ],
      ),
    );
  }
}

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,
  });

  Widget _buildInfoRow(IconData icon, String label, String? value) {
    return Padding(
      padding: const EdgeInsets.symmetric(vertical: 2.0),
      child: Row(
        crossAxisAlignment: CrossAxisAlignment.start,
        children: [
          Icon(icon, size: 20, color: Colors.blueGrey),
          const SizedBox(width: 6),
          Text(
            label,
            style: const TextStyle(
              fontSize: 15,
              fontWeight: FontWeight.w500,
              color: Colors.blueGrey,
            ),
          ),
          const SizedBox(width: 6),
          Expanded(
            child: Text(
              value ?? 'Not provided',
              style: TextStyle(
                fontWeight: FontWeight.bold,
                fontSize: 15,
                color: value == null || value.isEmpty
                    ? Colors.redAccent
                    : Colors.black87,
                fontStyle: value == null || value.isEmpty
                    ? FontStyle.italic
                    : FontStyle.normal,
              ),
            ),
          ),
        ],
      ),
    );
  }

  @override
  Widget build(BuildContext context) {
    return Slidable(
      key: ValueKey(familyMember),
      endActionPane: ActionPane(
        motion: const ScrollMotion(),
        extentRatio: 0.3,
        children: [
          SlidableAction(
            onPressed: (context) => onEdit(),
            foregroundColor: Colors.blue,
            icon: Icons.edit,
            padding: EdgeInsets.zero,
            spacing: 0,
          ),
          SlidableAction(
            onPressed: (context) => onDelete(),
            foregroundColor: Colors.red,
            icon: Icons.delete,
            padding: EdgeInsets.zero,
            spacing: 0,
          ),
        ],
      ),
      child: Card(
        elevation: 4,
        shape: RoundedRectangleBorder(
          borderRadius: BorderRadius.circular(12),
        ),
        child: Container(
          decoration: BoxDecoration(
            borderRadius: BorderRadius.circular(12),
            color: Colors.blueGrey[50],
          ),
          child: Padding(
            padding: const EdgeInsets.all(16),
            child: Column(
              crossAxisAlignment: CrossAxisAlignment.start,
              mainAxisSize: MainAxisSize.min,
              children: [
                _buildInfoRow(Icons.person, 'Name:', familyMember.name),
                const SizedBox(height: 10),
                _buildInfoRow(
                    Icons.transgender, 'Gender:', familyMember.gender),
                const SizedBox(height: 10),
                _buildInfoRow(
                  Icons.cake,
                  'Date of Birth:',
                  familyMember.dateOfBirth?.toString().split(' ')[0] ??
                      'Not provided',
                ),
                const SizedBox(height: 10),
                _buildInfoRow(
                    Icons.family_restroom, 'Relation:', familyMember.relation),
              ],
            ),
          ),
        ),
      ),
    );
  }
}
