import 'dart:math';

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

class ExamNumberInput extends StatefulWidget {
  const ExamNumberInput({
    super.key,
    required this.currentFormObject,
    required this.currentInputValue,
    this.commonInput,
    this.specialInput,
  });
  final FormObject currentFormObject;
  final String currentInputValue;
  final Function? commonInput;
  final Function(String value)? specialInput;

  @override
  State<ExamNumberInput> createState() => _ExamNumberInputState();
}

class _ExamNumberInputState extends State<ExamNumberInput> {
  TextEditingController specialInputController = TextEditingController();
  @override
  Widget build(BuildContext context) {
    return ExamCard(
      title: widget.currentFormObject.label ?? '',
      clickCard: () {
        widget.commonInput?.call();
      },
      content: Container(
        alignment: Alignment.bottomRight,
        padding: const EdgeInsets.only(
          bottom: 20,
          right: 30,
          left: 40,
        ),
        constraints: const BoxConstraints(minHeight: 150),
        child: FittedBox(
          child: Row(
            mainAxisAlignment: MainAxisAlignment.end,
            crossAxisAlignment: CrossAxisAlignment.end,
            children: [
              RichText(
                text: TextSpan(
                  text: widget.currentInputValue,
                  style: const TextStyle(
                    fontSize: 80,
                    color: Colors.black,
                  ),
                  children: [
                    TextSpan(
                      text: widget.currentFormObject.append ?? '',
                      style: const TextStyle(fontSize: 25),
                    )
                  ],
                ),
              ),
            ],
          ),
        ),
      ),
    );
  }

  double generateRandomNumber() {
    Random random = Random();
    double randomNumber = 35.8 + random.nextDouble() * (38 - 35.8);
    return double.parse(randomNumber.toStringAsFixed(2));
  }

  void _buildSpecialInput(FormObject currentFormObject) {
    Future.delayed(const Duration(milliseconds: 3000), () {
      specialInputController.text = generateRandomNumber().toString();
      widget.specialInput?.call(specialInputController.text);
      setState(() {});
    });
    Get.dialog(VAlertDialog(
      title: currentFormObject.label ?? '',
      width: 600,
      contentPadding: const EdgeInsets.symmetric(vertical: 12, horizontal: 24),
      content: Row(
        children: [
          Container(
            width: 400,
            padding: const EdgeInsets.only(left: 15),
            child: TextFormField(
              keyboardType: TextInputType.number,
              style: const TextStyle(
                fontSize: 100, // 设置字体大小
              ),
              showCursor: false,
              controller: specialInputController,
              decoration: const InputDecoration(
                labelStyle: TextStyle(
                  fontSize: 100, // 设置标签的字体大小
                ),
              ),
            ),
          ),
          specialInputController.text == ''
              ? Expanded(
                  child: Row(children: const [
                    Expanded(
                      child: Text(
                        '测量中',
                        style: TextStyle(
                          fontSize: 40,
                          color: Colors.blue,
                        ),
                      ),
                    ),
                    CircularProgressIndicator(
                      valueColor: AlwaysStoppedAnimation(
                        Colors.blue,
                      ),
                    ),
                    SizedBox(
                      width: 20,
                    ),
                  ]),
                )
              : TextButton(
                  onPressed: () {
                    specialInputController.text = '';
                    Future.delayed(const Duration(milliseconds: 3000), () {
                      specialInputController.text =
                          generateRandomNumber().toString();

                      setState(() {});
                    });
                  },
                  child: Container(
                    padding: const EdgeInsets.symmetric(horizontal: 30),
                    decoration: const BoxDecoration(
                      borderRadius: BorderRadius.all(
                        Radius.circular(
                          30,
                        ),
                      ),
                      color: Colors.blue,
                    ),
                    child: const Text(
                      '测量',
                      style: TextStyle(fontSize: 40, color: Colors.white),
                    ),
                  ),
                ),
        ],
      ),
      showCancel: false,
      // onConfirm: () {
      //   // Get.back(result: controller.text);
      // },
    ));
  }
}
