import 'package:cs_resources/theme/app_colors_theme.dart';
import 'package:flutter/material.dart';
import 'package:plugin_basic/dialog/country_code_selecter.dart';
import 'package:plugin_basic/router/basic_page_router.dart';
import 'package:shared/utils/log_utils.dart';
import 'package:hooks_riverpod/hooks_riverpod.dart';
import 'package:widgets/ext/ex_widget.dart';
import 'package:widgets/my_appbar.dart';
import 'package:widgets/widget_export.dart';
import '../widget/webview_page.dart';
import 'package:auto_route/auto_route.dart';

/*
 * 全局的公共Web页面，兼容网页与富文本展示
 */
@RoutePage()
class GlobalWebPage extends HookConsumerWidget {
  final bool isShowAppBar;
  final String initialUrl;
  final String title;

  const GlobalWebPage({
    Key? key,
    @PathParam('isShowAppBar') required this.isShowAppBar,
    @PathParam('initialUrl') required this.initialUrl,
    @PathParam('title') required this.title,
  }) : super(key: key);

  //启动当前页面
  static void startInstance({
    required BuildContext context,
    required String title,
    required String url,
    bool isShowAppBar = true,
  }) {
    context.router.push(GlobalWebPageRoute(isShowAppBar: isShowAppBar, initialUrl: url, title: title));
  }

  @override
  Widget build(BuildContext context, WidgetRef ref) {
    Log.d("GlobalWebPage 的参数 isShowAppBar：$isShowAppBar title:$title initialUrl:$initialUrl ");

    if (initialUrl.startsWith('http')) {
      //网页加载
      return WebViewPage(
        showAppbar: isShowAppBar,
        initialUrl: initialUrl,
        arguments: {'title': title},
      );
    } else {
      //富文本加载
      return Scaffold(
          backgroundColor: context.appColors.whiteBG,
          appBar: isShowAppBar
              ? MyAppBar.appBar(
                  context,
                  title ?? "",
                  backgroundColor: context.appColors.backgroundWhite,
                )
              : null,
          body: SafeArea(
            bottom: true,
            top: false,
            child: SingleChildScrollView(
              scrollDirection: Axis.vertical,
              physics: const BouncingScrollPhysics(),
              child: Html(data: initialUrl).paddingOnly(left: 10, right: 10),
            ),
          ));
    }
  }
}
