import 'package:flutter/material.dart';
import 'package:superport/models/company_model.dart';
import 'package:superport/models/address_model.dart';

/// 회사 폼에서 사용되는 지점 관리 컨트롤러
/// 각 지점의 정보를 개별적으로 관리
class BranchFormController {
  Branch _branch;
  final List<String> positions;
  final List<String> phonePrefixes;
  
  // 컨트롤러들
  final TextEditingController nameController = TextEditingController();
  final TextEditingController contactNameController = TextEditingController();
  final TextEditingController contactPositionController = TextEditingController();
  final TextEditingController contactPhoneController = TextEditingController();
  final TextEditingController contactEmailController = TextEditingController();
  final TextEditingController remarkController = TextEditingController();
  final FocusNode focusNode = FocusNode();
  
  // 전화번호 관련
  String selectedPhonePrefix = '010';
  
  BranchFormController({
    required Branch branch,
    required this.positions,
    required this.phonePrefixes,
  }) : _branch = branch {
    // 초기값 설정
    nameController.text = branch.name;
    contactNameController.text = branch.contactName ?? '';
    contactPositionController.text = branch.contactPosition ?? '';
    contactPhoneController.text = branch.contactPhone ?? '';
    contactEmailController.text = branch.contactEmail ?? '';
    remarkController.text = branch.remark ?? '';
    
    // 전화번호 접두사 추출
    if (branch.contactPhone != null && branch.contactPhone!.isNotEmpty) {
      for (String prefix in phonePrefixes) {
        if (branch.contactPhone!.startsWith(prefix)) {
          selectedPhonePrefix = prefix;
          contactPhoneController.text = branch.contactPhone!.substring(prefix.length);
          break;
        }
      }
    }
  }
  
  /// Branch 객체 getter
  Branch get branch => _branch.copyWith(
    name: nameController.text.trim(),
    contactName: contactNameController.text.trim().isEmpty ? null : contactNameController.text.trim(),
    contactPosition: contactPositionController.text.trim().isEmpty ? null : contactPositionController.text.trim(),
    contactPhone: contactPhoneController.text.trim().isEmpty ? null : '$selectedPhonePrefix${contactPhoneController.text.trim()}',
    contactEmail: contactEmailController.text.trim().isEmpty ? null : contactEmailController.text.trim(),
    remark: remarkController.text.trim().isEmpty ? null : remarkController.text.trim(),
  );
  
  /// 주소 업데이트
  void updateAddress(Address address) {
    _branch = _branch.copyWith(address: address);
  }
  
  /// 필드 업데이트
  void updateField(String field, String value) {
    switch (field) {
      case 'name':
        nameController.text = value;
        break;
      case 'contactName':
        contactNameController.text = value;
        break;
      case 'contactPosition':
        contactPositionController.text = value;
        break;
      case 'contactPhone':
        contactPhoneController.text = value;
        break;
      case 'contactEmail':
        contactEmailController.text = value;
        break;
      case 'remark':
        remarkController.text = value;
        break;
    }
  }
  
  /// 리소스 해제
  void dispose() {
    nameController.dispose();
    contactNameController.dispose();
    contactPositionController.dispose();
    contactPhoneController.dispose();
    contactEmailController.dispose();
    remarkController.dispose();
    focusNode.dispose();
  }
}