import 'dart:async';

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

///修改密码其中一行UI
class PasswordRow extends FStatefulWidget {
  PasswordRow({
    Key? key,
    this.hintText,
    this.label,
    this.onChanged,
    this.suffixIconOnPressed,
    this.isReadOnly,
    this.suffixText,
    this.isPassword,
    required this.businessParent,
  }) : super(key: key);

  ///父级节点
  final FPage businessParent;

  ///是否只读
  final bool? isReadOnly;

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

  ///标题
  final FWidget? label;

  ///输入框文字改变时间回调
  final ValueChanged? onChanged;

  ///尾部文字
  final String? suffixText;

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

  ///尾部Icon点击回调
  final VoidCallback? suffixIconOnPressed;

  @override
  FState<PasswordRow> createState() => _PasswordRowState();
}

class _PasswordRowState extends FState<PasswordRow> {
  ///密码输入框Controller
  final controller = TextEditingController();

  ///密码输入框FocusNode
  final focusNode = FocusNode();

  ///验证码倒计时
  final int countDown = 60;

  ///是否处于焦点状态
  bool isFocus = false;
  bool _isShowsuffixIcon = false;
  bool _isCountingdown = false;
  int _seconds = 0;

  Timer? _timer;

  @override
  void initState() {
    _listenFocusNodes();
    super.initState();
  }

  ///添加监听
  void _listenFocusNodes() {
    focusNode.addListener(() {
      if (focusNode.hasFocus) {
        setState(() {
          isFocus = true;
          _isShowsuffixIcon = true;
        });
      } else {
        setState(() {
          isFocus = false;
          _isShowsuffixIcon = false;
        });
      }
    });
  }

  @override
  FWidget build(BuildContext context) {
    bool isReadOnly = widget.isReadOnly == null ? false : widget.isReadOnly!;
    final padding = EdgeInsets.only(left: 95, right: 15);
    if (isReadOnly) {
      controller.text = widget.hintText!;
    }
    return FContainer(
      width: 500,
      child: FColumn(
        crossAxisAlignment: CrossAxisAlignment.start,
        children: [
          FRow(
            children: [
              FContainer(
                width: i18nBook.isCurrentChinese ? 65 : 80,
                child: widget.label,
              ),
              const FSizedBox(width: 20),
              FBorderInput(
                readOnly: isReadOnly,
                hintSize: 14,
                contentSize: 14,
                maxLength: 20,
                borderColor: Colors.transparent,
                height: 25,
                isPassword: widget.isPassword ?? true,
                focusNode: focusNode,
                hintText: widget.hintText,
                onChanged: (value) {
                  widget.onChanged!.call(value);
                },
                controller: controller,
                suffixIcon: _buildSuffxIcon(isReadOnly, _isShowsuffixIcon),
              ),
            ],
          ),
          FPadding(
            padding: padding,
            child: FDivider(
              thickness: 1,
              color: isReadOnly
                  ? FTheme.ins.colorScheme.textTips
                  : isFocus
                      ? FTheme.ins.colorScheme.primary
                      : FTheme.ins.colorScheme.textTips,
            ),
          ),
        ],
      ),
    );
  }

  /// 开启倒计时
  startCountDown() {
    _seconds = countDown;

    _timer = Timer.periodic(Duration(seconds: 1), (timer) {
      if (mounted) {
        setState(() {
          if (!_isCountingdown) _isCountingdown = true;
          _seconds--;

          if (_seconds == 0 && _timer != null) {
            _timer!.cancel();
            _isCountingdown = false;
          }
        });
      }
    });
  }

  @override
  void dispose() {
    _timer?.cancel();
    _timer = null;
    focusNode.dispose();
    controller.dispose();
    super.dispose();
  }

  ///验证按钮
  FWidget _buildVerifyButton() {
    return FGestureDetector(
      name: "VerifyButton",
      businessParent: this.widget.businessParent,
      onTap: _isCountingdown
          ? () {}
          : () {
              widget.suffixIconOnPressed!.call();
              startCountDown();
            },
      child: FContainer(
        margin: EdgeInsets.only(
          right: 15,
          top: 2,
        ),
        child: FText(
          _isCountingdown
              ? i18nBook.user.afterXSecondFetchAgain
                  .translate([_seconds.toString()])
              : widget.suffixText!,
          style: TextStyle(
            fontSize: 14,
            color:
                _isCountingdown ? Colors.grey : FTheme.ins.colorScheme.primary,
          ),
        ),
      ),
    );
  }

  ///关闭Icon按钮
  FWidget _buildCloseButton() {
    return FContainer(
      height: 36,
      padding: EdgeInsets.only(bottom: 10, top: 0, right: 4),
      child: FIconButton(
        name: "close",
        businessParent: this.widget.businessParent,
        icon: FIcon(
          Icons.close,
          size: 20,
        ),
        onPressed: () {
          widget.suffixIconOnPressed!.call();
          controller.text = "";
        },
      ),
    );
  }

  ///尾部IconButton
  Widget? _buildSuffxIcon(
    bool isReadOnly,
    bool isShowsuffixIcon,
  ) {
    return isReadOnly || (!isShowsuffixIcon && widget.suffixText == null)
        ? null
        : widget.suffixText != null
            ? _buildVerifyButton()
            : _buildCloseButton();
  }
}
