import 'package:flutter/material.dart';
import 'package:superport/screens/common/custom_widgets/form_field_wrapper.dart';
import 'package:superport/screens/equipment/controllers/equipment_in_form_controller.dart';
import 'custom_dropdown_field.dart';

/// 장비 기본 정보 섹션 위젯
class EquipmentBasicInfoSection extends StatelessWidget {
  final EquipmentInFormController controller;
  final TextEditingController partnerController;
  final TextEditingController warehouseController;
  final TextEditingController manufacturerController;
  final TextEditingController equipmentNameController;
  final FocusNode partnerFocusNode;
  final FocusNode warehouseFocusNode;
  final FocusNode manufacturerFocusNode;
  final FocusNode nameFieldFocusNode;
  final LayerLink partnerLayerLink;
  final LayerLink warehouseLayerLink;
  final LayerLink manufacturerLayerLink;
  final LayerLink equipmentNameLayerLink;
  final GlobalKey partnerFieldKey;
  final GlobalKey warehouseFieldKey;
  final GlobalKey manufacturerFieldKey;
  final GlobalKey equipmentNameFieldKey;
  final VoidCallback onPartnerDropdownPressed;
  final VoidCallback onWarehouseDropdownPressed;
  final VoidCallback onManufacturerDropdownPressed;
  final VoidCallback onEquipmentNameDropdownPressed;
  final String? Function(String) getPartnerAutocompleteSuggestion;
  final String? Function(String) getWarehouseAutocompleteSuggestion;
  final String? Function(String) getManufacturerAutocompleteSuggestion;
  final String? Function(String) getEquipmentNameAutocompleteSuggestion;

  const EquipmentBasicInfoSection({
    super.key,
    required this.controller,
    required this.partnerController,
    required this.warehouseController,
    required this.manufacturerController,
    required this.equipmentNameController,
    required this.partnerFocusNode,
    required this.warehouseFocusNode,
    required this.manufacturerFocusNode,
    required this.nameFieldFocusNode,
    required this.partnerLayerLink,
    required this.warehouseLayerLink,
    required this.manufacturerLayerLink,
    required this.equipmentNameLayerLink,
    required this.partnerFieldKey,
    required this.warehouseFieldKey,
    required this.manufacturerFieldKey,
    required this.equipmentNameFieldKey,
    required this.onPartnerDropdownPressed,
    required this.onWarehouseDropdownPressed,
    required this.onManufacturerDropdownPressed,
    required this.onEquipmentNameDropdownPressed,
    required this.getPartnerAutocompleteSuggestion,
    required this.getWarehouseAutocompleteSuggestion,
    required this.getManufacturerAutocompleteSuggestion,
    required this.getEquipmentNameAutocompleteSuggestion,
  });

