import 'package:flutter/material.dart';
import 'package:flyinsono/lab/color/lab_colors.dart';
import 'package:fis_measure/interfaces/process/items/item_metas.dart';
import 'package:fis_measure/interfaces/process/items/terms.dart';
import 'package:fis_measure/view/measure/combo_widget.dart';
import 'package:fis_measure/process/language/measure_language.dart';
import 'package:get/get.dart';
import 'package:fis_measure/process/workspace/measure_data_controller.dart';
import 'custom_combo_items/custom_combo_items.dart';

class MeasureMultiItemsButton extends StatefulWidget {
  const MeasureMultiItemsButton({
    super.key,
    required this.isSelected,
    required this.description,
    required this.itemMeta,
    required this.activeChildItemIndex,
    this.onChangeChildItem,
    this.onTap,
  });

  final bool isSelected;
  final String description;
  final ValueChanged<int>? onChangeChildItem;
  final VoidCallback? onTap;
  final ItemMeta itemMeta;
  final int activeChildItemIndex;

  @override
  State<MeasureMultiItemsButton> createState() =>
      _MeasureMultiItemsButtonState();
}

class _MeasureMultiItemsButtonState extends State<MeasureMultiItemsButton> {
  bool _isHover = false;

  /// 测量语言包
  final measureLanguage = MeasureLanguage();
  final measureData = Get.find<MeasureDataController>();

  /// 触发子测量项选中事件
  void selectChildItem(int selectIndex) {
    if (selectIndex == widget.activeChildItemIndex) return;
    widget.onChangeChildItem?.call(selectIndex);
  }

  @override
  Widget build(BuildContext context) {
    final transValue = measureLanguage.t('measure', widget.description);
    final ifShowCN = measureData.measureSystemSetting.showAnnotation;

    final Color bgColor = widget.isSelected
        ? LabColors.base200
        : _isHover
            ? LabColors.base200
            : Colors.transparent;
    return Container(
      margin: EdgeInsets.only(bottom: 10),
      child: MouseRegion(
        cursor: SystemMouseCursors.click,
        onEnter: (_) {
          setState(() {
            _isHover = true;
          });
        },
        onExit: (_) {
          setState(() {
            _isHover = false;
          });
        },
        child: GestureDetector(
          onTap: widget.onTap,
          child: Container(
            decoration: BoxDecoration(
              color: bgColor,
              borderRadius: BorderRadius.circular(3),
              border: Border.all(
                color: LabColors.base400,
              ),
            ),
            child: Column(
              mainAxisSize: MainAxisSize.min,
              children: [
                Padding(
                  padding: EdgeInsets.symmetric(vertical: 5, horizontal: 5),
                  child: Container(
                    height: 46,
                    child: Column(
                      mainAxisAlignment: MainAxisAlignment.spaceEvenly,
                      children: [
                        if (ifShowCN)
                          Text(
                            transValue,
                            style: TextStyle(
                              fontSize: 14,
                              color: widget.isSelected
                                  ? LabColors.text200
                                  : LabColors.text700,
                            ),
                          ),
                        Text(
                          widget.description,
                          style: TextStyle(
                            fontSize: 16,
                            color: LabColors.text700,
                          ),
                        ),
                      ],
                    ),
                  ),
                ),
                if (widget.isSelected)
                  _buildChildItemContainer(widget.itemMeta),
              ],
            ),
          ),
        ),
      ),
    );
  }

  /// 子测量项容器
  Widget _buildChildItemContainer(ItemMeta itemMeta) {
    final activeName = itemMeta.description;
    final isSpecial = MeasurespecialsupportedTerms.items.contains(activeName);
    if (isSpecial) {
      //是否为特殊组合测量项
      return const SpecialItemHR();
    } else {
      final children = (itemMeta.childItems)
          .asMap()
          .entries
          .map(
            (e) => _buildChildItemButton(
              e.value,
              e.key,
            ),
          )
          .toList();
      if (LabCustomComboItemConfig.types.keys.contains(itemMeta.measureType)) {
        final itemNames = LabCustomComboItemConfig.types[itemMeta.measureType]!;
        for (var itemName in itemNames) {
          children.add(
            LabCustomComboItemButton(
              typeName: itemMeta.measureType,
              itemName: itemName,
            ),
          );
        }
      }
      return Container(
        decoration: const BoxDecoration(
          color: LabColors.base200,
          border: Border(
            top: BorderSide(
              color: LabColors.base400,
            ),
          ),
        ),
        padding: const EdgeInsets.symmetric(horizontal: 6, vertical: 5),
        child: Column(
          children: children,
        ),
      );
    }
  }

  // 构建子测量项按钮
  Widget _buildChildItemButton(ItemMeta itemMeta, int childItemIndex) {
    final isActived = childItemIndex == widget.activeChildItemIndex;
    return Container(
      width: double.infinity,
      margin: const EdgeInsetsDirectional.all(5),
      child: ElevatedButton(
        onPressed: () {
          selectChildItem(childItemIndex);
        },
        child: Text(
          itemMeta.description,
          style: TextStyle(
            color: isActived ? LabColors.text200 : LabColors.text700,
          ),
        ),
        style: ElevatedButton.styleFrom(
          elevation: 0,
          backgroundColor: isActived ? LabColors.base600 : LabColors.base200,
          side: BorderSide(
            color: isActived ? Colors.transparent : LabColors.buttonColor,
          ),
        ),
      ),
    );
  }
}
