import 'dart:async';
import 'package:flutter/material.dart';
// import 'package:http/http.dart' as http;
import 'package:intl/intl.dart';
import 'package:siro_driver/constant/links.dart'; // افترض وجود هذا الملف
import 'package:siro_driver/controller/functions/crud.dart'; // افترض وجود هذا الملف
import '../../../main.dart'; // افترض وجود box هنا
import '../../../constant/box_name.dart'; // افترض وجود هذا الملف

// Service class to handle MTN payment logic
class MtnPaymentService {
  final String _baseUrl =
      "${AppLink.paymentServer}/ride/mtn_new"; // تأكد من تعديل المسار

  // Function to create a new invoice
  Future<String?> createInvoice({
    required String userId,
    required String userType, // 'driver' or 'passenger'
    required double amount,
    required String mtnPhone,
  }) async {
    final url = "$_baseUrl/create_mtn_invoice.php";
    try {
      final response = await CRUD().postWallet(
        // استخدام نفس دالة CRUD
        link: url,
        payload: {
          'user_id': userId,
          'user_type': userType,
          'amount': amount.toString(),
          'mtn_phone': mtnPhone,
        },
      ).timeout(const Duration(seconds: 15));

      if (response != 'failure') {
        final data = response;
        if (data['status'] == 'success' && data['invoice_number'] != null) {
          debugPrint("MTN Invoice created: ${data['invoice_number']}");
          return data['invoice_number'].toString();
        } else {
          debugPrint("Failed to create MTN invoice: ${data['message']}");
          return null;
        }
      } else {
        debugPrint("Server error during MTN invoice creation.");
        return null;
      }
    } catch (e) {
      debugPrint("Exception during MTN invoice creation: $e");
      return null;
    }
  }

  // Function to check invoice status (polling)
  Future<bool> checkInvoiceStatus(String invoiceNumber) async {
    // This should point to a new script on your server that checks mtn_invoices table
    final url = "$_baseUrl/check_mtn_invoice_status.php";
    try {
      final response = await CRUD().postWallet(link: url, payload: {
        'invoice_number': invoiceNumber,
      }).timeout(const Duration(seconds: 10));

      if (response != 'failure') {
        final data = response;
        return data['status'] == 'success' &&
            data['invoice_status'] == 'completed';
      }
      return false;
    } catch (e) {
      debugPrint("Error checking MTN invoice status: $e");
      return false;
    }
  }
}

enum PaymentStatus {
  creatingInvoice,
  waitingForPayment,
  paymentSuccess,
  paymentTimeout,
  paymentError
}

class PaymentScreenMtn extends StatefulWidget {
  final double amount;
  // يمكنك إضافة متغير لتحديد هل المستخدم سائق أم راكب
  final String userType; // 'driver' or 'passenger'

  const PaymentScreenMtn({
    super.key,
    required this.amount,
    required this.userType,
  });

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

class _PaymentScreenMtnState extends State<PaymentScreenMtn> {
  final MtnPaymentService _paymentService = MtnPaymentService();
  Timer? _pollingTimer;
  PaymentStatus _status = PaymentStatus.creatingInvoice;
  String? _invoiceNumber;
  // جلب البيانات من الـ box
  final String userId =
      box.read(BoxName.driverID) ?? box.read(BoxName.passengerID);
  final String phone = box.read(BoxName.phoneWallet);

  @override
  void initState() {
    super.initState();
    _createAndPollInvoice();
  }

  @override
  void dispose() {
    _pollingTimer?.cancel();
    super.dispose();
  }

  void _createAndPollInvoice() async {
    setState(() => _status = PaymentStatus.creatingInvoice);

    final invoiceNumber = await _paymentService.createInvoice(
      userId: userId,
      userType: widget.userType,
      amount: widget.amount,
      mtnPhone: phone,
    );

    if (invoiceNumber != null && mounted) {
      setState(() {
        _invoiceNumber = invoiceNumber;
        _status = PaymentStatus.waitingForPayment;
      });
      _startPolling(invoiceNumber);
    } else if (mounted) {
      setState(() => _status = PaymentStatus.paymentError);
    }
  }

  void _startPolling(String invoiceNumber) {
    const timeoutDuration = Duration(minutes: 15); // زيادة المهلة
    var elapsed = Duration.zero;

    _pollingTimer = Timer.periodic(const Duration(seconds: 5), (timer) async {
      elapsed += const Duration(seconds: 5);
      if (elapsed >= timeoutDuration) {
        timer.cancel();
        if (mounted) setState(() => _status = PaymentStatus.paymentTimeout);
        return;
      }

      debugPrint("Polling... Checking MTN invoice: $invoiceNumber");
      final isCompleted =
          await _paymentService.checkInvoiceStatus(invoiceNumber);
      if (isCompleted && mounted) {
        timer.cancel();
        setState(() => _status = PaymentStatus.paymentSuccess);
      }
    });
  }

  @override
  Widget build(BuildContext context) {
    return PopScope(
      canPop: _status != PaymentStatus.waitingForPayment,
      onPopInvoked: (didPop) async {
        if (didPop) return;
        if (_status == PaymentStatus.waitingForPayment) {
          final shouldPop = await showDialog<bool>(
            context: context,
            builder: (context) => AlertDialog(
              title: const Text('هل أنت متأكد؟'),
              content: const Text(
                  'إذا خرجت الآن، قد تفشل عملية الدفع. عليك إتمامها من تطبيق MTN.'),
              actions: [
                TextButton(
                    onPressed: () => Navigator.of(context).pop(false),
                    child: const Text('البقاء')),
                TextButton(
                    onPressed: () => Navigator.of(context).pop(true),
                    child: const Text('الخروج')),
              ],
            ),
          );
          if (shouldPop ?? false) {
            Navigator.of(context).pop();
          }
        }
      },
      child: Scaffold(
        appBar: AppBar(title: const Text("الدفع عبر MTN Cash")),
        body: Padding(
          padding: const EdgeInsets.all(16.0),
          child: Center(child: _buildContentByStatus()),
        ),
      ),
    );
  }

