// ignore: must_be_immutable

// ignore_for_file: must_be_immutable

import 'package:fis_i18n/i18n.dart';
import 'package:fis_jsonrpc/rpc.dart';
import 'package:fis_measure/process/language/measure_language.dart';
import 'package:fis_measure/process/workspace/measure_data_controller.dart';
import 'package:fis_measure/view/measure/measure_config/measure_configuation_page.dart';
import 'package:fis_ui/index.dart';
import 'package:fis_ui/interface/interactive_container.dart';
import 'package:flutter/material.dart';
import 'package:flyinsono/lab/color/lab_colors.dart';
import 'package:get/get.dart';

class SelectModule extends FStatefulWidget implements FInteractiveContainer {
  final List<ItemMetaGroup> itemMetaListGroup;

  /// 选中的测量项
  List<String> chooseMeasureList = [];

  @override
  final String pageName = 'SelectModule';
  SelectModule({
    super.key,
    required this.itemMetaListGroup,
    required this.chooseMeasureList,
  });
  @override
  FState<SelectModule> createState() => _SelectModulePageState();
}

class _SelectModulePageState extends FState<SelectModule> {
  /// 数据
  final measureData = Get.find<MeasureDataController>();
  List<ItemMetaGroup> groupList = [];

  /// 测量语言包
  final measureLanguage = MeasureLanguage();

  /// 新增测量项目
  ///
  /// [chooseMeasure] 选中的测量项
  void addChooseMeasure(String chooseMeasure) {
    widget.chooseMeasureList.add(
      chooseMeasure,
    );
    setState(() {});
  }

  /// 移除测量项目
  ///
  /// [chooseMeasure] 移除的测量项
  void removeChooseMeasure(String chooseMeasure) {
    widget.chooseMeasureList.remove(
      chooseMeasure,
    );
    setState(() {});
  }

  @override
  void initState() {
    widget.itemMetaListGroup.forEach((element) {
      if (element.itemMeta.length != 0) {
        groupList.add(element);
      }
    });
    super.initState();
  }

  @override
  FWidget build(BuildContext context) {
    return FColumn(
      children: groupList
          .map(
            (e) => FColumn(
              crossAxisAlignment: CrossAxisAlignment.start,
              children: _buildMeasureConfiguration(e),
            ),
          )
          .toList(),
    );
  }

  List<FWidget> _buildMeasureConfiguration(ItemMetaGroup e) {
    return [
      FInkWell(
        onTap: () {
          e.isExpend = !e.isExpend!;
          setState(() {});
        },
        child: FContainer(
          padding: const EdgeInsets.all(15),
          child: _buildGroupTitleRow(
            e.itemMetaGroupTitle.toString(),
            e.isExpend!,
          ),
        ),
      ),
      e.isExpend! ? _buildGroupBody(e) : FContainer(),
    ];
  }

  FWidget _buildGroupTitleRow(
    String itemMetaGroupTitle,
    bool isExpend,
  ) {
    return FRow(
      mainAxisAlignment: MainAxisAlignment.spaceBetween,
      children: [
        FRow(
          children: [
            FText(
              itemMetaGroupTitle,
              style: TextStyle(
                color: LabColors.text900,
              ),
            ),
            FText(
              measureLanguage.t('measure', itemMetaGroupTitle),
              style: TextStyle(
                color: LabColors.text900,
              ),
            ),
          ],
        ),
        isExpend
            ? const FIcon(
                Icons.keyboard_arrow_down_sharp,
                color: LabColors.text900,
              )
            : const FIcon(
                Icons.keyboard_arrow_right,
                color: LabColors.text900,
              ),
      ],
    );
  }

  /// 选中的测量项
  FWidget _buildSlected(ItemMetaDTO e) {
    return FGestureDetector(
      name: "${e.name}",
      businessParent: widget,
      onTap: () {
        removeChooseMeasure(
          e.description ?? '',
        );
      },
      child: FContainer(
        width: 180,
        height: 65,
        color: LabColors.selectMeasureBgColor,
        child: FCenter(
          child: FColumn(
            mainAxisAlignment: MainAxisAlignment.center,
            children: [
              if (!i18nBook.isCurrentEnglish)
                FText(
                  measureLanguage.t('measure', e.description ?? ''),
                  style: const TextStyle(
                    color: LabColors.text100,
                    fontSize: 14,
                  ),
                ),
              FText(
                e.description ?? '',
                style: const TextStyle(
                  color: LabColors.text100,
                ),
              ),
            ],
          ),
        ),
        // ),
      ),
    );
  }

  /// 未选中的测量项
  FWidget _buildNoSlected(ItemMetaDTO e) {
    return FGestureDetector(
      name: "${e.name}",
      businessParent: widget,
      onTap: () {
        addChooseMeasure(
          e.description ?? '',
        );
      },
      behavior: HitTestBehavior.translucent,
      child: FContainer(
        width: 180,
        height: 65,
        decoration: BoxDecoration(
          border: Border.all(
            width: 0.5,
            color: LabColors.base400,
          ),
        ),
        child: FCenter(
          child: FColumn(
            mainAxisAlignment: MainAxisAlignment.center,
            children: [
              if (!i18nBook.isCurrentEnglish)
                FPadding(
                  padding: const EdgeInsets.symmetric(horizontal: 5),
                  child: FText(
                    measureLanguage.t('measure', e.description ?? ''),
                    textAlign: TextAlign.center,
                    style: const TextStyle(
                      color: LabColors.text900,
                      fontSize: 14,
                    ),
                  ),
                ),
              FText(
                e.description ?? '',
                style: const TextStyle(
                  color: LabColors.text900,
                ),
              ),
            ],
          ),
        ),
      ),
    );
  }

  FWidget _buildGroupBody(ItemMetaGroup e) {
    return FContainer(
      padding: const EdgeInsets.only(left: 15),
      margin: const EdgeInsets.symmetric(vertical: 10),
      child: FWrap(
          spacing: 16.0, // 主轴(水平)方向间距
          runSpacing: 8.0, // 纵轴（垂直）方向间距
          direction: Axis.horizontal,
          crossAlignment: WrapCrossAlignment.center,
          alignment: WrapAlignment.start,
          children: e.itemMeta.map((e) {
            if (widget.chooseMeasureList.contains(
              e.description,
            )) {
              return FMouseRegion(
                cursor: SystemMouseCursors.click,
                child: _buildSlected(e),
              );
            } else {
              return FMouseRegion(
                cursor: SystemMouseCursors.click,
                child: _buildNoSlected(e),
              );
            }
          }).toList()),
    );
  }
}