  @override
  Widget build(BuildContext context) {
    return Column(
      crossAxisAlignment: CrossAxisAlignment.start,
      children: [
        // 섹션 제목
        Padding(
          padding: const EdgeInsets.only(bottom: 16.0),
          child: Text(
            '기본 정보',
            style: TextStyle(
              fontSize: 18,
              fontWeight: FontWeight.bold,
              color: Theme.of(context).primaryColor,
            ),
          ),
        ),
        // 1행: 구매처, 입고지
        Row(
          children: [
            Expanded(
              child: FormFieldWrapper(
                label: '구매처',
                isRequired: true,
                child: CustomDropdownField(
                  label: '구매처',
                  hint: '구매처를 입력 또는 선택하세요',
                  required: true,
                  controller: partnerController,
                  focusNode: partnerFocusNode,
                  items: controller.partnerCompanies,
                  onChanged: (value) {
                    controller.partnerCompany = value;
                  },
                  onFieldSubmitted: (value) {
                    final suggestion = getPartnerAutocompleteSuggestion(value);
                    if (suggestion != null && suggestion.length > value.length) {
                      partnerController.text = suggestion;
                      controller.partnerCompany = suggestion;
                      partnerController.selection = TextSelection.collapsed(
                        offset: suggestion.length,
                      );
                    }
                  },
                  getAutocompleteSuggestion: getPartnerAutocompleteSuggestion,
                  onDropdownPressed: onPartnerDropdownPressed,
                  layerLink: partnerLayerLink,
                  fieldKey: partnerFieldKey,
                ),
              ),
            ),
            const SizedBox(width: 16),
            Expanded(
              child: FormFieldWrapper(
                label: '입고지',
                isRequired: true,
                child: CustomDropdownField(
                  label: '입고지',
                  hint: '입고지를 입력 또는 선택하세요',
                  required: true,
                  controller: warehouseController,
                  focusNode: warehouseFocusNode,
                  items: controller.warehouseLocations,
                  onChanged: (value) {
                    controller.warehouseLocation = value;
                  },
                  onFieldSubmitted: (value) {
                    final suggestion = getWarehouseAutocompleteSuggestion(value);
                    if (suggestion != null && suggestion.length > value.length) {
                      warehouseController.text = suggestion;
                      controller.warehouseLocation = suggestion;
                      warehouseController.selection = TextSelection.collapsed(
                        offset: suggestion.length,
                      );
                    }
                  },
                  getAutocompleteSuggestion: getWarehouseAutocompleteSuggestion,
                  onDropdownPressed: onWarehouseDropdownPressed,
                  layerLink: warehouseLayerLink,
                  fieldKey: warehouseFieldKey,
                ),
              ),
            ),
          ],
        ),
        const SizedBox(height: 16),
        // 2행: 제조사, 장비명
        Row(
          children: [
            Expanded(
              child: FormFieldWrapper(
                label: '제조사',
                isRequired: true,
                child: CustomDropdownField(
                  label: '제조사',
                  hint: '제조사를 입력 또는 선택하세요',
                  required: true,
                  controller: manufacturerController,
                  focusNode: manufacturerFocusNode,
                  items: controller.manufacturers,
                  onChanged: (value) {
                    controller.manufacturer = value;
                  },
                  onFieldSubmitted: (value) {
                    final suggestion = getManufacturerAutocompleteSuggestion(value);
                    if (suggestion != null && suggestion.length > value.length) {
                      manufacturerController.text = suggestion;
                      controller.manufacturer = suggestion;
                      manufacturerController.selection = TextSelection.collapsed(
                        offset: suggestion.length,
                      );
                    }
                  },
                  getAutocompleteSuggestion: getManufacturerAutocompleteSuggestion,
                  onDropdownPressed: onManufacturerDropdownPressed,
                  layerLink: manufacturerLayerLink,
                  fieldKey: manufacturerFieldKey,
                ),
              ),
            ),
            const SizedBox(width: 16),
            Expanded(
              child: FormFieldWrapper(
                label: '장비명',
                isRequired: true,
                child: CustomDropdownField(
                  label: '장비명',
                  hint: '장비명을 입력 또는 선택하세요',
                  required: true,
                  controller: equipmentNameController,
                  focusNode: nameFieldFocusNode,
                  items: controller.equipmentNames,
                  onChanged: (value) {
                    // Equipment name은 model 선택으로 자동 설정됨
                    // controller.name = value;
                  },
                  onFieldSubmitted: (value) {
                    final suggestion = getEquipmentNameAutocompleteSuggestion(value);
                    if (suggestion != null && suggestion.length > value.length) {
                      equipmentNameController.text = suggestion;
                      // controller.name = suggestion;
                      equipmentNameController.selection = TextSelection.collapsed(
                        offset: suggestion.length,
                      );
                    }
                  },
                  getAutocompleteSuggestion: getEquipmentNameAutocompleteSuggestion,
                  onDropdownPressed: onEquipmentNameDropdownPressed,
                  layerLink: equipmentNameLayerLink,
                  fieldKey: equipmentNameFieldKey,
                ),
              ),
            ),
          ],
        ),
      ],
    );
  }
}