import 'package:flutter/material.dart';
import '../ui_colors.dart';
import '../ui_constants.dart';
import '../ui_typography.dart';

/// Sizes for the [UiChip] widget.
enum UiChipSize {
  /// Small size (e.g. for tags in tight spaces).
  small,

  /// Medium size (default).
  medium,

  /// Large size (e.g. for standalone filters).
  large,
}

/// Themes for the [UiChip] widget.
enum UiChipVariant {
  /// Primary style with solid background.
  primary,

  /// Secondary style with light background.
  secondary,

  /// Accent style with highlight background.
  accent,
}

/// A custom chip widget with supports for different sizes, themes, and icons.
class UiChip extends StatelessWidget {
  /// The text label to display.
  final String label;

  /// The size of the chip. Defaults to [UiChipSize.medium].
  final UiChipSize size;

  /// The theme variant of the chip. Defaults to [UiChipVariant.secondary].
  final UiChipVariant variant;

  /// Optional leading icon.
  final IconData? leadingIcon;

  /// Optional trailing icon.
  final IconData? trailingIcon;

  /// Callback when the chip is tapped.
  final VoidCallback? onTap;

  /// Callback when the trailing icon is tapped (e.g. for removal).
  final VoidCallback? onTrailingIconTap;

  /// Whether the chip is currently selected/active.
  final bool isSelected;

  /// Creates a [UiChip].
  const UiChip({
    super.key,
    required this.label,
    this.size = UiChipSize.medium,
    this.variant = UiChipVariant.secondary,
    this.leadingIcon,
    this.trailingIcon,
    this.onTap,
    this.onTrailingIconTap,
    this.isSelected = false,
  });

  @override
  Widget build(BuildContext context) {
    final Color backgroundColor = _getBackgroundColor();
    final Color contentColor = _getContentColor();
    final TextStyle textStyle = _getTextStyle().copyWith(color: contentColor);
    final EdgeInsets padding = _getPadding();
    final double iconSize = _getIconSize();

    final Row content = Row(
      mainAxisSize: MainAxisSize.min,
      children: <Widget>[
        if (leadingIcon != null) ...<Widget>[
          Icon(leadingIcon, size: iconSize, color: contentColor),
          SizedBox(width: _getGap()),
        ],
        Text(label, style: textStyle),
        if (trailingIcon != null) ...<Widget>[
          SizedBox(width: _getGap()),
          GestureDetector(
            onTap: onTrailingIconTap,
            child: Icon(trailingIcon, size: iconSize, color: contentColor),
          ),
        ],
      ],
    );

    return GestureDetector(
      onTap: onTap,
      child: AnimatedContainer(
        duration: const Duration(milliseconds: 200),
        padding: padding,
        decoration: BoxDecoration(
          color: backgroundColor,
          borderRadius: UiConstants.radiusFull,
          border: _getBorder(),
        ),
        child: content,
      ),
    );
  }

  Color _getBackgroundColor() {
    if (!isSelected && variant == UiChipVariant.primary) {
      return UiColors.white;
    }

    switch (variant) {
      case UiChipVariant.primary:
        return UiColors.primary;
      case UiChipVariant.secondary:
        return UiColors.tagInProgress;
      case UiChipVariant.accent:
        return UiColors.accent;
    }
  }

  Color _getContentColor() {
    if (!isSelected && variant == UiChipVariant.primary) {
      return UiColors.textSecondary;
    }

    switch (variant) {
      case UiChipVariant.primary:
        return UiColors.white;
      case UiChipVariant.secondary:
        return UiColors.primary;
      case UiChipVariant.accent:
        return UiColors.accentForeground;
    }
  }

  TextStyle _getTextStyle() {
    switch (size) {
      case UiChipSize.small:
        return UiTypography.body3r;
      case UiChipSize.medium:
        return UiTypography.body2m;
      case UiChipSize.large:
        return UiTypography.body1m;
    }
  }

  EdgeInsets _getPadding() {
    switch (size) {
      case UiChipSize.small:
        return const EdgeInsets.symmetric(horizontal: 10, vertical: 6);
      case UiChipSize.medium:
        return const EdgeInsets.symmetric(horizontal: 12, vertical: 8);
      case UiChipSize.large:
        return const EdgeInsets.symmetric(horizontal: 16, vertical: 10);
    }
  }

  double _getIconSize() {
    switch (size) {
      case UiChipSize.small:
        return 12;
      case UiChipSize.medium:
        return 16;
      case UiChipSize.large:
        return 20;
    }
  }

  double _getGap() {
    switch (size) {
      case UiChipSize.small:
        return UiConstants.space1;
      case UiChipSize.medium:
        return UiConstants.space1 + 2;
      case UiChipSize.large:
        return UiConstants.space2;
    }
  }

  BoxBorder? _getBorder() {
    if (!isSelected && variant == UiChipVariant.primary) {
      return Border.all(color: UiColors.border);
    }
    return null;
  }
}
