import 'package:flutter/material.dart';
import 'package:nearle/views/helpers/constants/Colorconstants.dart';
import 'package:get/get.dart';
import 'package:webview_flutter/webview_flutter.dart';

class FaqController extends GetxController {
  WebViewController? webViewController;
  var isLoading = true.obs;

  @override
  void onInit() {
    super.onInit();
    initializeWebView();
  }

  void initializeWebView() {
    webViewController = WebViewController()
      ..setJavaScriptMode(JavaScriptMode.unrestricted)
      ..setBackgroundColor(const Color(0x00000000))
      ..setNavigationDelegate(
        NavigationDelegate(
          onPageStarted: (url) {
            isLoading.value = true;
            print('Started loading: $url');
          },
          onPageFinished: (url) {
            isLoading.value = false;
            print('Finished loading: $url');
          },
          onWebResourceError: (error) {
            isLoading.value = false;
            print('WebView error: ${error.description}');
          },
        ),
      );
    loadFaqUrl();
  }

  Future<void> loadFaqUrl() async {
    if (webViewController != null) {
      try {
        await webViewController!.loadRequest(
          Uri.parse('https://nearle.in/faq'),
        );
      } catch (e) {
        print('Error loading URL: $e');
      }
    }
  }
}

class FaqPage extends StatelessWidget {
  const FaqPage({super.key});

  @override
  Widget build(BuildContext context) {
    final controller = Get.put(FaqController());
    return Scaffold(
      appBar: AppBar(
        backgroundColor: ColorConstants.primaryColor,
        centerTitle: true,
        toolbarHeight: 70,
        leading: IconButton(
          icon: const Icon(
            Icons.arrow_back_ios,
            color: Colors.white,
          ), // :small_blue_diamond: white back arrow
          onPressed: () {
            Navigator.pop(context); // goes back to previous screen
          },
        ),
        title: const Text(
          'FAQ',
          style: TextStyle(
            fontSize: 26, // :small_blue_diamond: larger font size
            color: Colors.white, // :small_blue_diamond: white text
            fontWeight: FontWeight.bold,
            letterSpacing: 1.2,
          ),
        ),
        elevation: 4,
      ),
      body: Obx(() {
        final wvc = controller.webViewController;
        return Stack(
          children: [
            if (wvc != null) WebViewWidget(controller: wvc),
            if (controller.isLoading.value)
              const LinearProgressIndicator(minHeight: 2),
          ],
        );
      }),
    );
  }
}
