import 'package:flutter/material.dart';
import 'package:qr_flutter/qr_flutter.dart';
import 'base_field.dart';

class QRCodeField extends BaseField {
  final String fieldKey;
  final String label;
  final String hint;
  final bool isRequired;
  final Future<String?> Function()? scanner; // returns scanned text

  QRCodeField({
    required this.fieldKey,
    required this.label,
    this.hint = '',
    this.isRequired = false,
    this.scanner,
  });

  @override
  String? Function(String?)? get validator => (v) {
        if (isRequired && (v == null || v.trim().isEmpty)) return 'Required';
        return null;
      };

  @override
  Widget buildField({
    required TextEditingController controller,
    required ColorScheme colorScheme,
    VoidCallback? onChanged,
  }) {
    return Column(
      crossAxisAlignment: CrossAxisAlignment.start,
      children: [
        Text(label,
            style: TextStyle(
                color: colorScheme.onSurface, fontWeight: FontWeight.w600)),
        const SizedBox(height: 8),
        Container(
          width: double.infinity,
          padding: const EdgeInsets.all(12),
          decoration: BoxDecoration(
            color: colorScheme.surface,
            borderRadius: BorderRadius.circular(12),
            border:
                Border.all(color: colorScheme.outlineVariant.withOpacity(0.3)),
          ),
          child: Column(
            children: [
              if (controller.text.trim().isNotEmpty) ...[
                QrImageView(
                  data: controller.text.trim(),
                  version: QrVersions.auto,
                  size: 160,
                  gapless: false,
                ),
                const SizedBox(height: 12),
              ],
              Row(
                children: [
                  Expanded(
                    child: TextFormField(
                      controller: controller,
                      decoration: InputDecoration(
                        labelText:
                            hint.isEmpty ? 'Enter or scan QR data' : hint,
                        border: OutlineInputBorder(
                            borderRadius: BorderRadius.circular(10)),
                        filled: true,
                      ),
                      validator: validator,
                      onChanged: (_) => onChanged?.call(),
                    ),
                  ),
                  const SizedBox(width: 8),
                  IconButton(
                    tooltip: 'Scan QR',
                    icon: Icon(Icons.qr_code_scanner_rounded,
                        color: colorScheme.primary),
                    onPressed: scanner == null
                        ? null
                        : () async {
                            final val = await scanner!.call();
                            if (val != null) {
                              controller.text = val;
                              onChanged?.call();
                            }
                          },
                  ),
                ],
              ),
            ],
          ),
        ),
      ],
    );
  }
}
