import 'package:fis_ui/index.dart';
import 'package:fis_ui/interface/interactive_container.dart';
import 'package:flutter/material.dart';
import 'package:fis_measure/values/unit_desc.dart';
import 'package:fis_measure/view/cursor.dart';
import 'package:fis_theme/theme.dart';
import 'package:fis_jsonrpc/services/remedical.m.dart';
import 'package:flyinsono/lab/color/lab_colors.dart';

import '../measure_configuration_style.dart';
import 'common.dart';

class PatternItemRadio extends FStatelessWidget {
  final List<dynamic> tabList;
  final PatternValueType? type;
  final dynamic value;
  final ValueCallback onChangeState;

  ///父级节点
  final FInteractiveContainer businessParent;
  const PatternItemRadio({
    Key? key,
    required this.businessParent,
    required this.tabList,
    required this.value,
    required this.onChangeState,
    this.type = PatternValueType.size,
  }) : super(key: key);

  @override
  FWidget build(BuildContext context) {
    return FContainer(
      margin: const EdgeInsets.only(left: 10),
      child: FWrap(
        spacing: 8.0, // 主轴(水平)方向间距
        runSpacing: 8.0, // 纵轴（垂直）方向间距
        direction: Axis.horizontal,
        crossAlignment: WrapCrossAlignment.center,
        alignment: WrapAlignment.start,
        children: List<FWidget>.generate(
          tabList.length,
          (index) {
            FWidget child = const FText("");
            switch (type) {
              case PatternValueType.size:
                child = FText(
                  MeasureTextSize(tabList[index]).toString(),
                  style: tabList[index] != value
                      ? const TextStyle(
                          color: Colors.black,
                        )
                      : const TextStyle(
                          color: Colors.white,
                        ),
                );
                break;
              case PatternValueType.unit:
                if (tabList is List<Unit>) {
                  child = FText(
                    UnitDescriptionMap.getDescByInt(tabList[index].index),
                    style: tabList[index] != value
                        ? const TextStyle(
                            color: Colors.black,
                          )
                        : const TextStyle(
                            color: Colors.white,
                          ),
                  );
                }
                break;
              case PatternValueType.cursor:
                child = MeasureCursor(
                  size: 10,
                  type: tabList[index],
                  color: tabList[index] != value ? Colors.black : Colors.white,
                );
                break;
              default:
                child = const FText("");
            }
            var tab = tabList[index];
            return FElevatedButton(
              name: "$tab",
              businessParent: businessParent,
              onPressed: () {
                if (tabList[index] != value) onChangeState.call(tabList[index]);
              },
              style: ButtonStyle(
                backgroundColor: tabList[index] != value
                    ? MaterialStateProperty.all(
                        Colors.white,
                      )
                    : MaterialStateProperty.all(
                        LabColors.base400,
                      ),
              ),
              child: FContainer(
                width: getIconButtonWidth(type),
                height: 20,
                child: FCenter(
                  child: child,
                ),
              ),
            );
          },
        ),
      ),
    );
  }

  double getIconButtonWidth(PatternValueType? type) {
    switch (type) {
      case PatternValueType.size:
        return 50;
      case PatternValueType.unit:
        return 46;
      case PatternValueType.cursor:
        return 20;
      default:
        return 50;
    }
  }
}
