import 'package:flutter/material.dart';
import 'package:rasadyar_core/core.dart';

const Duration _kFadeDuration = Duration(milliseconds: 165);

class RSegment extends StatefulWidget {
  const RSegment({
    super.key,
    required this.children,
    required this.selectedIndex,
    required this.onSegmentSelected,
    required this.backgroundColor,
    required this.selectedBackgroundColor,
    required this.borderColor,
    required this.selectedBorderColor,
    this.padding,
  });

  final List<String> children;
  final int selectedIndex;
  final Function(int index)? onSegmentSelected;
  final EdgeInsetsGeometry? padding;
  final Color backgroundColor;
  final Color borderColor;
  final Color selectedBackgroundColor;
  final Color selectedBorderColor;

  @override
  State<RSegment> createState() => _RSegmentState();
}

class _RSegmentState extends State<RSegment> {
  late int selectedIndex;

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

  @override
  void didUpdateWidget(covariant RSegment oldWidget) {
    super.didUpdateWidget(oldWidget);
    if (oldWidget.selectedIndex != widget.selectedIndex) {
      widget.onSegmentSelected?.call(widget.selectedIndex);
    }
  }

  @override
  Widget build(BuildContext context) {
    return Row(
      children: List.generate(
        widget.children.length,
        (index) => Expanded(
          child: _generateChild(
            index: index,
            hasNext: widget.children.length > index + 1,
            hasPrevious: index > 0,
          ),
        ),
      ),
    );
  }

  Widget _generateChild({required int index, required bool hasNext, required bool hasPrevious}) {
    final bool isSelected = selectedIndex == index;

    return GestureDetector(
      onTap: () {
        setState(() {
          selectedIndex = index;
        });
        widget.onSegmentSelected?.call(index);
      },
      child: AnimatedContainer(
        duration: _kFadeDuration,
         padding: widget.padding??EdgeInsets.symmetric(vertical: 8),
        margin: EdgeInsets.zero,
        decoration: BoxDecoration(
          color: isSelected ? widget.selectedBackgroundColor : widget.backgroundColor,
          borderRadius: BorderRadius.only(
            topRight: !hasPrevious && hasNext ? Radius.circular(8) : Radius.zero,
            bottomRight: !hasPrevious && hasNext ? Radius.circular(8) : Radius.zero,
            bottomLeft: hasPrevious && !hasNext ? Radius.circular(8) : Radius.zero,
            topLeft: hasPrevious && !hasNext ? Radius.circular(8) : Radius.zero,
          ),
          border: Border.all(
            width: 1,
            color: isSelected ? widget.selectedBorderColor : widget.borderColor,
          ),
        ),
        child: Center(
          child: Text(
            widget.children[index],
            textAlign: TextAlign.center,
            style: isSelected
                ? AppFonts.yekan16Bold.copyWith(color: widget.selectedBorderColor)
                : AppFonts.yekan16.copyWith(color: AppColor.mediumGreyDarkHover),
          ),
        ),
      ),
    );
  }
}
