import 'package:flutter/material.dart';
import 'package:flutter_translate/common/components/view_state.dart';
import 'package:get/get.dart';
import 'package:webview_flutter/webview_flutter.dart';

class WebController extends GetxController {
  var title = '';
  var url = '';
  late final WebViewController webViewController;
  var viewState = ViewStateEnum.loading.obs;

  @override
  void onInit() {
    super.onInit();
    title = Get.arguments['title'];
    url = Get.arguments['url'] ?? '';
    webViewController = WebViewController()
      ..setJavaScriptMode(JavaScriptMode.unrestricted)
      ..setBackgroundColor(Colors.white)
      ..setNavigationDelegate(
        NavigationDelegate(
          onProgress: (int progress) {
            // Update loading bar.
          },
          onPageStarted: (String url) {},
          onPageFinished: (String url) {
            viewState.value = ViewStateEnum.normal;
          },
          onWebResourceError: (WebResourceError error) {},
          onNavigationRequest: (NavigationRequest request) {
            return NavigationDecision.navigate;
          },
        ),
      )
      ..loadRequest(Uri.parse(url));
  }
}
