import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:plugin_basic/utils/ext_get_nav.dart';
import 'package:router/path/router_path.dart';
import 'package:shared/utils/log_utils.dart';
import 'package:shared/utils/screen_util.dart';
import 'package:widgets/my_appbar.dart';
import 'package:widgets/widget_export.dart';

import '../base/base_stateless_page.dart';
import '../widget/webview_page.dart';

/*
 * 全局的公共Web页面
 */
class GlobalWebPage extends BaseStatelessPage {
  GlobalWebPage({super.key});

  //启动当前页面
  static void startInstance(
    String title,
    String url, {
    bool isShowAppBar = true,
  }) {
    return Get.start(RouterPath.globalWeb, arguments: {'title': title, 'initialUrl': Uri.encodeFull(url), 'isShowAppBar': isShowAppBar});
  }

  @override
  GetxController createRawController() {
    throw UnimplementedError();
  }

  @override
  void initState() {}

  @override
  Widget buildWidget(BuildContext context) {
    bool isShowAppBar = Get.arguments['isShowAppBar'];
    String initialUrl = Get.arguments['initialUrl'];
    String title = Get.arguments['title'];
    Log.d("GlobalWebPage 的参数 isShowAppBar：$isShowAppBar title:$title initialUrl:$initialUrl ");

    if (initialUrl.startsWith('http')) {
      //网页加载
      return WebViewPage(
        showAppbar: isShowAppBar,
        initialUrl: initialUrl,
        arguments: {'title': title},
      );
    } else {
      //富文本加载
      return SafeArea(
        bottom: MediaQuery.of(context).padding.bottom > 38,
        top: false,
        child: Container(
          width: double.infinity,
          height: double.infinity,
          padding: EdgeInsets.only(top: ScreenUtil.getStatusBarH(context)),
          decoration: const BoxDecoration(
            gradient: LinearGradient(
              colors: [
                Color(0xFF091D44),
                Color(0xFF245A8A),
                Color(0xFF7F7CEC),
              ],
              begin: Alignment.topCenter,
              end: Alignment.bottomCenter,
            ),
          ),
          child: Column(
            children: [
              MyAppBar.titleBar(context, title),

              SingleChildScrollView(
                scrollDirection: Axis.vertical,
                physics: const BouncingScrollPhysics(),
                child: Html(data: initialUrl).paddingOnly(left: 10, right: 10),
              )
            ],
          ),
        ),
      );
    }
  }
}
