import 'package:flutter/material.dart';
import 'package:superport/core/services/lookups_service.dart';
import 'package:get_it/get_it.dart';
import 'package:superport/core/utils/debug_logger.dart';

/// 3단계 카테고리 연동 선택 위젯
/// 
/// 대분류 → 중분류 → 소분류 순서로 연동 선택
/// - 상위 선택 시 하위 자동 로딩
/// - 직접입력과 드롭다운 선택 모두 지원
/// - 백엔드 API 조합 데이터 기반
class CategoryCascadeFormField extends StatefulWidget {
  final String? category1;
  final String? category2;
  final String? category3;
  final void Function(String?, String?, String?) onChanged;
  final bool enabled;
  final String? Function(String?)? category1Validator;
  final String? Function(String?)? category2Validator;
  final String? Function(String?)? category3Validator;

  const CategoryCascadeFormField({
    super.key,
    this.category1,
    this.category2,
    this.category3,
    required this.onChanged,
    this.enabled = true,
    this.category1Validator,
    this.category2Validator,
    this.category3Validator,
  });

  @override
  State<CategoryCascadeFormField> createState() => _CategoryCascadeFormFieldState();
}

class _CategoryCascadeFormFieldState extends State<CategoryCascadeFormField> {
  final LookupsService _lookupsService = GetIt.instance<LookupsService>();
  
  late TextEditingController _category1Controller;
  late TextEditingController _category2Controller;
  late TextEditingController _category3Controller;
  
  List<String> _category1Options = [];
  List<String> _category2Options = [];
  List<String> _category3Options = [];
  
  bool _isLoadingCategory2 = false;
  bool _isLoadingCategory3 = false;

  @override
  void initState() {
    super.initState();
    
    _category1Controller = TextEditingController(text: widget.category1 ?? '');
    _category2Controller = TextEditingController(text: widget.category2 ?? '');
    _category3Controller = TextEditingController(text: widget.category3 ?? '');
    
    _loadCategory1Options();
    
    // 초기값이 있는 경우 하위 카테고리 로딩
    if (widget.category1 != null && widget.category1!.isNotEmpty) {
      _loadCategory2Options(widget.category1!);
      
      if (widget.category2 != null && widget.category2!.isNotEmpty) {
        _loadCategory3Options(widget.category1!, widget.category2!);
      }
    }
  }

  @override
  void didUpdateWidget(CategoryCascadeFormField oldWidget) {
    super.didUpdateWidget(oldWidget);
    
    // 외부에서 값이 변경된 경우 컨트롤러 업데이트
    if (widget.category1 != oldWidget.category1) {
      _category1Controller.text = widget.category1 ?? '';
    }
    if (widget.category2 != oldWidget.category2) {
      _category2Controller.text = widget.category2 ?? '';
    }
    if (widget.category3 != oldWidget.category3) {
      _category3Controller.text = widget.category3 ?? '';
    }
  }

  @override
  void dispose() {
    _category1Controller.dispose();
    _category2Controller.dispose();
    _category3Controller.dispose();
    super.dispose();
  }

  Future<void> _loadCategory1Options() async {
    try {
      final result = await _lookupsService.getCategory1List();
      result.fold(
        (failure) {
          DebugLogger.logError('대분류 로딩 실패', error: failure.message);
          if (mounted) {
            setState(() {
              _category1Options = [];
            });
          }
        },
        (categories) {
          if (mounted) {
            setState(() {
              _category1Options = categories;
            });
          }
        },
      );
    } catch (e) {
      DebugLogger.logError('대분류 로딩 예외', error: e);
      if (mounted) {
        setState(() {
          _category1Options = [];
        });
      }
    }
  }

  Future<void> _loadCategory2Options(String category1) async {
    if (category1.isEmpty) {
      setState(() {
        _category2Options = [];
      });
      return;
    }

    setState(() {
      _isLoadingCategory2 = true;
    });

    try {
      final result = await _lookupsService.getCategory2List(category1);
      result.fold(
        (failure) {
          DebugLogger.logError('중분류 로딩 실패', error: failure.message);
          if (mounted) {
            setState(() {
              _category2Options = [];
              _isLoadingCategory2 = false;
            });
          }
        },
        (categories) {
          if (mounted) {
            setState(() {
              _category2Options = categories;
              _isLoadingCategory2 = false;
            });
          }
        },
      );
    } catch (e) {
      DebugLogger.logError('중분류 로딩 예외', error: e);
      if (mounted) {
        setState(() {
          _category2Options = [];
          _isLoadingCategory2 = false;
        });
      }
    }
  }

  Future<void> _loadCategory3Options(String category1, String category2) async {
    if (category1.isEmpty || category2.isEmpty) {
      setState(() {
        _category3Options = [];
      });
      return;
    }

    setState(() {
      _isLoadingCategory3 = true;
    });

    try {
      final result = await _lookupsService.getCategory3List(category1, category2);
      result.fold(
        (failure) {
          DebugLogger.logError('소분류 로딩 실패', error: failure.message);
          if (mounted) {
            setState(() {
              _category3Options = [];
              _isLoadingCategory3 = false;
            });
          }
        },
        (categories) {
          if (mounted) {
            setState(() {
              _category3Options = categories;
              _isLoadingCategory3 = false;
            });
          }
        },
      );
    } catch (e) {
      DebugLogger.logError('소분류 로딩 예외', error: e);
      if (mounted) {
        setState(() {
          _category3Options = [];
          _isLoadingCategory3 = false;
        });
      }
    }
  }

