import 'package:fis_i18n/i18n.dart';
import 'package:fis_ui/base_define/page.dart';
import 'package:fis_ui/index.dart';
import 'package:flutter/material.dart';
import 'package:fis_theme/theme.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:flyinsono/architecture/define.dart';
import 'package:flyinsono/pages/auth/retrieve_password/controller.dart';
import 'package:flyinsono/pages/auth/widgets/code_field.dart';
import 'package:flyinsono/pages/auth/widgets/form_validator.dart';
import 'package:flyinsono/pages/auth/widgets/text_field.dart';
import 'package:get/get.dart';

import '../auth_board/controller.dart';
import '../widgets/button.dart';
import '../widgets/page_resizable_box.dart';

///找回密码页面
class RetrievePasswordPage extends FisView<RetrievePasswordController>
    implements FPage {
  const RetrievePasswordPage({this.pageName = 'RetrievePasswordPage', Key? key})
      : super(key: key);

  @override
  final String pageName;
  @override
  FWidget build(BuildContext context) {
    return FPageResizableBox(
      builder: () => _Layout(this),
    );
  }
}

///找回密码页面布局
class _Layout extends FisView<RetrievePasswordController> implements FWidget {
  _Layout(this.businessParent);
  final FPage businessParent;
  final _parentContext = Get.find<AuthBoardController>().currentContext;

  final _colorScheme = FTheme.ins.colorScheme;

  @override
  FWidget build(BuildContext context) {
    final spaceBetween = const FSizedBox(height: 14);
    final state = controller.state;
    final form = FForm(
      child: FColumn(
        children: [
          if (kIsMobile)
            FSizedBox(height: 38.sp)
          else ...[
            FText(
              i18nBook.auth.forgotPwd.t,
              style: TextStyle(
                fontSize: 20.sp,
                color: _colorScheme.primary,
              ),
            ),
            FSizedBox(height: 32.sp),
          ],
          _buildAccountField(),
          spaceBetween,
          _buildVerificationCodeField(),
          spaceBetween,
          _buildPwdField(),
          spaceBetween,
          _buildReenterPwdField(),
          const FSizedBox(height: 40),
          FObx(
            () => FContainer(
              width: double.infinity,
              child: SubmitButton(
                businessParent: this.businessParent,
                description: "RetrievePasswordPage resetPwd",
                text: i18nBook.auth.resetPwd.t,
                onPressed: controller.onSubmit,
                loading: state.onSubmit,
                loadingText: i18nBook.common.submitting.t,
                disabled: !controller.formValidateState.isCanSubmit,
              ),
            ),
          ),
          if (!kIsMobile) spaceBetween,
          FContainer(
            width: double.infinity,
            child: FormSecondaryButton(
              businessParent: this.businessParent,
              onPressed: controller.onRouteBack,
              description: "Back login",
              text: i18nBook.auth.back4Login.t,
            ),
          ),
        ],
      ),
    );

    return FContainer(
      color: kIsMobile ? Colors.white : Color.fromARGB(255, 247, 251, 255),
      child: FListView(
        children: [form],
      ),
    );
  }

  FWidget _buildAccountField() {
    final validateState = controller.formValidateState;
    final txtField = FObx(
      () => FBorderTextFormField(
        controller: controller.accountTextController,
        focusNode: controller.accountFocusNode,
        maxLength: 25,
        inError: !validateState.accountChecked &&
            controller.formEditState.accountEdited,
        onChanged: (val) {
          controller.state.account = val;
          controller.showAccountValidator();
        },
        hintText: i18nBook.auth.hint4AccountDouble.t,
      ),
    );

    return FFormItemValidator(
      key: ValueKey("RetrievePwd.Validator.Account"),
      child: txtField,
      overlayContext: _parentContext,
      controller: controller.accountValidatorController,
      validateSteps: [
        FFormItemValidateStep(
          validateFn: () => validateState.accountFormatCheck,
          tips: i18nBook.auth.toast4AccountDouble.t,
        ),
      ],
    );
  }

  FWidget _buildPwdField() {
    final validateState = controller.formValidateState;
    final txtField = FObx(
      () => FBorderTextFormField(
        controller: controller.passwordTextController,
        focusNode: controller.passwordFocusNode,
        inError:
            !validateState.pwdChecked && controller.formEditState.pwdEdited,
        maxLength: 20,
        obscureText: true,
        onChanged: (val) {
          controller.state.password = val;
          controller.showPwdValidator();
        },
        isShowVisibilityIcon: true,
        hintText: i18nBook.auth.hint4Pwd.t,
      ),
    );

    return FFormItemValidator(
      key: ValueKey("RetrievePwd.Validator.Pwd"),
      child: txtField,
      overlayContext: _parentContext,
      controller: controller.passwordValidatorController,
      validateSteps: [
        FFormItemValidateStep(
          tips: i18nBook.auth.toast4PwdBlankError.t,
          validateFn: () => validateState.pwdNotContainBlank,
        ),
        FFormItemValidateStep(
          tips: i18nBook.auth.toast4PwdLengthError.t,
          validateFn: () => validateState.pwdLengthChecked,
        ),
        FFormItemValidateStep(
          tips: i18nBook.auth.toast4PwdCharCombinError.t,
          validateFn: () => validateState.pwdCharCombinChecked,
        ),
      ],
    );
  }

  FWidget _buildReenterPwdField() {
    final validateState = controller.formValidateState;
    final txtField = FObx(
      () => FBorderTextFormField(
        controller: controller.reenterPasswordTextController,
        focusNode: controller.reenterPasswordFocusNode,
        maxLength: 20,
        inError: !validateState.reenterPwdChecked &&
            controller.formEditState.reenterPwdEdited,
        obscureText: true,
        onChanged: (val) {
          controller.state.reenterPassword = val;
          controller.showReenterPwdValidator();
        },
        isShowVisibilityIcon: true,
        hintText: i18nBook.auth.hint4ConfirmPwd.t,
      ),
    );

    return FFormItemValidator(
      key: ValueKey("RetrievePwd.Validator.ReenterPwd"),
      child: txtField,
      overlayContext: _parentContext,
      controller: controller.reenterPasswordValidatorController,
      validateSteps: [
        FFormItemValidateStep(
          tips: i18nBook.auth.toast4ConfirmPwdMatchError.t,
          validateFn: () => validateState.reenterPwdEqualsPwd,
        ),
      ],
    );
  }

  FWidget _buildVerificationCodeField() {
    return FObx(
      () => VerificationCodeField(
        tag: RetrievePasswordController.VERIFICATION_CONTROLLER_TAG,
        textFocusNode: controller.verificationCodeFocusNode,
        textController: controller.verificationCodeTextController,
        onTextChanged: (val) {
          controller.state.verificationCode = val;
        },
        inError: !controller.formValidateState.verificationCodeChecked &&
            controller.formEditState.verificationCodeEdited,
        fetchCodeFunc: controller.onFetchCodeClick,
      ),
    );
  }
}
