import 'package:hmg_patient_app/pages/landing/landing_page.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_inappwebview/flutter_inappwebview.dart';

class VideoCallWebPage extends StatelessWidget{
  final GlobalKey webViewKey = GlobalKey();

  // InAppWebViewController webViewController;
  InAppWebViewController? controller;

  URLRequest? request;
  final String receiverId;
  final String callerId;
  VideoCallWebPage({required this.receiverId, required this.callerId}){
    request = URLRequest(url: WebUri.uri(Uri.parse("https://vcallapi.hmg.com/Mobileindex.html?username=$receiverId&doctorid=$callerId")));
  }

  InAppWebViewGroupOptions options = InAppWebViewGroupOptions(
      crossPlatform: InAppWebViewOptions(
        cacheEnabled: false,
        clearCache: true,
        disableHorizontalScroll: true,
        disableVerticalScroll: true,
        disableContextMenu: true,
        supportZoom: false,
        javaScriptEnabled: true,
        preferredContentMode: UserPreferredContentMode.MOBILE,
        useShouldOverrideUrlLoading: true,
        mediaPlaybackRequiresUserGesture: false,

      ),
      android: AndroidInAppWebViewOptions(
        hardwareAcceleration: true,
        useHybridComposition: true,
      ),
      ios: IOSInAppWebViewOptions(
        allowsAirPlayForMediaPlayback: true,
        allowsPictureInPictureMediaPlayback: true,
        allowsInlineMediaPlayback: true,
      )
  );

  @override
  Widget build(BuildContext context) {
    return  Scaffold(
      primary: true,
      appBar: AppBar(
        backgroundColor: Colors.transparent, leading: Container(),
        systemOverlayStyle: SystemUiOverlayStyle(statusBarColor: Colors.black),
      ),
      extendBodyBehindAppBar: true,
      extendBody: false,
      // backgroundColor: Colors.white,
      body: Column(
        children: [
          SizedBox(height: MediaQuery.of(context).viewPadding.top),
          Expanded(
            child: InAppWebView(
              androidOnPermissionRequest: androidOnPermissionRequest,
              initialOptions: options,
              initialUrlRequest: request,
              onWebViewCreated: onWebViewCreated,
              onLoadStart: onLoadStart,
              onLoadError: onError,
              onConsoleMessage: onConsoleMessage,
              // shouldOverrideUrlLoading: shouldRedirect ,
            ),
          ),
        ],
      ),
    );

    ChromeSafariBrowser();
  }


  Future<PermissionRequestResponse> androidOnPermissionRequest(InAppWebViewController controller, String origin, List<String> resources) async {
    return PermissionRequestResponse(
        resources: resources,
        action: PermissionRequestResponseAction.GRANT
    );
  }

  onWebViewCreated(InAppWebViewController controller) {
    this.controller = controller;
    LandingPage.isOpenCallPage = true;
  }

  onConsoleMessage(controller, ConsoleMessage consoleMessage){
    print(consoleMessage);
  }

  onError(InAppWebViewController? controller, Uri? url, int code, String message) {

  }

  onLoadStart(InAppWebViewController? controller, Uri? url) {
    print(url);
    if(url.toString().toLowerCase().contains("endcallpage")){
      LandingPage.isOpenCallPage = false;
      if(LandingPage.isOpenCallPage == true) {
        print("END CALL!!!");
        // controller.goBack();
        Navigator.pop(webViewKey.currentContext!);
      }
    }
  }

  Future<NavigationActionPolicy> shouldRedirect(InAppWebViewController controller, NavigationAction navigationAction) async {
    var uri = navigationAction.request.url;
    // if(uri.queryParameters['exit'] == "yes"){
    //   Navigator.pop(webViewKey.currentContext);
    // }
    if(uri.toString().toLowerCase().contains("endcallpage")){
      Navigator.pop(webViewKey.currentContext!);
    }

    return NavigationActionPolicy.ALLOW;
  }


}