import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:iconsax/iconsax.dart';
import 'package:sigap/src/utils/constants/sizes.dart';
import 'package:sigap/src/utils/device/device_utility.dart';

class DAppBar extends StatelessWidget implements PreferredSizeWidget {
  final Widget? avatar;
  final Widget? title;
  final IconData? leadingIcon;
  final List<Widget>? actions;
  final VoidCallback? leadingOnPressed;
  final VoidCallback? onBack;
  final bool showBackArrow;
  final bool centerTitle;
  final bool showActions;

  const DAppBar({
    super.key,
    this.avatar,
    this.title,
    this.leadingIcon,
    this.actions,
    this.leadingOnPressed,
    this.showBackArrow = false,
    this.centerTitle = false,
    this.onBack,
    this.showActions = false,
  });

  @override
  Widget build(BuildContext context) {
    return Padding(
      padding: const EdgeInsets.symmetric(horizontal: TSizes.defaultSpace),
      child: AppBar(
        centerTitle: centerTitle,
        automaticallyImplyLeading: false,
        leading:
            showBackArrow
                ? IconButton(
                  onPressed: onBack ?? () => Get.back(),
                  icon: const Icon(Icons.chevron_left),
                )
                : leadingIcon != null
                ? IconButton(
                  onPressed: leadingOnPressed,
                  icon: Icon(leadingIcon),
                )
                : null,
        title: Row(
          mainAxisSize: MainAxisSize.min,
          children: [
            if (avatar != null) ...[
              CircleAvatar(
                radius: 24,
                backgroundColor: Colors.transparent,
                child: avatar,
              ),
              const SizedBox(width: 8), // Spasi antara avatar dan title
            ],
            if (title != null) title!, // Title jika ada
          ],
        ),
        actions:
            showActions
                ? actions ?? []
                : [
                  IconButton(
                    onPressed: () {},
                    icon: const Icon(Iconsax.notification),
                  ),
                  IconButton(
                    onPressed: () {},
                    icon: const Icon(Iconsax.search_normal),
                  ),
                ],
      ),
    );
  }

  @override
  Size get preferredSize => Size.fromHeight(TDeviceUtils.getAppBarHeight());
}
