import 'package:flutter/material.dart';
import 'package:nearle/views/helpers/constants/Colorconstants.dart';
import 'package:nearle/views/helpers/constants/Font_constant.dart';
import 'package:get/get.dart';
import 'package:connectivity_plus/connectivity_plus.dart';
import 'package:nearle/views/introscreens/splashscreen.dart';

class OfflinePage extends StatelessWidget {
  const OfflinePage({super.key});

  Future<void> _handleRetry(BuildContext context) async {
    try {
      final results = await Connectivity().checkConnectivity();
      final isOnline = results.isNotEmpty && results.any((r) => r != ConnectivityResult.none);
      if (isOnline) {
        // Return to normal app flow; Splashscreen decides login vs home
        Get.offAll(() => Splashscreen());
        return;
      }
      ScaffoldMessenger.of(context).showSnackBar(
        SnackBar(
          content: const Text('Still offline. Please check your internet connection.'),
          backgroundColor: Colors.black87,
          behavior: SnackBarBehavior.floating,
        ),
      );
    } catch (_) {
      if (Navigator.of(context).canPop()) {
        Navigator.of(context).pop();
      }
    }
  }

  @override
  Widget build(BuildContext context) {
    final width = MediaQuery.of(context).size.width;
    final height = MediaQuery.of(context).size.height;
    return Scaffold(
      backgroundColor: Colors.white,
      body: SafeArea(
        child: Stack(
          children: [
            Positioned(
              bottom: -height * 0.12,
              left: -width * 0.1,
              right: -width * 0.1,
              child: Container(
                width: width * 1.2,
                height: height * 0.28,
                decoration: BoxDecoration(
                  color: const Color(0xFFF3EAF9),
                  borderRadius: BorderRadius.only(
                    topLeft: Radius.circular(width * 0.8),
                    topRight: Radius.circular(width * 0.8),
                  ),
                ),
              ),
            ),
            Center(
              child: Padding(
                padding: const EdgeInsets.symmetric(horizontal: 24),
                child: Column(
                  mainAxisSize: MainAxisSize.min,
                  children: [
                    Container(
                      width: 120,
                      height: 120,
                      decoration: BoxDecoration(
                        color: ColorConstants.primaryColor.withOpacity(0.1),
                        shape: BoxShape.circle,
                      ),
                      child: Icon(
                        Icons.wifi_off,
                        color: ColorConstants.primaryColor,
                        size: 64,
                      ),
                    ),
                    const SizedBox(height: 24),
                    Text(
                      'You are offline',
                      textAlign: TextAlign.center,
                      style: TextStyle(
                        fontSize: 26,
                        fontWeight: FontWeight.bold,
                        fontFamily: FontConstants.fontFamily,
                        color: Colors.black,
                      ),
                    ),
                    const SizedBox(height: 12),
                    Text(
                      'Please check your internet connection. We\'ll reconnect automatically when you\'re back online.',
                      textAlign: TextAlign.center,
                      style: TextStyle(
                        fontSize: 16,
                        color: Colors.grey[700],
                        fontFamily: FontConstants.fontFamily,
                      ),
                    ),
                    const SizedBox(height: 24),
                    SizedBox(
                      width: 180,
                      height: 48,
                      child: ElevatedButton(
                        style: ElevatedButton.styleFrom(
                          backgroundColor: ColorConstants.primaryColor,
                          shape: RoundedRectangleBorder(
                            borderRadius: BorderRadius.circular(12),
                          ),
                        ),
                        onPressed: () => _handleRetry(context),
                        child: const Text(
                          'Retry',
                          style: TextStyle(color: Colors.white, fontSize: 16),
                        ),
                      ),
                    ),
                  ],
                ),
              ),
            ),
          ],
        ),
      ),
    );
  }
}