  Widget _buildContentByStatus() {
    switch (_status) {
      case PaymentStatus.creatingInvoice:
        return const Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: [
            CircularProgressIndicator(),
            SizedBox(height: 20),
            Text("جاري إنشاء فاتورة دفع...", style: TextStyle(fontSize: 16)),
          ],
        );
      case PaymentStatus.waitingForPayment:
        return _buildWaitingForPaymentUI();
      case PaymentStatus.paymentSuccess:
        return _buildSuccessUI();
      case PaymentStatus.paymentTimeout:
      case PaymentStatus.paymentError:
        return _buildErrorUI();
    }
  }

  Widget _buildWaitingForPaymentUI() {
    final currencyFormat = NumberFormat.decimalPattern('ar_SY');

    return SingleChildScrollView(
      child: Column(
        children: [
          // **مهم**: استبدل هذا المسار بمسار شعار MTN الصحيح في مشروعك
          Image.asset('assets/images/cashMTN.png', width: 120),
          const SizedBox(height: 24),
          Text("تعليمات الدفع", style: Theme.of(context).textTheme.titleLarge),
          const SizedBox(height: 12),
          Text(
            "المبلغ المطلوب: ${currencyFormat.format(widget.amount)} ل.س",
            style: Theme.of(context)
                .textTheme
                .titleMedium
                ?.copyWith(fontWeight: FontWeight.bold),
          ),
          const SizedBox(height: 16),
          Card(
            elevation: 1.5,
            shape:
                RoundedRectangleBorder(borderRadius: BorderRadius.circular(12)),
            child: const Padding(
              padding: EdgeInsets.all(16),
              child: Column(
                children: [
                  _StepTile(number: 1, text: "افتح تطبيق MTN Cash Mobile."),
                  _StepTile(
                      number: 2,
                      text: "اذهب إلى قسم 'دفع الفواتير' أو 'خدمات الدفع'."),
                  _StepTile(
                      number: 3,
                      text: "ابحث عن 'Siro App' في قائمة المفوترين."),
                  _StepTile(
                      number: 4,
                      text:
                          "أدخل رقم هاتفك المسجل لدينا للاستعلام عن الفاتورة."),
                  _StepTile(
                      number: 5,
                      text:
                          "ستظهر لك فاتورة بالمبلغ المطلوب. قم بتأكيد الدفع."),
                ],
              ),
            ),
          ),
          const SizedBox(height: 24),
          const LinearProgressIndicator(minHeight: 2),
          const SizedBox(height: 12),
          Text("بانتظار تأكيد الدفع من MTN...",
              style: TextStyle(color: Colors.grey.shade700)),
          const SizedBox(height: 4),
          const Text("هذه الشاشة ستتحدث تلقائيًا عند اكتمال الدفع",
              style: TextStyle(color: Colors.grey),
              textAlign: TextAlign.center),
        ],
      ),
    );
  }

  Widget _buildSuccessUI() {
    return Column(
      mainAxisAlignment: MainAxisAlignment.center,
      children: [
        const Icon(Icons.check_circle, color: Colors.green, size: 80),
        const SizedBox(height: 20),
        const Text("تم الدفع بنجاح!",
            style: TextStyle(fontSize: 22, fontWeight: FontWeight.bold)),
        const SizedBox(height: 8),
        const Text("تمت إضافة النقاط إلى حسابك.",
            style: TextStyle(fontSize: 16)),
        const SizedBox(height: 20),
        ElevatedButton(
          onPressed: () => Navigator.of(context).pop(),
          child: const Text("العودة إلى المحفظة"),
        ),
      ],
    );
  }

  Widget _buildErrorUI() {
    return Column(
      mainAxisAlignment: MainAxisAlignment.center,
      children: [
        const Icon(Icons.error, color: Colors.red, size: 80),
        const SizedBox(height: 20),
        Text(
          _status == PaymentStatus.paymentTimeout
              ? "انتهى الوقت المحدد للدفع"
              : "حدث خطأ أثناء إنشاء الفاتورة",
          style: const TextStyle(fontSize: 22, fontWeight: FontWeight.bold),
          textAlign: TextAlign.center,
        ),
        const SizedBox(height: 8),
        const Text("يرجى المحاولة مرة أخرى.", style: TextStyle(fontSize: 16)),
        const SizedBox(height: 20),
        ElevatedButton(
          onPressed: _createAndPollInvoice,
          child: const Text("المحاولة مرة أخرى"),
        ),
      ],
    );
  }
}

// ويدجت مساعد لعرض خطوات التعليمات بشكل أنيق
class _StepTile extends StatelessWidget {
  final int number;
  final String text;
  const _StepTile({required this.number, required this.text});

  @override
  Widget build(BuildContext context) {
    return ListTile(
      contentPadding: const EdgeInsets.symmetric(vertical: 4.0),
      leading: CircleAvatar(
        radius: 14,
        backgroundColor: Theme.of(context).primaryColor,
        child: Text("$number",
            style: const TextStyle(
                fontSize: 12,
                color: Colors.white,
                fontWeight: FontWeight.bold)),
      ),
      title: Text(text),
    );
  }
}
