import 'package:flutter/material.dart';
import 'package:vitalapp/components/dialog_input.dart';
import 'package:vitalapp/pages/check/models/form.dart';
import 'package:vitalapp/pages/check/widgets/exam_configurable/exam_card.dart';

class ExamToxicSubstance extends StatelessWidget {
  const ExamToxicSubstance({
    Key? key,
    required this.currentFormObject,
    required this.options,
    required this.selectRadioChange,
    this.currentSelectedToxicSubstance,
    required this.selectValueChange,
    required this.currentValue,
  }) : super(key: key);

  final FormObject currentFormObject;
  final List<Option> options;
  final Function selectRadioChange;
  final Map? currentSelectedToxicSubstance;
  final Function selectValueChange;
  final String currentValue;

  @override
  Widget build(BuildContext context) {
    return ExamCard(
      title: currentFormObject.label ?? '',
      content: Column(
        children: [
          _buildTextField(),
          _buildProtectionMeasuresText(),
          _buildOptionsRow(),
        ],
      ),
    );
  }

  Widget _buildTextField() {
    return Container(
      padding: const EdgeInsets.all(32).copyWith(top: 0),
      child: TextField(
        readOnly: true,
        controller: TextEditingController(text: currentValue),
        style: const TextStyle(fontSize: 30),
        onTap: () async {
          final result = await _showInputDialog(
            title: currentFormObject.label ?? '',
            initialValue: currentValue,
          );
          selectValueChange.call(result);
        },
      ),
    );
  }

  Widget _buildProtectionMeasuresText() {
    return Container(
      alignment: Alignment.centerLeft,
      padding: const EdgeInsets.only(left: 32, bottom: 12),
      child: const Text(
        '防护措施：',
        style: TextStyle(fontSize: 25),
      ),
    );
  }

  Widget _buildOptionsRow() {
    final selectedValue = currentSelectedToxicSubstance?['value']?['value'];

    return Container(
      padding: const EdgeInsets.symmetric(horizontal: 16),
      width: double.infinity,
      child: Row(
        children: [
          Wrap(
            children: options
                .map(
                  (e) => Container(
                    padding: const EdgeInsets.all(8),
                    child: InkWell(
                      onTap: () {
                        final toxicSubstance = ToxicSubstance(
                          value: e.toJson(),
                        );
                        selectRadioChange(toxicSubstance.toJson());
                      },
                      borderRadius: BorderRadius.circular(50),
                      child: Ink(
                        decoration: BoxDecoration(
                          border: Border.all(
                            color: selectedValue == e.value
                                ? Colors.blue
                                : Colors.black26,
                          ),
                          borderRadius: const BorderRadius.all(
                            Radius.circular(50),
                          ),
                          color: selectedValue == e.value
                              ? Colors.blue
                              : Colors.transparent,
                        ),
                        child: Container(
                          padding: const EdgeInsets.all(15),
                          alignment: Alignment.center,
                          width: 250,
                          child: FittedBox(
                            child: Text(
                              e.label ?? '',
                              style: TextStyle(
                                fontSize: 20,
                                color: selectedValue == e.value
                                    ? Colors.white
                                    : Colors.black54,
                              ),
                            ),
                          ),
                        ),
                      ),
                    ),
                  ),
                )
                .toList(),
          ),
          if (selectedValue == '2') _buildProtectionMeasureTextField(),
        ],
      ),
    );
  }

  Widget _buildProtectionMeasureTextField() {
    return Container(
      child: Row(
        children: [
          const SizedBox(width: 16),
          SizedBox(
            width: 200,
            child: TextField(
              readOnly: true,
              controller: TextEditingController(
                text: currentSelectedToxicSubstance?['label'],
              ),
              style: const TextStyle(fontSize: 30),
              onTap: () async {
                final result = await _showInputDialog(
                  title: '防护措施',
                  initialValue: currentSelectedToxicSubstance?['label'] ?? '',
                );
                final toxicSubstance = ToxicSubstance(
                  value: Option(label: '有', value: '2').toJson(),
                  label: result,
                );
                selectRadioChange(toxicSubstance.toJson());
              },
            ),
          )
        ],
      ),
    );
  }

  Future<String?> _showInputDialog({
    required String title,
    required String initialValue,
  }) async {
    return await VDialogInput(
      title: title,
      initialValue: initialValue,
    ).show();
  }
}
