import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:superport/models/address_model.dart';
import 'package:superport/models/company_model.dart';
import 'package:superport/screens/common/custom_widgets.dart';
import 'package:superport/screens/common/theme_tailwind.dart';
import 'package:superport/screens/common/widgets/address_input.dart';
import 'package:superport/screens/company/widgets/contact_info_widget.dart';
import 'package:superport/utils/validators.dart';
import 'package:superport/utils/phone_utils.dart';

class BranchCard extends StatefulWidget {
  final GlobalKey cardKey;
  final int index;
  final Branch branch;
  final TextEditingController nameController;
  final TextEditingController contactNameController;
  final TextEditingController contactPositionController;
  final TextEditingController contactPhoneController;
  final TextEditingController contactEmailController;
  final FocusNode focusNode;
  final List<String> positions;
  final List<String> phonePrefixes;
  final String selectedPhonePrefix;
  final ValueChanged<String> onNameChanged;
  final ValueChanged<Address> onAddressChanged;
  final ValueChanged<String> onContactNameChanged;
  final ValueChanged<String> onContactPositionChanged;
  final ValueChanged<String> onContactPhoneChanged;
  final ValueChanged<String> onContactEmailChanged;
  final ValueChanged<String> onPhonePrefixChanged;
  final VoidCallback onDelete;

  const BranchCard({
    Key? key,
    required this.cardKey,
    required this.index,
    required this.branch,
    required this.nameController,
    required this.contactNameController,
    required this.contactPositionController,
    required this.contactPhoneController,
    required this.contactEmailController,
    required this.focusNode,
    required this.positions,
    required this.phonePrefixes,
    required this.selectedPhonePrefix,
    required this.onNameChanged,
    required this.onAddressChanged,
    required this.onContactNameChanged,
    required this.onContactPositionChanged,
    required this.onContactPhoneChanged,
    required this.onContactEmailChanged,
    required this.onPhonePrefixChanged,
    required this.onDelete,
  }) : super(key: key);

  @override
  _BranchCardState createState() => _BranchCardState();
}

class _BranchCardState extends State<BranchCard> {
  @override
  Widget build(BuildContext context) {
    return GestureDetector(
      onTap: () {
        // 화면의 빈 공간 터치 시 포커스 해제
        FocusScope.of(context).unfocus();
      },
      child: Card(
        key: widget.cardKey,
        margin: const EdgeInsets.only(bottom: 16.0),
        elevation: 2,
        child: Padding(
          padding: const EdgeInsets.all(16.0),
          child: Column(
            crossAxisAlignment: CrossAxisAlignment.start,
            children: [
              Row(
                mainAxisAlignment: MainAxisAlignment.spaceBetween,
                children: [
                  Text(
                    '지점 #${widget.index + 1}',
                    style: AppThemeTailwind.subheadingStyle,
                  ),
                  IconButton(
                    icon: const Icon(Icons.delete, color: Colors.red),
                    onPressed: widget.onDelete,
                  ),
                ],
              ),
              const SizedBox(height: 8),
              FormFieldWrapper(
                label: '지점명',
                isRequired: true,
                child: TextFormField(
                  controller: widget.nameController,
                  focusNode: widget.focusNode,
                  decoration: const InputDecoration(hintText: '지점명을 입력하세요'),
                  onChanged: widget.onNameChanged,
                  validator: FormValidator.required('지점명은 필수입니다'),
                ),
              ),
              AddressInput(
                initialZipCode: widget.branch.address.zipCode,
                initialRegion: widget.branch.address.region,
                initialDetailAddress: widget.branch.address.detailAddress,
                onAddressChanged: (zipCode, region, detailAddress) {
                  final address = Address(
                    zipCode: zipCode,
                    region: region,
                    detailAddress: detailAddress,
                  );
                  widget.onAddressChanged(address);
                },
              ),

              // 담당자 정보 - ContactInfoWidget 사용
              ContactInfoWidget(
                title: '담당자 정보',
                contactNameController: widget.contactNameController,
                contactPositionController: widget.contactPositionController,
                contactPhoneController: widget.contactPhoneController,
                contactEmailController: widget.contactEmailController,
                positions: widget.positions,
                selectedPhonePrefix: widget.selectedPhonePrefix,
                phonePrefixes: widget.phonePrefixes,
                onPhonePrefixChanged: widget.onPhonePrefixChanged,
                onContactNameChanged: widget.onContactNameChanged,
                onContactPositionChanged: widget.onContactPositionChanged,
                onContactPhoneChanged: widget.onContactPhoneChanged,
                onContactEmailChanged: widget.onContactEmailChanged,
                compactMode: false, // compactMode를 false로 변경하여 한 줄로 표시
              ),
            ],
          ),
        ),
      ),
    );
  }
}