  void _onCategory1Changed(String? value) {
    // 대분류 변경 시 중분류, 소분류 초기화
    _category2Controller.clear();
    _category3Controller.clear();
    _category2Options.clear();
    _category3Options.clear();
    
    // 새로운 대분류에 대한 중분류 로딩
    if (value != null && value.isNotEmpty) {
      _loadCategory2Options(value);
    }
    
    // 변경 알림
    widget.onChanged(value, null, null);
  }

  void _onCategory2Changed(String? value) {
    // 중분류 변경 시 소분류 초기화
    _category3Controller.clear();
    _category3Options.clear();
    
    // 새로운 중분류에 대한 소분류 로딩
    final category1 = _category1Controller.text;
    if (category1.isNotEmpty && value != null && value.isNotEmpty) {
      _loadCategory3Options(category1, value);
    }
    
    // 변경 알림
    widget.onChanged(_category1Controller.text, value, null);
  }

  void _onCategory3Changed(String? value) {
    // 변경 알림
    widget.onChanged(
      _category1Controller.text, 
      _category2Controller.text, 
      value,
    );
  }

  Widget _buildComboBox({
    required String labelText,
    required TextEditingController controller,
    required List<String> options,
    required void Function(String?) onChanged,
    String? Function(String?)? validator,
    bool isLoading = false,
  }) {
    // 드롭다운 선택 가능한 값 (현재 텍스트가 옵션에 있으면 선택)
    String? selectedValue;
    if (controller.text.isNotEmpty && options.contains(controller.text)) {
      selectedValue = controller.text;
    }

    return Column(
      crossAxisAlignment: CrossAxisAlignment.start,
      children: [
        // 드롭다운 선택
        if (options.isNotEmpty && !isLoading)
          DropdownButtonFormField<String>(
            value: selectedValue,
            items: options.map((option) {
              return DropdownMenuItem<String>(
                value: option,
                child: Text(option),
              );
            }).toList(),
            onChanged: widget.enabled ? (value) {
              if (value != null) {
                controller.text = value;
                onChanged(value);
              }
            } : null,
            decoration: InputDecoration(
              labelText: '$labelText (선택)',
              hintText: '$labelText를 선택하세요',
              border: const OutlineInputBorder(),
              enabledBorder: OutlineInputBorder(
                borderSide: BorderSide(color: Colors.grey.shade400),
              ),
              focusedBorder: OutlineInputBorder(
                borderSide: BorderSide(color: Theme.of(context).primaryColor),
              ),
            ),
            validator: validator,
          ),
        
        // 로딩 표시
        if (isLoading)
          Container(
            height: 56,
            decoration: BoxDecoration(
              border: Border.all(color: Colors.grey.shade400),
              borderRadius: BorderRadius.circular(4),
            ),
            child: const Center(
              child: Row(
                mainAxisAlignment: MainAxisAlignment.center,
                children: [
                  SizedBox(
                    width: 20,
                    height: 20,
                    child: CircularProgressIndicator(strokeWidth: 2),
                  ),
                  SizedBox(width: 8),
                  Text('로딩 중...'),
                ],
              ),
            ),
          ),

        // 옵션이 없을 때
        if (options.isEmpty && !isLoading)
          TextFormField(
            controller: controller,
            enabled: widget.enabled,
            validator: validator,
            onChanged: onChanged,
            decoration: InputDecoration(
              labelText: '$labelText (직접입력)',
              hintText: '$labelText를 직접 입력하세요',
              border: const OutlineInputBorder(),
              enabledBorder: OutlineInputBorder(
                borderSide: BorderSide(color: Colors.grey.shade400),
              ),
              focusedBorder: OutlineInputBorder(
                borderSide: BorderSide(color: Theme.of(context).primaryColor),
              ),
            ),
          ),
      ],
    );
  }

  @override
  Widget build(BuildContext context) {
    return Column(
      children: [
        // 대분류
        _buildComboBox(
          labelText: '대분류',
          controller: _category1Controller,
          options: _category1Options,
          onChanged: _onCategory1Changed,
          validator: widget.category1Validator,
        ),
        const SizedBox(height: 16),
        
        // 중분류
        _buildComboBox(
          labelText: '중분류',
          controller: _category2Controller,
          options: _category2Options,
          onChanged: _onCategory2Changed,
          validator: widget.category2Validator,
          isLoading: _isLoadingCategory2,
        ),
        const SizedBox(height: 16),
        
        // 소분류
        _buildComboBox(
          labelText: '소분류',
          controller: _category3Controller,
          options: _category3Options,
          onChanged: _onCategory3Changed,
          validator: widget.category3Validator,
          isLoading: _isLoadingCategory3,
        ),
      ],
    );
  }
}