import 'package:flutter/material.dart';
import 'package:get/get.dart';

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

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 Obx(() {
      // 使用响应式主题颜色作为默认值
      final effectiveTitleColor =
          titleColor ?? Get.reactiveTheme.textTheme.bodyLarge!.color;
      final effectiveBackIconColor =
          backIconColor ?? Get.reactiveTheme.textTheme.bodyLarge!.color;

      return AppBar(
        backgroundColor: backgroundColor ?? Colors.transparent,
        elevation: 0,
        centerTitle: true,
        toolbarHeight: 64,
        scrolledUnderElevation: 0,
        leading: showBackButton
            ? ClickOpacity(
                onTap: onBackPressed ?? () => Get.back(),
                child: Transform.rotate(
                  angle: LocalizationService.isRTL() ? pi : 0, // 180度 = π 弧度
                  child: Icon(
                    backIcon ?? Icons.arrow_back,
                    color: effectiveBackIconColor,
                    size: 24,
                  ),
                ),
              )
            : null,
        title: Text(
          title,
          style: TextStyle(
            color: effectiveTitleColor,
            fontSize: titleSize,
            fontWeight: titleWeight,
          ),
        ),
        actions: actions,
      );
    });
  }

  @override
  Size get preferredSize => const Size.fromHeight(64);
}
