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

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

  BarcodeField({
    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) ...[
                BarcodeWidget(
                  barcode: Barcode.code128(),
                  data: controller.text.trim(),
                  width: 220,
                  height: 80,
                ),
                const SizedBox(height: 12),
              ],
              Row(
                children: [
                  Expanded(
                    child: TextFormField(
                      controller: controller,
                      decoration: InputDecoration(
                        labelText:
                            hint.isEmpty ? 'Enter or scan barcode' : hint,
                        border: OutlineInputBorder(
                            borderRadius: BorderRadius.circular(10)),
                        filled: true,
                      ),
                      validator: validator,
                      onChanged: (_) => onChanged?.call(),
                    ),
                  ),
                  const SizedBox(width: 8),
                  IconButton(
                    tooltip: 'Scan Barcode',
                    icon: Icon(Icons.document_scanner_rounded,
                        color: colorScheme.primary),
                    onPressed: scanner == null
                        ? null
                        : () async {
                            final val = await scanner!.call();
                            if (val != null) {
                              controller.text = val;
                              onChanged?.call();
                            }
                          },
                  ),
                ],
              ),
            ],
          ),
        ),
      ],
    );
  }
}
