import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';
import 'package:flashlink/utils/misc.dart';

import '../../config/translations/localization_service.dart';
import '../../config/theme/theme_extensions/theme_extension.dart';
import 'dart:math';

import '../constants/iconfont/iconfont.dart';
import 'click_opacity.dart';

class IXAppBar extends StatelessWidget implements PreferredSizeWidget {
  final String title;
  final bool showBackButton;
  final VoidCallback? onBackPressed;
  final List<Widget>? actions;
  final Color? backgroundColor;
  final Color? titleColor;
  final Color? backIconColor;
  final double? titleSize;
  final IconData? backIcon;
  final FontWeight? titleWeight;

  const IXAppBar({
    super.key,
    required this.title,
    this.showBackButton = true,
    this.onBackPressed,
    this.actions,
    this.backgroundColor,
    this.titleColor,
    this.backIconColor,
    this.titleSize = 20,
    this.titleWeight = FontWeight.w600,
    this.backIcon,
  });

  @override
  Widget build(BuildContext context) {
    // 使用 Obx 包装 AppBar 以响应主题变化
    return AppBar(
      backgroundColor:
          backgroundColor ?? Get.reactiveTheme.scaffoldBackgroundColor,
      elevation: 0,
      centerTitle: true,
      toolbarHeight: isDesktop ? 40 : 60, // 配置桌面版本的AppBar高度
      scrolledUnderElevation: 0,
      leading: showBackButton
          ? ClickOpacity(
              onTap: onBackPressed ?? () => Get.back(),
              child: Transform.rotate(
                angle: LocalizationService.isRTL() ? pi : 0, // 180度 = π 弧度
                child: Icon(
                  backIcon ?? IconFont.icon01,
                  color: Get.reactiveTheme.textTheme.bodyLarge!.color,
                  size: 24.w,
                ),
              ),
            )
          : null,
      title: Text(
        title,
        style: TextStyle(
          color: Get.reactiveTheme.textTheme.bodyLarge!.color,
          fontSize: isDesktop ? 16 : titleSize,
          fontWeight: titleWeight,
        ),
      ),
      actions: actions,
    );
  }

  @override
  Size get preferredSize => Size.fromHeight(isDesktop ? 40 : 60); // 配置桌面版本的AppBar高度
}
