import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:hyper_local/config/theme.dart';
import '../../../config/helper.dart';
import '../../../l10n/app_localizations.dart';

class DeliveryMethodSelectorWidget extends StatelessWidget {
  final String selectedMethod;
  final bool isPickupEnabled;
  final ValueChanged<String> onMethodChanged;

  const DeliveryMethodSelectorWidget({
    super.key,
    required this.selectedMethod,
    this.isPickupEnabled = true,
    required this.onMethodChanged,
  });

  @override
  Widget build(BuildContext context) {
    final l10n = AppLocalizations.of(context);

    final isPickup = selectedMethod == 'pickup';
    final isDelivery = selectedMethod == 'delivery';

    return Container(
      padding: EdgeInsets.only(
        left: 12.0.w,
        right: 12.0.w,
        top: 12.h,
        bottom: 12.h,
      ),
      decoration: BoxDecoration(
        color: Theme.of(context).colorScheme.surface,
        borderRadius: BorderRadius.circular(16.r),
      ),
      child: Column(
        crossAxisAlignment: CrossAxisAlignment.start,
        children: [
          Text(
            l10n?.deliveryType ?? 'Delivery Type',
            style: TextStyle(
              fontSize: isTablet(context) ? 24 : 15.sp,
              fontWeight: FontWeight.w600,
            ),
          ),
          SizedBox(height: 12.h),
          Row(
            children: [
              Expanded(
                child: _buildMethodCard(
                  context: context,
                  isSelected: isPickup,
                  label: l10n?.pickup ?? 'Pickup',
                  icon: isPickupEnabled ? Icons.storefront_outlined : Icons.lock_outline,
                  isEnabled: isPickupEnabled,
                  onTap: () => onMethodChanged('pickup'),
                ),
              ),
              SizedBox(width: 12.w),
              Expanded(
                child: _buildMethodCard(
                  context: context,
                  isSelected: isDelivery,
                  label: l10n?.delivery ?? 'Delivery',
                  icon: Icons.local_shipping_outlined,
                  onTap: () => onMethodChanged('delivery'),
                ),
              ),
            ],
          ),
        ],
      ),
    );
  }

  Widget _buildMethodCard({
    required BuildContext context,
    required bool isSelected,
    required String label,
    required IconData icon,
    bool isEnabled = true,
    required VoidCallback onTap,
  }) {
    const primaryColor = AppTheme.primaryColor;
    
    return Opacity(
      opacity: isEnabled ? 1.0 : 0.4,
      child: InkWell(
        onTap: isEnabled ? onTap : null,
        borderRadius: BorderRadius.circular(12.r),
        child: AnimatedContainer(
          duration: const Duration(milliseconds: 200),
          padding: EdgeInsets.symmetric(vertical: 16.h, horizontal: 8.w),
          decoration: BoxDecoration(
            borderRadius: BorderRadius.circular(12.r),
            border: isSelected
                ? Border.all(color: primaryColor, width: 1.5)
                : Border.all(color: Theme.of(context).colorScheme.outline),
            color: isSelected
                ? primaryColor.withValues(alpha: 0.08)
                : Colors.transparent,
          ),
          child: Column(
            mainAxisSize: MainAxisSize.min,
            mainAxisAlignment: MainAxisAlignment.center,
            children: [
              Icon(
                icon,
                size: isTablet(context) ? 36 : 28.sp,
                color: isSelected ? primaryColor : Colors.grey[600],
              ),
              SizedBox(height: 8.h),
              Text(
                label,
                style: TextStyle(
                  fontSize: isTablet(context) ? 18 : 13.sp,
                  fontWeight: isSelected ? FontWeight.bold : FontWeight.w500,
                  color: isSelected ? primaryColor : Colors.grey[700],
                ),
              ),
            ],
          ),
        ),
      ),
    );
  }
}
