import 'package:fis_common/extensions/string.dart';
import 'package:fis_i18n/i18n.dart';
import 'package:fis_theme/theme.dart';
import 'package:fis_ui/index.dart';
import 'package:fis_ui/interface/interactive_container.dart';
import 'package:flutter/material.dart';
import 'package:flyinsono/architecture/router/router.dart';
import 'package:flyinsono/architecture/utils/prompt_box.dart';
import 'package:get/get.dart';

///手机端更新用户文本信息
class UpdateUserTextInfo extends StatefulWidget
    implements FInteractiveContainer {
  ///原有值
  final String? value;

  ///标题
  final String? title;

  final String hintText;

  ///类型：姓名、手机号、邮箱、医院短码
  final ChangeType? changeType;

  @override
  final String pageName = 'UpdateUserTextInfo';

  UpdateUserTextInfo({
    Key? key,
    this.value,
    this.title,
    this.changeType,
    this.hintText = '',
  }) : super(key: key);

  @override
  State<StatefulWidget> createState() => _UpdateUserTextInfoState();
}

class _UpdateUserTextInfoState extends State<UpdateUserTextInfo> {
  final _controller = TextEditingController();
  final _focusNode = FocusNode();
  bool _focus = false;
  String _text = '';
  @override
  void initState() {
    _focusNode.addListener(() {
      if (_focus != _focusNode.hasFocus) {
        setState(() {
          _focus = _focusNode.hasFocus;
        });
      }
    });

    _text = widget.value ?? '';
    _controller.text = _text;
    super.initState();
  }

  @override
  FWidget build(BuildContext context) {
    return FScaffold(
      appBar: FAppBar(
        title: FText(
          widget.title!,
          style: TextStyle(fontSize: 16),
        ),
        centerTitle: true,
        leading: FIconButton(
          name: "back",
          businessParent: widget,
          icon: FIcon(Icons.arrow_back_ios_new),
          onPressed: () {
            router.back();
          },
        ),
        elevation: 0,
        actions: [
          FTextButton(
              name: "confirm",
              businessParent: widget,
              child: FText(
                i18nBook.common.confirm.t,
                style: TextStyle(color: Colors.white, fontSize: 14),
              ),
              onPressed: () {
                if (checkValue()) {
                  Get.back(result: _text);
                }
              }),
        ],
      ),
      body: FColumn(
        children: [
          FSizedBox(height: 30),
          FRow(
            children: [
              FSizedBox(width: 20),
              FBorderInput(
                hintSize: 16,
                contentSize: 16,
                maxLength: 20,
                focusNode: _focusNode,
                controller: _controller,
                borderColor: Colors.transparent,
                height: 30,
                onChanged: (value) => {
                  _text = value,
                },
                hintText: widget.hintText,
                suffixIcon: FContainer(
                  margin: EdgeInsets.only(right: 18),
                  height: 25,
                  child: FIconButton(
                    name: "close",
                    businessParent: this.widget,
                    icon: FIcon(
                      Icons.close,
                      size: 25,
                    ),
                    onPressed: () {
                      setState(() {
                        _text = "";
                        _controller.text = '';
                      });
                    },
                  ),
                ),
              ),
            ],
          ),
          FPadding(
            padding: EdgeInsets.only(left: 30, right: 30),
            child: FDivider(
              thickness: 1,
              color: _focus ? FTheme.ins.colorScheme.primary : Colors.grey,
            ),
          ),
        ],
      ),
    );
  }

  ///验证手机号或者邮箱格式是否正确
  bool checkValue() {
    if (widget.changeType == ChangeType.Phone && _text.isNotNullOrEmpty) {
      if (i18nBook.isCurrentChinese) {
        if (!_text.isChineseMobileNumber) {
          PromptBox.toast(i18nBook.user.phoneFormError.t);
          return false;
        }
      } else {
        if (!_text.isNumberPhone) {
          PromptBox.toast(i18nBook.user.phoneFormError.t);
          return false;
        }
      }
    } else if (widget.changeType == ChangeType.Email &&
        !_text.isEmail &&
        _text.isNotNullOrEmpty) {
      PromptBox.toast(i18nBook.user.emailFormError.t);
      return false;
    }
    return true;
  }

  @override
  void dispose() {
    _focusNode.dispose();
    super.dispose();
  }
}

//修改类别
enum ChangeType {
  Name,
  Phone,
  Email,
  OrgShortCode,
}
