import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_inappwebview/flutter_inappwebview.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';

import '../../base/base_page.dart';
import '../../resource/assets.gen.dart';
import '../../resource/colors.gen.dart';
import '../../router/app_pages.dart';
import 'browser_controller.dart';

class BrowserPage extends BasePage<BrowserController> {
  const BrowserPage({super.key});

  static start(String url) {
    Get.toNamed(RoutePath.browser, arguments: url);
  }

  @override
  Widget buildBody(BuildContext context) {
    return WillPopScope(
      onWillPop: () async {
        return await controller.handleBack();
      },
      child: Scaffold(
        backgroundColor: Colors.transparent,
        appBar: AppBar(
          systemOverlayStyle: SystemUiOverlayStyle.dark,
          backgroundColor: Colors.transparent,
          title: Obx(() => Text(controller.title.value,
              style: TextStyle(
                  fontSize: 17.sp, color: ColorName.primaryTextColor))),
          leading: IconButton(
            icon: SizedBox(
                width: 24.w,
                height: 24.w,
                child: Assets.images.iconBlackBack.image()),
            // Custom icon
            onPressed: () {
              Get.back();
            },
          ),
        ),
        body: _buildContentView(),
      ),
    );
  }

  Widget _buildContentView() {
    return InAppWebView(
      initialUrlRequest: URLRequest(url: WebUri(controller.url)),
      onWebViewCreated: (webController) =>
          controller.webViewController = webController,
      onTitleChanged: (webController, title) =>
          controller.title.value = title ?? '',
    );
  }
}
