import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';

/// WhatsApp share button widget
class WhatsAppShareButton extends StatelessWidget {
  final String label;
  final VoidCallback onPressed;
  final bool isLoading;
  final bool isSmall;
  final bool showIcon;

  const WhatsAppShareButton({
    super.key,
    required this.label,
    required this.onPressed,
    this.isLoading = false,
    this.isSmall = false,
    this.showIcon = true,
  });

  @override
  Widget build(BuildContext context) {
    return ElevatedButton.icon(
      onPressed: isLoading ? null : onPressed,
      icon: showIcon
          ? (isLoading
              ? SizedBox(
                  width: 20.w,
                  height: 20.h,
                  child: const CircularProgressIndicator(
                    strokeWidth: 2,
                    valueColor: AlwaysStoppedAnimation<Color>(Colors.white),
                  ),
                )
              : Icon(
                  Icons.send_rounded,
                  size: isSmall ? 18.w : 20.w,
                ))
          : const SizedBox(),
      label: Text(
        label,
        style: TextStyle(
          fontSize: isSmall ? 14.sp : 16.sp,
          fontWeight: FontWeight.w600,
        ),
      ),
      style: ElevatedButton.styleFrom(
        backgroundColor: const Color(0xFF25D366), // WhatsApp green
        foregroundColor: Colors.white,
        padding: EdgeInsets.symmetric(
          horizontal: isSmall ? 12.w : 20.w,
          vertical: isSmall ? 10.h : 14.h,
        ),
        shape: RoundedRectangleBorder(
          borderRadius: BorderRadius.circular(12.r),
        ),
        elevation: 2,
      ),
    );
  }
}

/// Compact WhatsApp icon button
class WhatsAppIconButton extends StatelessWidget {
  final VoidCallback onPressed;
  final String? tooltip;
  final double? size;

  const WhatsAppIconButton({
    super.key,
    required this.onPressed,
    this.tooltip,
    this.size,
  });

  @override
  Widget build(BuildContext context) {
    return IconButton(
      onPressed: onPressed,
      icon: Icon(
        Icons.send_rounded,
        color: const Color(0xFF25D366),
        size: size ?? 24.w,
      ),
      tooltip: tooltip ?? 'Share on WhatsApp',
      style: IconButton.styleFrom(
        backgroundColor: const Color(0xFF25D366).withOpacity(0.1),
        shape: RoundedRectangleBorder(
          borderRadius: BorderRadius.circular(8.r),
        ),
      ),
    );
  }
}

/// Floating WhatsApp action button (for quick contact)
class WhatsAppFloatingButton extends StatelessWidget {
  final VoidCallback onPressed;
  final String label;

  const WhatsAppFloatingButton({
    super.key,
    required this.onPressed,
    this.label = 'Contact',
  });

  @override
  Widget build(BuildContext context) {
    return FloatingActionButton.extended(
      onPressed: onPressed,
      icon: const Icon(Icons.send_rounded),
      label: Text(label),
      backgroundColor: const Color(0xFF25D366),
      foregroundColor: Colors.white,
    );
  }
}

/// WhatsApp share bottom sheet with options
class WhatsAppShareSheet extends StatelessWidget {
  final List<WhatsAppShareOption> options;

  const WhatsAppShareSheet({
    super.key,
    required this.options,
  });

  static Future<void> show(
    BuildContext context,
    List<WhatsAppShareOption> options,
  ) {
    return showModalBottomSheet(
      context: context,
      shape: RoundedRectangleBorder(
        borderRadius: BorderRadius.vertical(top: Radius.circular(24.r)),
      ),
      builder: (context) => WhatsAppShareSheet(options: options),
    );
  }

  @override
  Widget build(BuildContext context) {
    return Container(
      padding: EdgeInsets.all(24.w),
      child: Column(
        mainAxisSize: MainAxisSize.min,
        crossAxisAlignment: CrossAxisAlignment.start,
        children: [
          Row(
            children: [
              Container(
                padding: EdgeInsets.all(10.w),
                decoration: BoxDecoration(
                  color: const Color(0xFF25D366).withOpacity(0.1),
                  borderRadius: BorderRadius.circular(12.r),
                ),
                child: Icon(
                  Icons.send_rounded,
                  color: const Color(0xFF25D366),
                  size: 24.w,
                ),
              ),
              SizedBox(width: 12.w),
              Text(
                'Share via WhatsApp',
                style: TextStyle(
                  fontSize: 20.sp,
                  fontWeight: FontWeight.bold,
                ),
              ),
            ],
          ),
          SizedBox(height: 24.h),
          ...options.map((option) => _buildOption(context, option)),
          SizedBox(height: 8.h),
        ],
      ),
    );
  }

  Widget _buildOption(BuildContext context, WhatsAppShareOption option) {
    return ListTile(
      leading: Container(
        padding: EdgeInsets.all(10.w),
        decoration: BoxDecoration(
          color: option.color?.withOpacity(0.1) ?? Colors.grey.shade100,
          borderRadius: BorderRadius.circular(12.r),
        ),
        child: Icon(
          option.icon,
          color: option.color ?? Colors.grey.shade700,
          size: 24.w,
        ),
      ),
      title: Text(
        option.title,
        style: TextStyle(
          fontSize: 16.sp,
          fontWeight: FontWeight.w600,
        ),
      ),
      subtitle: option.subtitle != null
          ? Text(
              option.subtitle!,
              style: TextStyle(fontSize: 14.sp),
            )
          : null,
      trailing: Icon(
        Icons.arrow_forward_ios_rounded,
        size: 16.w,
        color: Colors.grey.shade400,
      ),
      onTap: () {
        Navigator.pop(context);
        option.onTap();
      },
      contentPadding: EdgeInsets.symmetric(vertical: 4.h),
    );
  }
}

/// WhatsApp share option data class
class WhatsAppShareOption {
  final String title;
  final String? subtitle;
  final IconData icon;
  final Color? color;
  final VoidCallback onTap;

  WhatsAppShareOption({
    required this.title,
    this.subtitle,
    required this.icon,
    this.color,
    required this.onTap,
  });
}

