import 'package:flutter/material.dart';

import 'package:im_app/core/ui/base/context_theme_ext.dart';

/// # AppButton — 按钮原子组件（L2 Component）
///
/// 四种命名构造器对应四种变体，loading 状态自动禁用点击。
/// 颜色和形状由 AppTheme 定义，AppButton 只做变体切换和 loading 逻辑。
///
/// ## 数据流位置
///
/// ```
/// View 层 Widget 树
///   → ★ AppButton.primary / .secondary / .text / .inverse ★ ← 你在这里
///     → ElevatedButton / OutlinedButton / TextButton / FilledButton
///       → AppTheme（颜色 / 形状已在 ThemeData 中定义）
/// ```
///
/// ## 使用
///
/// ```dart
/// // 主按钮（全宽，填充色）
/// AppButton.primary(label: '登录', onPressed: () => vm.login()),
///
/// // 加载状态（禁用点击，显示进度圈）
/// AppButton.primary(label: '登录', onPressed: null, isLoading: true),
///
/// // 副按钮（描边）
/// AppButton.secondary(label: '注册', onPressed: () {}),
///
/// // 文字按钮（非全宽）
/// AppButton.text(label: '忘记密码？', onPressed: () {}),
///
/// // 反色按钮：亮色模式黑底白字，暗色模式白底黑字
/// AppButton.inverse(
///   label: '切换 Tab',
///   icon: const Icon(Icons.swap_horiz),
///   onPressed: () {},
/// ),
/// ```
enum _ButtonVariant { primary, secondary, text, inverse }

class AppButton extends StatelessWidget {
  const AppButton.primary({
    super.key,
    required this.label,
    this.onPressed,
    this.isLoading = false,
    this.fullWidth = true,
  })  : _variant = _ButtonVariant.primary,
        icon = null;

  const AppButton.secondary({
    super.key,
    required this.label,
    this.onPressed,
    this.isLoading = false,
    this.fullWidth = true,
  })  : _variant = _ButtonVariant.secondary,
        icon = null;

  const AppButton.text({
    super.key,
    required this.label,
    this.onPressed,
    this.isLoading = false,
    this.fullWidth = false,
  })  : _variant = _ButtonVariant.text,
        icon = null;

  /// 反色按钮：颜色随明暗主题取反。
  ///
  /// 亮色模式：黑色背景 + 白色文字。
  /// 暗色模式：白色背景 + 黑色文字。
  ///
  /// 可选传 [icon]（`Icon` widget），自动切换为带图标布局。
  const AppButton.inverse({
    super.key,
    required this.label,
    this.onPressed,
    this.isLoading = false,
    this.fullWidth = false,
    this.icon,
  }) : _variant = _ButtonVariant.inverse;

  final String label;
  final VoidCallback? onPressed;
  final bool isLoading;
  final bool fullWidth;

  /// 仅 [AppButton.inverse] 使用，其余变体固定为 null
  final Widget? icon;

  final _ButtonVariant _variant;

  @override
  Widget build(BuildContext context) {
    final label = isLoading
        ? const SizedBox.square(
            dimension: 20,
            child: CircularProgressIndicator.adaptive(strokeWidth: 2),
          )
        : Text(this.label);

    final button = switch (_variant) {
      _ButtonVariant.primary =>
        ElevatedButton(onPressed: isLoading ? null : onPressed, child: label),
      _ButtonVariant.secondary =>
        OutlinedButton(onPressed: isLoading ? null : onPressed, child: label),
      _ButtonVariant.text =>
        TextButton(onPressed: isLoading ? null : onPressed, child: label),
      _ButtonVariant.inverse => _buildInverse(context, label),
    };

    return fullWidth ? SizedBox(width: double.infinity, child: button) : button;
  }

  Widget _buildInverse(BuildContext context, Widget label) {
    final c = context.colors;
    final style = FilledButton.styleFrom(
      backgroundColor: c.textPrimary,
      foregroundColor: c.bgSecondary,
    );
    if (icon != null) {
      return FilledButton.icon(
        onPressed: isLoading ? null : onPressed,
        style: style,
        icon: icon!,
        label: label,
      );
    }
    return FilledButton(
      onPressed: isLoading ? null : onPressed,
      style: style,
      child: label,
    );
  }
}
