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

class CountryPrefixPicker extends StatelessWidget {
  const CountryPrefixPicker({
    super.key,
    required this.onChanged,
    required this.headerText,
    this.initialCountryCode = '+34',
    this.radius = 12,
    this.width = 90,
    this.height = 55,
    this.borderColor = const Color(0xFF4B4B4B),
    this.backgroundColor = Colors.white,
  });

  final ValueChanged<CountryCode> onChanged;
  final String initialCountryCode;
  final String headerText;
  final double radius;
  final double width;
  final double height;
  final Color borderColor;
  final Color backgroundColor;

  @override
  Widget build(BuildContext context) {
    return SizedBox(
      width: width,
      height: height,
      child: CountryCodePicker(
        showCountryOnly: true,
        showOnlyCountryWhenClosed: true,
        showDropDownButton: true,
        headerText: headerText,
        onChanged: onChanged,
        initialSelection: initialCountryCode,
        showFlag: true,
        padding: EdgeInsets.zero,
        builder: (CountryCode? country) {
          if (country == null) {
            return const SizedBox.shrink();
          }

          return InputDecorator(
            decoration: InputDecoration(
              isDense: false,
              contentPadding: const EdgeInsets.symmetric(
                horizontal: 12,
                vertical: 16,
              ),
              filled: true,
              fillColor: backgroundColor,
              enabledBorder: OutlineInputBorder(
                borderRadius: BorderRadius.all(Radius.circular(radius)),
                borderSide: BorderSide(color: borderColor),
              ),
              border: OutlineInputBorder(
                borderRadius: BorderRadius.all(Radius.circular(radius)),
                borderSide: BorderSide(color: borderColor),
              ),
            ),
            child: Row(
              mainAxisAlignment: MainAxisAlignment.spaceBetween,
              children: [
                if (country.flagUri != null)
                  Image.asset(
                    country.flagUri!,
                    package: 'country_code_picker',
                    width: 24,
                    height: 24,
                    fit: BoxFit.cover,
                  ),
                const Icon(Icons.arrow_drop_down, size: 24),
              ],
            ),
          );
        },
      ),
    );
  }
}
