import 'package:fis_theme/theme.dart';
import 'package:fis_ui/base_define/page.dart';
import 'package:fis_ui/index.dart';
import 'package:fis_ui/values/font_sizes.dart';
import 'package:flutter/material.dart';

///待输入密码框
class WaitingForInputPassword extends FStatefulWidget {
  WaitingForInputPassword({
    Key? key,
    this.onChanged,
    this.hintText,
    this.isVerifyCode = false,
    this.isPassword = true,
    this.maxLength = 50,
    required this.parent,
  }) : super(key: key);

  ///父级节点
  final FPage parent;

  ///文本改变回调
  final ValueChanged? onChanged;

  ///提示文字
  final String? hintText;

  ///是否验证码
  final bool? isVerifyCode;

  ///是否密码框
  final bool? isPassword;

  final int maxLength;

  @override
  FState<WaitingForInputPassword> createState() =>
      _WaitingForInputPasswordState();
}

class _WaitingForInputPasswordState extends FState<WaitingForInputPassword> {
  TextEditingController controller = TextEditingController();
  final focusNode = FocusNode();
  bool _isFocus = false;
  bool _isPassword = true;

  @override
  void initState() {
    _isPassword = widget.isPassword!;
    _listenFocusNodes();
    super.initState();
  }

  @override
  FWidget build(BuildContext context) {
    final _itemHeight = 33.0;
    final _isVerifyCode =
        widget.isVerifyCode == null ? false : widget.isVerifyCode!;

    return FBorderInput(
      controller: controller,
      height: _itemHeight,
      isPassword: _isPassword,
      hintSize: FFontSizes.H6,
      contentSize: FFontSizes.H6,
      readOnly: widget.onChanged == null ? true : false,
      hintText: widget.hintText,
      maxLength: widget.maxLength,
      fillColor:
          widget.onChanged == null ? Color.fromRGBO(224, 224, 224, 1) : null,
      borderColor: _isFocus
          ? FTheme.ins.colorScheme.primary
          : Color.fromRGBO(230, 230, 230, 1),
      focusNode: focusNode,
      onChanged: (val) {
        widget.onChanged!.call(val);
      },
      suffixIcon: FIconButton(
        name: "close",
        businessParent: this.widget.parent,
        icon: FIcon(
          Icons.close,
          color: _isFocus && !_isVerifyCode
              ? FTheme.ins.colorScheme.primary
              : Colors.transparent,
        ),
        onPressed: () {
          setState(() {
            controller.text = '';
          });
        },
      ),
    );
  }

  ///监听Focus
  void _listenFocusNodes() {
    if (widget.onChanged != null) {
      focusNode.addListener(() {
        if (focusNode.hasFocus) {
          setState(() {
            _isFocus = true;
          });
        } else {
          setState(() {
            _isFocus = false;
          });
        }
      });
    }
  }
}
