import 'package:flutter/material.dart';
import 'package:connectivity_plus/connectivity_plus.dart';

class CheckConnectionPage extends StatefulWidget {
  final Widget redirectPage;

  const CheckConnectionPage({Key? key, required this.redirectPage})
      : super(key: key);

  @override
  _CheckConnectionPageState createState() => _CheckConnectionPageState();
}

class _CheckConnectionPageState extends State<CheckConnectionPage> {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text("Mayday  ..."),
        backgroundColor:
            Colors.red, // Définir la couleur de fond de l'appbar en rouge
      ),
      body: SafeArea(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: [
            Image.asset(
              'assets/images/no_connection.png',
              width: 250,
            ),
            SizedBox(height: 32),
            Column(
              mainAxisAlignment: MainAxisAlignment.center,
              children: [
                Text(
                  "No internet connection",
                  style: TextStyle(
                    fontWeight: FontWeight.bold,
                    fontSize: 18,
                  ),
                ),
                SizedBox(height: 10),
                Center(
                  child: Text(
                    "You are not connected. Check your connection.",
                    style: TextStyle(fontSize: 14),
                    textAlign: TextAlign.center,
                  ),
                ),
              ],
            ),
            SizedBox(height: 32),
            ElevatedButton(
              style: ElevatedButton.styleFrom(
                backgroundColor: Colors.red,
              ),
              child: const Text("Check Connection"),
              onPressed: () async {
                final connectivityResult =
                    await Connectivity().checkConnectivity();
                if (connectivityResult == ConnectivityResult.none) {
                  await showDialog(
                    barrierDismissible: false,
                    context: context,
                    builder: (_) => NetworkErrorDialog(
                      onRetryPressed: () async {
                        final connectivityResult =
                            await Connectivity().checkConnectivity();
                        if (connectivityResult == ConnectivityResult.none) {
                          ScaffoldMessenger.of(context).showSnackBar(
                              const SnackBar(
                                  content: Text(
                                      'Please turn on your wifi or mobile data')));
                        } else {
                          Navigator.pop(context);
                          // redirect to UploadPhotosPage here
                        }
                      },
                    ),
                  );
                } else {
                  // redirect to UploadPhotosPage here
                }
              },
            ),
          ],
        ),
      ),
    );
  }
}

class NetworkErrorDialog extends StatelessWidget {
  const NetworkErrorDialog({Key? key, this.onRetryPressed}) : super(key: key);

  final VoidCallback? onRetryPressed;

  @override
  Widget build(BuildContext context) {
    return AlertDialog(
      title: const Text('No Internet Connection'),
      content: Column(
        mainAxisSize: MainAxisSize.min,
        children: [
          const Icon(Icons.error, color: Colors.red, size: 48),
          const SizedBox(height: 16),
          const Text(
            'Please check your internet connection and try again.',
            textAlign: TextAlign.center,
          ),
        ],
      ),
      actions: [
        TextButton(
          onPressed: () => Navigator.of(context).pop(),
          child: const Text('Go Back'),
        ),
        TextButton(
          onPressed: onRetryPressed,
          child: const Text('Retry'),
        ),
      ],
    );
  }
}
