import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:webview_flutter/webview_flutter.dart';

class lahzaPaymentGateway extends StatefulWidget {
  final String url;
  final String referenceNumber;
  final Function checkPaymentStatus;
  const lahzaPaymentGateway(this.url, this.checkPaymentStatus, this.referenceNumber, {Key? key}) : super(key: key);

  @override
  State<lahzaPaymentGateway> createState() => _lahzaPaymentGatewayState();
}

class _lahzaPaymentGatewayState extends State<lahzaPaymentGateway> {
  String refNumber = "";

  @override
  void initState() {
     super.initState();
  }
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: WebView(
        initialUrl: this.widget.url,
        javascriptMode: JavascriptMode.unrestricted,
        userAgent: 'Flutter;Webview',
        navigationDelegate: (navigation){
          if(navigation.url == 'https://api.lahza.io/close'){
            Navigator.of(context).pop(); //close webview
            this.widget.checkPaymentStatus(this.widget.referenceNumber);
          }
          if(navigation.url == "https://cancelpayment.com/"){
            Navigator.of(context).pop(); //close webview
          } 
          if(navigation.url == "https://example.ps/callback"){
            Navigator.of(context).pop(); //close webview
          }
          return NavigationDecision.navigate;
        },
      ),
    );
  }
}
