import 'package:flutter/material.dart';
import 'package:google_fonts/google_fonts.dart';
import 'package:p_tits_pas/models/m_dashbord/child_model.dart';
import 'package:p_tits_pas/services/bug_report_service.dart';

class AppFooter extends StatelessWidget {
  const AppFooter({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return Container(
      padding: const EdgeInsets.symmetric(horizontal: 24, vertical: 16),
      decoration: BoxDecoration(
        // color: Colors.white,
        border: Border(
          top: BorderSide(color: Colors.grey.shade300),
        ),
      ),
      child: LayoutBuilder(
        builder: (context, constraints) {
          if (constraints.maxWidth < 768) {
            return _buildMobileFooter(context);
          } else {
            return _buildDesktopFooter(context);
          }
        },
      ),
    );
  }

  Widget _buildDesktopFooter(BuildContext context) {
    return Row(
      mainAxisAlignment: MainAxisAlignment.center,
      children: [
        _buildFooterLink(context, 'Contact support', () => _handleContactSupport(context)),
        SizedBox(width: MediaQuery.of(context).size.width * 0.1),
        // _buildFooterDivider(),
        _buildFooterLink(context, 'Signaler un bug', () => _handleReportBug(context)),
        SizedBox(width: MediaQuery.of(context).size.width * 0.1),
        // _buildFooterDivider(),
        _buildFooterLink(context, 'Mentions légales', () => _handleLegalNotices(context)),
        // _buildFooterDivider(),
        SizedBox(width: MediaQuery.of(context).size.width * 0.1),
        _buildFooterLink(context, 'Politique de confidentialité', () => _handlePrivacyPolicy(context)),
      ],
    );
  }

  Widget _buildMobileFooter(BuildContext context) {
    return PopupMenuButton<String>(
      child: Container(
        padding: const EdgeInsets.symmetric(horizontal: 16, vertical: 8),
        decoration: BoxDecoration(
          border: Border.all(color: Colors.grey.shade300),
          borderRadius: BorderRadius.circular(8),
        ),
        child: Row(
          mainAxisSize: MainAxisSize.min,
          children: const [
            Icon(Icons.info_outline, size: 20),
            SizedBox(width: 8),
            Text('Informations'),
            Icon(Icons.keyboard_arrow_down),
          ],
        ),
      ),
      itemBuilder: (context) => [
        const PopupMenuItem(value: 'support', child: Text('Contact support')),
        const PopupMenuItem(value: 'bug', child: Text('Signaler un bug')),
        const PopupMenuItem(value: 'legal', child: Text('Mentions légales')),
        const PopupMenuItem(value: 'privacy', child: Text('Politique de confidentialité')),
      ],
      onSelected: (value) {
        switch (value) {
          case 'support':
            _handleContactSupport(context);
            break;
          case 'bug':
            _handleReportBug(context);
            break;
          case 'legal':
            _handleLegalNotices(context);
            break;
          case 'privacy':
            _handlePrivacyPolicy(context);
            break;
        }
      },
    );
  }

  Widget _buildFooterLink(BuildContext context, String text, VoidCallback onTap) {
    return InkWell(
      onTap: onTap,
      child: Text(
        text,
        style: TextStyle(
          color: Theme.of(context).primaryColor,
        ),
      ),
    );
  }

  void _handleReportBug(BuildContext context) {
    final TextEditingController controller = TextEditingController();

    showDialog(
      context: context,
      builder: (context) => AlertDialog(
        title: Text(
          'Signaler un bug',
          style: GoogleFonts.merienda(),
        ),
        content: Column(
          mainAxisSize: MainAxisSize.min,
          children: [
            TextField(
              controller: controller,
              maxLines: 5,
              decoration: InputDecoration(
                hintText: 'Décrivez le problème rencontré...',
                border: OutlineInputBorder(
                  borderRadius: BorderRadius.circular(10),
                ),
              ),
            ),
          ],
        ),
        actions: [
          TextButton(
            onPressed: () => Navigator.pop(context),
            child: Text(
              'Annuler',
              style: GoogleFonts.merienda(),
            ),
          ),
          TextButton(
            onPressed: () async {
              if (controller.text.trim().isEmpty) {
                ScaffoldMessenger.of(context).showSnackBar(
                  SnackBar(
                    content: Text(
                      'Veuillez décrire le problème',
                      style: GoogleFonts.merienda(),
                    ),
                  ),
                );
                return;
              }

              try {
                await BugReportService.sendReport(controller.text);
                if (context.mounted) {
                  Navigator.pop(context);
                  ScaffoldMessenger.of(context).showSnackBar(
                    SnackBar(
                      content: Text(
                        'Rapport envoyé avec succès',
                        style: GoogleFonts.merienda(),
                      ),
                    ),
                  );
                }
              } catch (e) {
                if (context.mounted) {
                  ScaffoldMessenger.of(context).showSnackBar(
                    SnackBar(
                      content: Text(
                        'Erreur lors de l\'envoi du rapport',
                        style: GoogleFonts.merienda(),
                      ),
                    ),
                  );
                }
              }
            },
            child: Text(
              'Envoyer',
              style: GoogleFonts.merienda(),
            ),
          ),
        ],
      ),
    );
  }

  void _handleLegalNotices(BuildContext context) {
    // Handle legal notices action
    Navigator.pushNamed(context, '/legal');
  }

  void _handlePrivacyPolicy(BuildContext context) {
    // Handle privacy policy action
    Navigator.pushNamed(context, '/privacy');
  }

  void _handleContactSupport(BuildContext context) {
    // Handle contact support action
    // Navigator.pushNamed(context, '/support');
  }

  Widget _buildFooterDivider() {
    return Divider(
      color: Colors.grey[300],
      thickness: 1,
      height: 40,
    );
  }
}