import 'package:flutter/gestures.dart';
import 'package:flutter/material.dart';
import 'package:sigap/src/utils/constants/colors.dart';
import 'package:sigap/src/utils/constants/sizes.dart';
import 'package:sigap/src/utils/device/device_utility.dart';
import 'package:sigap/src/utils/helpers/helper_functions.dart';

class TabBarApp extends StatelessWidget implements PreferredSizeWidget {
  const TabBarApp({
    super.key,
    required this.tabs,
    this.controller,
    this.isScrollable = false,
    this.padding,
    this.indicatorColor,
    this.automaticIndicatorColorAdjustment = true,
    this.indicatorWeight = 2.0,
    this.indicatorPadding = EdgeInsets.zero,
    this.indicator,
    this.indicatorSize,
    this.dividerColor,
    this.dividerHeight,
    this.labelColor,
    this.labelStyle,
    this.labelPadding,
    this.unselectedLabelColor,
    this.unselectedLabelStyle,
    this.dragStartBehavior = DragStartBehavior.start,
    this.overlayColor,
    this.mouseCursor,
    this.enableFeedback,
    this.onTap,
    this.physics,
    this.splashFactory,
    this.splashBorderRadius,
    this.tabAlignment = TabAlignment.fill,
    this.textScaler,
    this.indicatorAnimation,
  });

  final List<Widget> tabs;
  final TabController? controller;
  final bool isScrollable;
  final EdgeInsetsGeometry? padding;
  final Color? indicatorColor;
  final bool automaticIndicatorColorAdjustment;
  final double indicatorWeight;
  final EdgeInsetsGeometry indicatorPadding;
  final Decoration? indicator;
  final TabBarIndicatorSize? indicatorSize;
  final Color? dividerColor;
  final double? dividerHeight;
  final Color? labelColor;
  final TextStyle? labelStyle;
  final EdgeInsetsGeometry? labelPadding;
  final Color? unselectedLabelColor;
  final TextStyle? unselectedLabelStyle;
  final DragStartBehavior dragStartBehavior;
  final WidgetStateProperty<Color?>? overlayColor;
  final MouseCursor? mouseCursor;
  final bool? enableFeedback;
  final void Function(int)? onTap;
  final ScrollPhysics? physics;
  final InteractiveInkFeatureFactory? splashFactory;
  final BorderRadius? splashBorderRadius;
  final TabAlignment? tabAlignment;
  final TextScaler? textScaler;
  final TabIndicatorAnimation? indicatorAnimation;

  @override
  Widget build(BuildContext context) {
    final isDark = THelperFunctions.isDarkMode(context);

    return Material(
      color: isDark ? TColors.dark : TColors.white,
      child: TabBar(
        tabs: tabs,
        controller: controller,
        isScrollable: isScrollable,
        padding: padding,
        indicatorColor: indicatorColor ?? TColors.primary,
        automaticIndicatorColorAdjustment: automaticIndicatorColorAdjustment,
        indicatorWeight: indicatorWeight,
        indicatorPadding: indicatorPadding,
        indicator: indicator,
        indicatorSize: indicatorSize,
        dividerColor: dividerColor,
        dividerHeight: dividerHeight ?? TSizes.dividerHeight,
        labelColor: labelColor ?? TColors.primary,
        labelStyle:
            labelStyle ??
            Theme.of(
              context,
            ).textTheme.bodyMedium?.copyWith(fontWeight: FontWeight.w600),
        labelPadding:
            labelPadding ?? const EdgeInsets.symmetric(horizontal: TSizes.md),
        unselectedLabelColor:
            unselectedLabelColor ?? (isDark ? TColors.grey : TColors.darkGrey),
        unselectedLabelStyle:
            unselectedLabelStyle ?? Theme.of(context).textTheme.bodyMedium,
        dragStartBehavior: dragStartBehavior,
        mouseCursor: mouseCursor,
        enableFeedback: enableFeedback,
        onTap: onTap,
        physics: physics,
        splashFactory: splashFactory,
        tabAlignment: tabAlignment,
        textScaler: textScaler,
        indicatorAnimation: indicatorAnimation,
        overlayColor:
            overlayColor ??
            WidgetStateProperty.resolveWith<Color?>((Set<WidgetState> states) {
              if (states.contains(WidgetState.hovered)) {
                return TColors.primary.withOpacity(0.1);
              }
              if (states.contains(WidgetState.pressed)) {
                return TColors.primary.withOpacity(0.2);
              }
              return null;
            }),
        splashBorderRadius:
            splashBorderRadius ?? BorderRadius.circular(TSizes.md),
      ),
    );
  }

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