import 'package:flutter/material.dart';
import 'package:kasy_kit/core/theme/theme.dart';
import 'package:kasy_kit/core/widgets/kasy_hover.dart';

typedef SettingsTileOnTap = void Function();

/// Rounded-square icon container used in settings rows.
/// Uses a soft tinted background (12 % opacity) with a colored icon —
/// keeping the hue recognizable without being visually heavy.
class SettingsIconBadge extends StatelessWidget {
  const SettingsIconBadge({
    super.key,
    required this.icon,
    required this.color,
    this.size = 32,
  });

  final IconData icon;
  final Color color;
  final double size;

  @override
  Widget build(BuildContext context) {
    return Container(
      width: size,
      height: size,
      decoration: BoxDecoration(
        color: color.withValues(alpha: 0.12),
        borderRadius: BorderRadius.circular(8),
      ),
      child: Icon(
        icon,
        size: size * 0.56,
        color: color,
      ),
    );
  }
}

/// Trailing chevron for tappable settings rows: slightly larger and higher
/// contrast in light mode, slightly lighter in dark mode.
class SettingsListChevron extends StatelessWidget {
  const SettingsListChevron({super.key});

  static const double _iconSize = 18;

  @override
  Widget build(BuildContext context) {
    final Brightness brightness = Theme.of(context).brightness;
    final Color onSurface = context.colors.onSurface;
    final Color color = brightness == Brightness.light
        ? onSurface.withValues(alpha: 0.57)
        : Color.lerp(onSurface, Colors.white, 0.14) ?? onSurface;
    return Icon(
      KasyIcons.arrowForwardIos,
      size: _iconSize,
      color: color,
    );
  }
}

/// This widget is used to show a divider between settings tiles
class SettingsDivider extends StatelessWidget {
  const SettingsDivider({super.key});

  @override
  Widget build(BuildContext context) {
    return Divider(color: context.colors.onBackground.withValues(alpha: .06));
  }
}

/// Same row layout as [SettingsTile], with a trailing switch instead of the chevron.
class SettingsSwitchTile extends StatelessWidget {
  const SettingsSwitchTile({
    super.key,
    required this.icon,
    required this.title,
    required this.value,
    required this.onChanged,
    this.iconBackgroundColor,
  });

  final IconData icon;
  final String title;
  final bool value;
  final ValueChanged<bool> onChanged;
  final Color? iconBackgroundColor;

  @override
  Widget build(BuildContext context) {
    return Padding(
      padding: const EdgeInsets.symmetric(vertical: KasySpacing.sm),
      child: Row(
        children: <Widget>[
          if (iconBackgroundColor != null)
            SettingsIconBadge(icon: icon, color: iconBackgroundColor!)
          else
            Icon(icon, size: 21, color: context.colors.onSurface),
          const SizedBox(width: KasySpacing.sm),
          Expanded(
            child: Text(
              title,
              style: context.textTheme.titleMedium?.copyWith(
                color: context.colors.onSurface,
              ),
            ),
          ),
          Transform.scale(
            scale: 0.82,
            child: Switch.adaptive(
              value: value,
              onChanged: onChanged,
              materialTapTargetSize: MaterialTapTargetSize.shrinkWrap,
            ),
          ),
        ],
      ),
    );
  }
}

/// This widget is used to show a settings tile with an icon and a title
class SettingsTile extends StatelessWidget {
  final IconData icon;
  final String title;
  final SettingsTileOnTap onTap;
  final Color? iconBackgroundColor;

  const SettingsTile({
    super.key,
    required this.icon,
    required this.title,
    required this.onTap,
    this.iconBackgroundColor,
  });

  @override
  Widget build(BuildContext context) {
    return KasyHover(
      onTap: onTap,
      semanticLabel: title,
      padding: const EdgeInsets.symmetric(vertical: KasySpacing.sm),
      child: Row(
        children: [
          if (iconBackgroundColor != null)
            SettingsIconBadge(icon: icon, color: iconBackgroundColor!)
          else
            Icon(icon, size: 21, color: context.colors.onSurface),
          const SizedBox(width: KasySpacing.sm),
          Expanded(
            child: Text(
              title,
              style: context.textTheme.titleMedium?.copyWith(
                color: context.colors.onSurface,
              ),
            ),
          ),
          const SettingsListChevron(),
        ],
      ),
    );
  }
}
