import 'package:flutter/material.dart';
import 'package:hmg_patient_app_new/core/app_export.dart';
import 'package:hmg_patient_app_new/core/utils/utils.dart';
import 'package:hmg_patient_app_new/extensions/int_extensions.dart';
import 'package:hmg_patient_app_new/extensions/string_extensions.dart';
import 'package:hmg_patient_app_new/extensions/widget_extensions.dart';
import 'package:hmg_patient_app_new/theme/colors.dart';

class CustomTabBarModel {
  String? image;
  String title;

  CustomTabBarModel(this.image, this.title);
}

class CustomTabBar extends StatefulWidget {
  final int initialIndex;
  final List<CustomTabBarModel> tabs;
  final Color? activeTextColor;
  final Color? activeBackgroundColor;
  final Color? inActiveTextColor;
  final Color? inActiveBackgroundColor;
  final Function(int)? onTabChange;
  final bool shouldTabExpanded;

  const CustomTabBar({
    super.key,
    required this.tabs,
    this.initialIndex = 0,
    this.activeTextColor,
    this.inActiveTextColor,
    this.activeBackgroundColor,
    this.inActiveBackgroundColor,
    this.onTabChange,
    this.shouldTabExpanded = false
  });

  @override
  CustomTabBarState createState() => CustomTabBarState();
}

class CustomTabBarState extends State<CustomTabBar> {
  int selectedIndex = 0;

  @override
  void initState() {
    selectedIndex = widget.initialIndex;
    super.initState();
  }

  @override
  void didUpdateWidget(covariant CustomTabBar oldWidget) {
    super.didUpdateWidget(oldWidget);
    if (oldWidget.initialIndex != widget.initialIndex) {
      selectedIndex = widget.initialIndex;
    }
  }

  @override
  void dispose() {
    super.dispose();
  }

  void callBackCurrentIndex() {
    if (widget.onTabChange != null) widget.onTabChange!(selectedIndex);
  }

  @override
  Widget build(BuildContext context) {
    final resolvedActiveTextColor = widget.activeTextColor ?? AppColors.textColor;
    final resolvedInActiveTextColor = widget.inActiveTextColor ?? AppColors.inputLabelTextColor;
    final resolvedActiveBgColor = widget.activeBackgroundColor ?? AppColors.lightGrayBGColor;
    final resolvedInActiveBgColor = widget.inActiveBackgroundColor ?? AppColors.whiteColor;
    late Widget parentWidget;
    if(widget.shouldTabExpanded){
      return Row(
        children:List.generate(widget.tabs.length, (index)=>myTab(widget.tabs[index], index, resolvedActiveTextColor, resolvedInActiveTextColor, resolvedActiveBgColor, resolvedInActiveBgColor).expanded),
      );
    }

    if (widget.tabs.length > 3) {
      parentWidget = ListView.separated(
        shrinkWrap: true,
        scrollDirection: Axis.horizontal,
        padding: EdgeInsets.zero,
        physics: const BouncingScrollPhysics(),
        itemBuilder: (cxt, index) => myTab(widget.tabs[index], index, resolvedActiveTextColor, resolvedInActiveTextColor, resolvedActiveBgColor, resolvedInActiveBgColor),
        separatorBuilder: (cxt, index) => 4.width,
        itemCount: widget.tabs.length,
      );
    } else {
      parentWidget = Row(
        mainAxisAlignment: MainAxisAlignment.center,
        spacing: 4.w,
        children: [for (int i = 0; i < widget.tabs.length; i++) myTab(widget.tabs[i], i, resolvedActiveTextColor, resolvedInActiveTextColor, resolvedActiveBgColor, resolvedInActiveBgColor).expanded],
      );
    }

    return Container(
        height: 62.h,
        padding: EdgeInsets.all(4.w),
        decoration: RoundedRectangleBorder().toSmoothCornerDecoration(
          color: AppColors.whiteColor,
          borderRadius: 10.r,
        ),
        child: Center(child: parentWidget));
  }

  Widget myTab(CustomTabBarModel tabBar, int currentIndex, Color activeTextColor, Color inActiveTextColor, Color activeBgColor, Color inActiveBgColor) {
    bool isSelected = selectedIndex == currentIndex;
    return Container(
        height: (isTablet || isFoldable) ? 60.h : 54.h,
        padding: EdgeInsets.only(top: 4.h, bottom: 4.h, left: 14.w, right: 14.w),
        alignment: Alignment.center,
        decoration: RoundedRectangleBorder().toSmoothCornerDecoration(
          color: isSelected ? activeBgColor : inActiveBgColor,
          borderRadius: 10.r,
        ),
        child: Row(
          mainAxisSize: MainAxisSize.min,
          spacing: 4.w,
          children: [
            if (tabBar.image != null)
              Utils.buildSvgWithAssets(
                icon: tabBar.image!,
                height: 18.h,
                width: 18.w,
                iconColor: isSelected ? activeTextColor : inActiveTextColor,
              ),
            tabBar.title.toText13(color: isSelected ? activeTextColor : inActiveTextColor, letterSpacing: isSelected ? -0.3 : -0.1, isBold: true),
          ],
        )).onPress(() {
      setState(() {
        selectedIndex = currentIndex;
      });
      callBackCurrentIndex();
    });
  }
}
