import 'package:fis_i18n/i18n.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/combos/pisa.dart';
import 'package:fis_measure/utils/prompt_box.dart';
import 'package:fis_ui/index.dart';
import 'package:flutter/material.dart';
import 'package:flyinsono/architecture/define.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 'normal_child.dart';
import 'widgets.dart';

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

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

class _AlsVelItemButtonState extends FState<AlsVelItemButton> {
  double _customAlsVelValue = 0;

  late final TextEditingController _inputTextController;

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

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

  void _initState() {
    final activeMeasureItem = Get.find<IApplication>().activeMeasureItem;
    if (activeMeasureItem == null) {
      return;
    }
    _customAlsVelValue = (activeMeasureItem as Pisa).alsVel.value!;
  }

  Widget _buildDialog() {
    return Stack(
      children: [
        QuickFWidget(
          ModalBarrier(
            color: Colors.black.withOpacity(0.5),
            onDismiss: () {},
          ),
        ),
        LabAlertDialog(
          maxWidth: 400,
          content: Container(
            height: 180,
            child: Column(children: [_buildInputButton()]),
          ),
          onConfirm: () {
            _onValueConfrim();
            Get.back();
          },
          title: 'Als.Vel',
        )
      ],
    );
  }

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

    double alsVel = _customAlsVelValue;
    (activeMeasureItem as Pisa).updateAlsVel(alsVel);
  }

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