import 'package:flutter/material.dart';
import 'package:flyinsono/architecture/services/modules/config_language.dart';
import 'package:flyinsono/lab/color/lab_colors.dart';
import 'package:fis_theme/theme.dart';
import 'package:get/get.dart';

class ValueSelectorField extends StatefulWidget {
  ValueSelectorField({
    super.key,
    required this.intialValue,
    required this.dataSource,
    this.label = "Label",
    this.hintText = "",
    this.editable = true,
    this.onChanged,
    this.bgColor = Colors.transparent,
    this.bgColorOnHover = LabColors.base300,
  });

  final String label;
  final String hintText;
  final String intialValue;
  final List<String> dataSource;
  final bool editable;
  final ValueChanged<String>? onChanged;
  final Color bgColor;
  final Color bgColorOnHover;

  @override
  State<ValueSelectorField> createState() => _ValueSelectorFieldState();
}

class _ValueSelectorFieldState extends State<ValueSelectorField> {
  bool _isHover = false;
  final GlobalKey _dropdownButtonKey = GlobalKey();

  /// 语言服务
  final languageService = Get.find<LanguageService>();
  @override
  void initState() {
    super.initState();
  }

  /// 获取翻译值
  String getValues(String code) {
    var value = languageService.findLanguageValueByCode('exam', code);
    return value;
  }

  // 打开下拉框
  void openDropdown() {
    GestureDetector? detector;
    void searchForGestureDetector(BuildContext element) {
      element.visitChildElements((element) {
        if (element.widget is GestureDetector) {
          detector = element.widget as GestureDetector;
          return;
        } else {
          searchForGestureDetector(element);
        }
        return;
      });
    }

    searchForGestureDetector(_dropdownButtonKey.currentContext!);
    assert(detector != null);
    detector?.onTap?.call();
  }

  @override
  Widget build(BuildContext context) {
    return Padding(
      padding: const EdgeInsets.all(5.0),
      child: GestureDetector(
        onTap: _handleTap,
        child: MouseRegion(
          cursor: SystemMouseCursors.click,
          onEnter: _handleMouseEnter,
          onExit: _handleMouseExit,
          child: Container(
            padding: EdgeInsets.all(5),
            decoration: BoxDecoration(
              color: _isHover ? widget.bgColorOnHover : widget.bgColor,
              borderRadius: BorderRadius.circular(5),
            ),
            child: Column(
              crossAxisAlignment: CrossAxisAlignment.start,
              children: [
                Text(
                  widget.label,
                  style: TextStyle(
                    fontSize: 14,
                    color: LabColors.text600,
                  ),
                ),
                SizedBox(height: 2),
                SizedBox(
                  height: 26,
                  child: DropdownButton<String>(
                    key: _dropdownButtonKey,
                    underline: SizedBox(),
                    value: widget.intialValue,
                    // itemHeight: 50,
                    dropdownColor: LabColors.base100,
                    icon: Icon(
                      Icons.keyboard_arrow_down_rounded,
                      color: LabColors.base800,
                    ),
                    style: TextStyle(
                      fontSize: 18,
                      color: LabColors.text800,
                      fontFamily: FTheme.ins.localeSetting.fontFamily,
                    ),
                    isExpanded: true,
                    elevation: 1,
                    items: widget.dataSource.map((String dataSource) {
                      return DropdownMenuItem<String>(
                        value: dataSource,
                        child: Text(
                          getValues(
                            dataSource,
                          ),
                          overflow: TextOverflow.ellipsis,
                          maxLines: 1,
                        ),
                      );
                    }).toList(),
                    onChanged: (String? value) {
                      if (value != null) {
                        widget.onChanged?.call(value);
                      }
                    },
                  ),
                ),
                Container(
                  height: 2,
                  color: LabColors.base600,
                ),
              ],
            ),
          ),
        ),
      ),
    );
  }

  void _handleMouseEnter(PointerEvent details) {
    setState(() {
      _isHover = true;
    });
  }

  void _handleMouseExit(PointerEvent details) {
    setState(() {
      _isHover = false;
    });
  }

  // 如果没聚焦，点击后聚焦
  void _handleTap() {
    openDropdown();
  }
}
