import 'package:flutter/material.dart';
import '../../../theme/app_colors.dart';
import '../../../l10n/app_localizations.dart';

/// 결제 주기 선택 위젯
/// 월간, 분기별, 반기별, 연간 중 선택할 수 있습니다.
class BillingCycleSelector extends StatelessWidget {
  final String billingCycle;
  final ValueChanged<String> onChanged;
  final Color? baseColor;
  final List<Color>? gradientColors;
  final bool isGlassmorphism;

  const BillingCycleSelector({
    super.key,
    required this.billingCycle,
    required this.onChanged,
    this.baseColor,
    this.gradientColors,
    this.isGlassmorphism = false,
  });

  @override
  Widget build(BuildContext context) {
    final localization = AppLocalizations.of(context);
    // 상세 화면에서는 '매월', 추가 화면에서는 '월간'으로 표시
    final cycles = isGlassmorphism
        ? [
            localization.billingCycleMonthly,
            localization.billingCycleQuarterly,
            localization.billingCycleHalfYearly,
            localization.billingCycleYearly,
          ]
        : [
            localization.monthly,
            localization.billingCycleQuarterly,
            localization.billingCycleHalfYearly,
            localization.yearly,
          ];

    return SingleChildScrollView(
      scrollDirection: Axis.horizontal,
      child: Row(
        children: cycles.map((cycle) {
          final isSelected = billingCycle == cycle;
          return Padding(
            padding: const EdgeInsets.only(right: 8),
            child: InkWell(
              onTap: () => onChanged(cycle),
              borderRadius: BorderRadius.circular(12),
              child: Container(
                padding: const EdgeInsets.symmetric(
                  horizontal: 20,
                  vertical: 12,
                ),
                decoration: BoxDecoration(
                  color: _getBackgroundColor(isSelected),
                  borderRadius: BorderRadius.circular(12),
                  border: _getBorder(isSelected),
                ),
                child: Text(
                  cycle,
                  style: TextStyle(
                    fontSize: 14,
                    fontWeight: FontWeight.w500,
                    color: _getTextColor(isSelected),
                  ),
                ),
              ),
            ),
          );
        }).toList(),
      ),
    );
  }

  Color _getBackgroundColor(bool isSelected) {
    if (!isSelected) {
      return isGlassmorphism
          ? AppColors.backgroundColor
          : Colors.grey.withValues(alpha: 0.1);
    }

    if (baseColor != null) {
      return baseColor!;
    }

    if (gradientColors != null && gradientColors!.isNotEmpty) {
      return gradientColors![0];
    }

    return const Color(0xFF3B82F6);
  }

  Border? _getBorder(bool isSelected) {
    if (isSelected || !isGlassmorphism) {
      return null;
    }
    return Border.all(
      color: AppColors.borderColor.withValues(alpha: 0.5),
      width: 1.5,
    );
  }

  Color _getTextColor(bool isSelected) {
    if (isSelected) {
      return Colors.white;
    }
    return isGlassmorphism ? AppColors.darkNavy : Colors.grey[700]!;
  }
}
