import 'package:flutter/material.dart';
import 'package:get/get.dart';
import '../../config/theme/light_theme_colors.dart';
import '../../config/theme/theme_extensions/theme_extension.dart';
import '../controllers/base_core_api.dart';

abstract class BaseView<T> extends GetView<T> {
  const BaseView({super.key});

  // 子类可以重写这些属性来自定义视图
  PreferredSizeWidget? get appBar => null;
  Widget? get bottomNavigationBar => null;
  Widget? get floatingActionButton => null;
  FloatingActionButtonLocation? get floatingActionButtonLocation => null;
  bool get showBackground => true;
  Color? get backgroundColor => LightThemeColors.backgroundColor;
  bool get extendBody => false;
  bool get extendBodyBehindAppBar => false;
  bool get safeArea => false;
  EdgeInsets? get padding => null;
  bool get resizeToAvoidBottomInset => true;
  Widget? get drawer => null;
  Widget? get endDrawer => null;

  bool get isPopScope => false;

  // 添加这个方法来处理返回事件
  void onPopInvoked(bool didPop) {
    if (!didPop) {
      BaseCoreApi().moveTaskToBack();
    }
  }

  // 子类必须实现这个方法来提供视图内容
  Widget buildContent(BuildContext context);

  @override
  Widget build(BuildContext context) {
    // 使用 Obx 包装整个视图以响应主题变化
    Widget view = Obx(() {
      // 在 Obx 内部构建 content，确保使用 Get.reactiveTheme 的子视图能响应主题变化
      return Scaffold(
        backgroundColor: Get.reactiveTheme.scaffoldBackgroundColor,
        appBar: appBar,
        extendBody: extendBody,
        extendBodyBehindAppBar: extendBodyBehindAppBar,
        bottomNavigationBar: bottomNavigationBar,
        floatingActionButton: floatingActionButton,
        floatingActionButtonLocation: floatingActionButtonLocation,
        resizeToAvoidBottomInset: resizeToAvoidBottomInset,
        drawer: drawer,
        endDrawer: endDrawer,
        body: buildContent(context),
      );
    });

    if (isPopScope) {
      return PopScope(
        canPop: false,
        onPopInvokedWithResult: (didPop, result) => onPopInvoked(didPop),
        child: view,
      );
    }
    return view;
  }
}
