import 'package:fis_jsonrpc/rpc.dart';
import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:vitalapp/pages/controllers/crowd_labels.dart';

class CrowdSelectLabelView extends StatefulWidget {
  final CrowdLabelsController controller;

  const CrowdSelectLabelView({super.key, required this.controller});
  @override
  State<StatefulWidget> createState() => _CrowdSelectLabelState();
}

class _CrowdSelectLabelState extends State<CrowdSelectLabelView> {
  late CrowdLabelsController controller;

  @override
  void initState() {
    super.initState();
    controller = widget.controller;
  }

  @override
  Widget build(BuildContext context) {
    final state = controller.state;
    return Obx(() => Wrap(
          spacing: 16,
          runSpacing: 12,
          children: [
            ...state.normalOptions
                .map((LabelDTO e) => buildItem(e, context))
                .toList(),
            ...state.diseaseOptions
                .map((LabelDTO e) => buildItem(e, context))
                .toList(),
            ...state.specialCareOptions
                .map((LabelDTO e) => buildItem(e, context))
                .toList(),
          ],
        ));
  }

  Widget buildItem(LabelDTO dto, BuildContext context) {
    return InkWell(
      child: VCheckBoxButton(
        label: dto.labelName ?? '',
        isChecked: controller.state.selectedCodes.contains(dto.code),
        onChanged: (value) {
          controller.onItemCheckChanged(dto.code!);
        },
      ),
    );
  }
}

/// 按钮式多选框
class VCheckBoxButton extends StatefulWidget {
  /// 文本
  final String label;

  /// 是否默认选中
  final bool? isChecked;

  /// 选中状态变更
  final ValueChanged<bool>? onChanged;

  const VCheckBoxButton({
    super.key,
    required this.label,
    this.isChecked,
    this.onChanged,
  });
  @override
  State<StatefulWidget> createState() => _VCheckBoxState();
}

class _VCheckBoxState extends State<VCheckBoxButton> {
  bool _isChecked = false;

  @override
  void initState() {
    if (widget.isChecked != null) {
      _isChecked = widget.isChecked!;
    }
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    const height = 40.0;
    const borderRadius = height / 3;
    final primaryColor = Theme.of(context).primaryColor;
    return Material(
      child: Ink(
        child: InkWell(
          borderRadius: BorderRadius.circular(borderRadius),
          onTap: () {
            setState(() {
              _isChecked = !_isChecked;
              widget.onChanged?.call(_isChecked);
            });
          },
          child: IntrinsicWidth(
            child: Container(
              padding: const EdgeInsets.only(left: 10, right: 10),
              constraints: const BoxConstraints(minWidth: 150),
              alignment: Alignment.center,
              height: height,
              decoration: BoxDecoration(
                color: _isChecked ? primaryColor : Colors.white,
                borderRadius: BorderRadius.circular(borderRadius),
                border: _isChecked ? null : Border.all(color: primaryColor),
              ),
              child: Row(
                mainAxisSize: MainAxisSize.min,
                crossAxisAlignment: CrossAxisAlignment.center,
                children: [
                  Text(
                    widget.label,
                    style: TextStyle(
                      color: _isChecked ? Colors.white : primaryColor,
                      fontSize: 16,
                    ),
                  ),
                ],
              ),
            ),
          ),
        ),
      ),
    );
  }
}
