import 'package:flutter/cupertino.dart';
import 'package:keyboard_actions/keyboard_actions.dart';

class InputView extends StatefulWidget {
  final String title;
  final int value;
  final Function(int)? newSelection;

  const InputView(this.title, this.value, this.newSelection, {super.key});

  @override
  State<InputView> createState() => _InputViewState();
}

class _InputViewState extends State<InputView> {
  final TextEditingController _controller = TextEditingController();
  final FocusNode _nodeText = FocusNode();
  late String _textBefore;

  @override
  Widget build(BuildContext context) {
    _controller.text = '${widget.value}';
    _textBefore = '${widget.value}';
    return CupertinoListTile(
        key: widget.key,
        trailing: SizedBox(
            width: 155,
            height: 30,
            child: KeyboardActions(
              tapOutsideBehavior: TapOutsideBehavior.translucentDismiss,
              config: _buildConfig(context),
              child: Container(
                padding: const EdgeInsets.only(left: 12),
                child: CupertinoTextField(
                  minLines: 1,
                  maxLines: 1,
                  maxLength: 10,
                  autocorrect: false,
                  focusNode: _nodeText,
                  decoration: null,
                  keyboardType: TextInputType.number,
                  controller: _controller,
                  onSubmitted: _onSubmit,
                  onTapOutside: (_) {
                    _controller.text = _textBefore;
                  },
                ),
              ),
            )
        ),
        title: Text(widget.title, style: const TextStyle(decoration: TextDecoration.none, fontSize: 17)));
  }

  void _onSubmit(String newText) {
    _textBefore = newText;
    var newTime = int.tryParse(newText);
    if (newTime != null) {
      widget.newSelection?.call(newTime);
    }
  }

  KeyboardActionsConfig _buildConfig(BuildContext context) {
    return KeyboardActionsConfig(
      keyboardActionsPlatform: KeyboardActionsPlatform.IOS,
      keyboardBarColor: CupertinoColors.white,
      keyboardSeparatorColor: CupertinoColors.systemGrey4,
      nextFocus: false,
      actions: [
        KeyboardActionsItem(
          focusNode: _nodeText,
          toolbarButtons: [
            (node) {
              return TextFieldTapRegion(
                child: Container(
                  width: MediaQuery.of(context).size.width - 14,
                  margin: const EdgeInsets.symmetric(horizontal: 7),
                  child: Row(
                    children: [
                      CupertinoButton(
                          padding: const EdgeInsets.all(5),
                          onPressed: () {
                            _controller.text = _textBefore;
                            node.unfocus();
                          },
                          child: const Text("Отмена", style: TextStyle(fontWeight: FontWeight.w500))
                      ),
                      const Spacer(),
                      CupertinoButton(
                          padding: const EdgeInsets.all(5),
                          onPressed: () {
                             _onSubmit(_controller.text);
                             node.unfocus();
                           },
                          child: const Text("Готово", style: TextStyle(fontWeight: FontWeight.w500))
                      )
                    ],
                  ),
                ),
              );
            }
          ],
        ),
      ],
    );
  }
}
