import 'dart:developer';

import 'package:hub/app_state.dart';
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:flutter_inappwebview/flutter_inappwebview.dart';
import 'package:url_launcher/url_launcher_string.dart';
import 'package:webview_flutter/webview_flutter.dart';
import 'dart:io' show Platform;

class FastPassPageWidget extends StatefulWidget {
  final String freToken = FFAppState().userUUID;
  final String freUserData = "{\"name\": \"${FFAppState().name}\", " +
      "\"email\": \"${FFAppState().email}\"," +
      "\"dev_id\": \"${FFAppState().devUUID}\"," +
      "\"created_at\": \"${FFAppState().createdAt}\"," +
      "\"updated_at\": \"0000-00-00 00:00:00\"," +
      "\"status\": \"A\" }";

  final String clientId = FFAppState().cliUUID;

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

class _FastPassPageWidgetState extends State<FastPassPageWidget> {
  late InAppWebViewController _controllerIOS;
  late WebViewController _controllerAll;
  late String url;
  late String name;
  late String email;
  late String userUUID;
  late String created_at;

  @override
  void initState() {
    super.initState();
    name = FFAppState().name;
    email = FFAppState().email;
    userUUID = FFAppState().userUUID;
    created_at = FFAppState().createdAt;
    url = 'https://hub.freaccess.com.br/hub/fast-pass/${widget.clientId}';
  }

  @override
  Widget build(BuildContext context) {


    return SafeArea(
      child: Scaffold(
        body: Platform.isIOS ? InAppWebView(
          initialUrlRequest: URLRequest(url: WebUri(url)),
          initialSettings: InAppWebViewSettings(
            allowsBackForwardNavigationGestures: true,
            javaScriptEnabled: true,
          ),
          onWebViewCreated: (controller) async {
            _controllerIOS = controller;
          },
          onLoadStop: (controller, url) async {
            await controller.evaluateJavascript(source: "window.localStorage.setItem('fre-token', '\"${widget.freToken}\"')");
            await controller.evaluateJavascript(source: "window.localStorage.setItem('fre-user-data', '${widget.freUserData}')");
          },
        ) : WebViewWidget(
          controller: _controllerAll = WebViewController()
            ..clearCache()
            ..clearLocalStorage()
            ..setJavaScriptMode(JavaScriptMode.unrestricted)
            ..setBackgroundColor(const Color(0x00000000))
            ..setNavigationDelegate(
              NavigationDelegate(
                onProgress: (int progress) {},
                onPageStarted: (String url) {
                  final String token = "localStorage.setItem('fre-token', '\"${widget.freToken}\"');";
                  final String data = "localStorage.setItem('fre-user-data', '${widget.freUserData}');";

                  _controllerAll.runJavaScript(token);
                  _controllerAll.runJavaScript(data);
                },
                onNavigationRequest: (NavigationRequest request) {
                  if (request.url.startsWith('http') ||
                      request.url.startsWith('https://api.whatsapp.com/send') ||
                      request.url.startsWith('https://wa.me')) {
                    launchUrlString(request.url);
                    return NavigationDecision.prevent;
                  }
                  return NavigationDecision.prevent;
                },
              ),
            )
            ..loadRequest(Uri.parse(url)),
        ),
      ),
    );
    
  }
}