import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:hello_wallpaper/common/components/view_state_widget.dart';
import 'package:webview_flutter/webview_flutter.dart';

class WebPageController extends GetxController {
  var title = '';
  var url = '';
  late final WebViewController webViewController;
  var viewState = ViewState.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 = ViewState.normal;
          },
          onWebResourceError: (WebResourceError error) {
          },
          onNavigationRequest: (NavigationRequest request) {
            return NavigationDecision.navigate;
          },
        ),
      )
      ..loadRequest(Uri.parse(url));
  }
}
