import 'package:fis_measure/define.dart';
import 'package:fis_measure/interfaces/process/items/item_metas.dart';
import 'package:fis_measure/interfaces/process/workspace/application.dart';
import 'package:fis_measure/process/primitives/rvsp.dart';
import 'package:fis_measure/utils/prompt_box.dart';
import 'package:fis_ui/index.dart';
import 'package:flutter/material.dart';
import 'package:flyinsono/lab/color/lab_colors.dart';
import 'package:flyinsono/lab/pages/lab_record_info/widgets/alert_dialog.dart';
import 'package:get/get.dart';

import 'widgets.dart';
import 'normal_child.dart';

class RapItemButton extends FStatefulWidget {
  const RapItemButton({super.key});

  @override
  FState<FStatefulWidget> createState() => _RapItemButtonState();
}

class _RapItemButtonState extends FState<RapItemButton> {
  static const _options = <double>[double.nan, 5, 10, 15];
  late final RxInt _activeIndex;
  double _customRapValue = 0;

  late final TextEditingController _inputTextController;

  @override
  void initState() {
    _inputTextController =
        TextEditingController(text: _customRapValue.toString());
    _initState();
    super.initState();
  }

  @override
  FWidget build(BuildContext context) {
    return NormalChildButton(
      businessParent: const FPlaceHolderInteractiveContainer(),
      activeIndex: -1,
      index: 0,
      itemMeta: ItemMeta(
        "RAP",
        measureType: '',
        description: 'RAP',
        outputs: [],
      ),
      onClick: () {
        Get.dialog(
          _buildDialog(),
          barrierColor: LabColors.base100.withOpacity(0.1),
        );
      },
    );
  }

  void _initState() {
    final activeMeasureItem = Get.find<IApplication>().activeMeasureItem;
    if (activeMeasureItem == null) {
      _activeIndex = RxInt(0);
      return;
    }
    final rap = (activeMeasureItem as Rvsp).rap.value;
    if (rap == null) {
      _activeIndex = RxInt(0);
      return;
    }
    final index = _options.indexWhere((e) => e == rap);
    if (index > 0) {
      _activeIndex = RxInt(index);
    } else {
      _customRapValue = rap;
      _activeIndex = RxInt(0);
    }
  }

  Widget _buildDialog() {
    return Stack(
      children: [
        QuickFWidget(
          ModalBarrier(
            color: Colors.black.withOpacity(0.5),
            onDismiss: () {},
          ),
        ),
        LabAlertDialog(
          maxWidth: 400,
          content: Container(
            height: 320,
            child: Obx(() {
              final children = <Widget>[];
              for (var i = 0; i < _options.length; i++) {
                if (i == 0) {
                  children.add(_buildInputButton());
                } else {
                  children.add(const CustomChildOptionDivider());
                  children.add(_buildValueButton(i));
                }
              }
              return Column(children: children);
            }),
          ),
          onConfirm: () {
            _onValueConfrim();
            Get.back();
          },
          title: 'RAP',
        )
      ],
    );
  }

  void _onValueConfrim() {
    final activeMeasureItem = Get.find<IApplication>().activeMeasureItem;
    if (activeMeasureItem == null) {
      return;
    }

    double rap;
    if (_activeIndex.value == 0) {
      rap = _customRapValue;
    } else {
      rap = _options[_activeIndex.value];
    }

    (activeMeasureItem as Rvsp).updateRap(rap);
  }

  Widget _buildValueButton(int index) {
    final isActive = _activeIndex.value == index;
    final value = _options[index];
    return CustomChildTextOptionWidget(
      isActive: isActive,
      text: "$value mmHg",
      onClick: () {
        _activeIndex.value = index;
      },
    );
  }

  Widget _buildInputButton() {
    const index = 0;
    final isActive = _activeIndex.value == index;
    return CustomChildInputOptionWidget(
      isActive: isActive,
      value: _customRapValue.toString(),
      textController: _inputTextController,
      suffixWidget: const Text(
        " mmHg",
        style: TextStyle(color: LabColors.text200),
      ),
      onClick: () {
        _activeIndex.value = index;
      },
      onInputChanged: (String value) {
        final doubleValue = double.tryParse(value);
        if (doubleValue == null) {
          PromptBox.toast("请输入正确的RAP数值");
        } else {
          if (doubleValue < 0) {
            PromptBox.toast("RAP必须大于0");
          } else {
            _customRapValue = doubleValue;
          }
        }
      },
    );
  }
}
