import 'package:flutter/material.dart';
import 'package:vitalapp/consts/styles.dart';
import 'package:vitalapp/pages/check/models/form.dart';
import 'package:vitalapp/pages/check/widgets/exam_configurable/exam_card.dart';
import 'package:vitalapp/pages/check/widgets/exam_title.dart';

class ExamCheckBox extends StatelessWidget {
  static final _borderRadius = BorderRadius.circular(16.0);

  const ExamCheckBox({
    super.key,
    required this.currentFormObject,
    required this.options,
    required this.selectCheckBoxChange,
    required this.currentSelectedCheckBox,
    this.disbaleOthers = false,
  });
  final FormObject currentFormObject;
  final List<Option> options;
  final Function selectCheckBoxChange;
  final List<dynamic> currentSelectedCheckBox;

  /// 禁用其他选项(除已选)
  final bool disbaleOthers;

  @override
  Widget build(BuildContext context) {
    String label = currentFormObject.label ?? '';
    return ExamCard(
      titleText: ExamTitle(
        label: label,
        titleType: '（多选）',
      ),
      title: label,
      content: Center(
        child: Wrap(
          children: options
              .map(
                (e) => _buildItem(context, e),
              )
              .toList(),
        ),
      ),
    );
  }

  Widget _buildItem(BuildContext context, Option e) {
    Color bgColor;
    Color txtColor;
    Color borderColor;
    if (currentSelectedCheckBox.contains(e.value)) {
      // 已选中
      final primaryColor = Theme.of(context).primaryColor;
      bgColor = primaryColor;
      txtColor = Colors.white;
      borderColor = primaryColor;
    } else {
      // 非选中的
      if (disbaleOthers) {
        // 含有独占选项时
        bgColor = Colors.grey.shade400;
        txtColor = Colors.white;
        borderColor = Colors.grey.shade400;
      } else {
        // 不含有独占选项时
        bgColor = Colors.transparent;
        txtColor = Colors.black54;
        borderColor = Colors.black26;
      }
    }

    return Container(
      padding: const EdgeInsets.all(8),
      child: InkWell(
        onTap: () => selectCheckBoxChange(e),
        borderRadius: _borderRadius,
        child: Ink(
          decoration: BoxDecoration(
            border: Border.all(
              color: borderColor,
            ),
            borderRadius: _borderRadius,
            color: bgColor,
          ),
          child: Container(
            padding: const EdgeInsets.all(15),
            alignment: Alignment.center,
            width: 250,
            child: FittedBox(
              child: Text(
                e.label ?? '',
                style: TextStyle(
                  fontSize: 20,
                  color: txtColor,
                ),
              ),
            ),
          ),
        ),
      ),
    );
  }
}
