import 'package:flutter/material.dart';
import '../../core/theme/app_colors.dart';
import '../../core/theme/app_colors_extension.dart';

/// Pencil Component/ActionBar — 底部操作栏
///
/// 水平排列三个按钮：重置（可选）、存草稿、提交。
/// 所有按钮圆角22，总高度72，padding 16，gap 12，背景bgCard。
class ActionBar extends StatelessWidget {
  final String? leftLabel;
  final String? centerLabel;
  final String? rightLabel;
  final VoidCallback? onLeftTap;
  final VoidCallback? onCenterTap;
  final VoidCallback? onRightTap;
  final bool showLeft;
  final bool showCenter;
  final bool showRight;
  final bool centerTextOnly;

  const ActionBar({
    super.key,
    this.leftLabel,
    this.centerLabel,
    this.rightLabel,
    this.onLeftTap,
    this.onCenterTap,
    this.onRightTap,
    this.showLeft = true,
    this.showCenter = true,
    this.showRight = true,
    this.centerTextOnly = false,
  });

  @override
  Widget build(BuildContext context) {
    final colors = Theme.of(context).extension<AppColorsExtension>()!;
    return Container(
      height: 72,
      padding: const EdgeInsets.symmetric(horizontal: 16, vertical: 16),
      decoration: BoxDecoration(
        color: colors.bgCard,
        border: Border(top: BorderSide(color: colors.border, width: 0.5)),
      ),
      child: Row(
        children: _buildButtons(colors),
      ),
    );
  }

  List<Widget> _buildButtons(AppColorsExtension colors) {
    final buttons = <Widget>[];

    void addGap() {
      if (buttons.isNotEmpty) buttons.add(const SizedBox(width: 12));
    }

    if (showLeft && leftLabel != null) {
      buttons.add(Expanded(
        child: _ActionButton(
          label: leftLabel!,
          backgroundColor: colors.bgCard,
          textColor: colors.textSecondary,
          onTap: onLeftTap,
        ),
      ));
    }

    if (showCenter && centerLabel != null) {
      addGap();
      buttons.add(Expanded(
        child: _ActionButton(
          label: centerLabel!,
          backgroundColor: centerTextOnly ? colors.bgCard : colors.primaryLight,
          textColor: centerTextOnly ? colors.textSecondary : colors.primary,
          onTap: onCenterTap,
        ),
      ));
    }

    if (showRight && rightLabel != null) {
      addGap();
      buttons.add(Expanded(
        child: _ActionButton(
          label: rightLabel!,
          backgroundColor: colors.primary,
          textColor: colors.bgCard,
          onTap: onRightTap,
        ),
      ));
    }

    return buttons;
  }
}

class _ActionButton extends StatelessWidget {
  final String label;
  final Color backgroundColor;
  final Color textColor;
  final VoidCallback? onTap;

  const _ActionButton({
    required this.label,
    required this.backgroundColor,
    required this.textColor,
    this.onTap,
  });

  @override
  Widget build(BuildContext context) {
    return SizedBox(
      height: 40,
      child: Material(
        color: backgroundColor,
        borderRadius: BorderRadius.circular(22),
        child: InkWell(
          onTap: onTap,
          borderRadius: BorderRadius.circular(22),
          child: Center(
            child: Text(
              label,
              style: TextStyle(
                fontSize: AppFontSizes.body,
                fontWeight: FontWeight.w500,
                color: textColor,
              ),
            ),
          ),
        ),
      ),
    );
  }
}
