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';
import 'package:flyinsono/architecture/define.dart';
import 'package:flyinsono/architecture/router/index.dart';
import 'package:flyinsono/architecture/states/states.dart';
import 'package:flyinsono/pages/user/user_center/controller.dart';
import 'package:flyinsono/pages/user/user_center/widgets/password_row.dart';
import 'package:flyinsono/pages/user/user_center/widgets/waitin_for_input_password.dart';

///修改密码页面
class PasswordValidationPage extends FisView<UserCenterController>
    implements FPage {
  const PasswordValidationPage({Key? key}) : super(key: key);

  @override
  final String pageName = 'PasswordValidationPage';

  @override
  FWidget build(BuildContext context) {
    return context.fResponsiveVoid(
      mobile: () => _MobileLayout(this),
      desktop: () => _DesktopLayout(this),
    );
  }
}

class _MobileLayout extends FisView<UserCenterController> implements FWidget {
  ///父级节点
  final FPage businessParent;

  _MobileLayout(this.businessParent);
  @override
  FWidget build(BuildContext context) {
    var isPhoneEmpty = Store.user.phone == null || Store.user.phone!.isEmpty;
    var isEmailEmpty = Store.user.email == null || Store.user.email!.isEmpty;
    return FScaffold(
      resizeToAvoidBottomInset: false,
      appBar: _buildChangePasswordAppBar(),
      body: FPadding(
        padding: EdgeInsets.symmetric(horizontal: 15, vertical: 15),
        child: FColumn(
          crossAxisAlignment: CrossAxisAlignment.stretch,
          children: [
            const FSizedBox(height: 20),
            if (!isPhoneEmpty || !isEmailEmpty) ...[
              _buildPasswordWordOrEmailTitle(isPhoneEmpty),
              const FSizedBox(height: 30),
              _buildVerifyCodeRow(),
            ] else ...[
              _buildOldPasswordRow(),
            ],
            const FSizedBox(height: 30),
            // FText(
            //   i18nBook.user.passowrdTip.t,
            //   style: TextStyle(
            //     fontSize: 12,
            //     color: FTheme.ins.colorScheme.textTips,
            //   ),
            // ),
          ],
        ),
      ),
    );
  }

  /// 修改密码APPBar
  _buildChangePasswordAppBar() {
    return FAppBar(
      title: FText(
        i18nBook.common.validateIdentity.t,
        style: TextStyle(fontSize: 16, color: Colors.white),
      ),
      elevation: 0,
      centerTitle: true,
      leading: FIconButton(
        name: "back",
        businessParent: businessParent,
        icon: FIcon(
          Icons.arrow_back_ios_new,
          size: 20,
        ),
        onPressed: () {
          router.back();
        },
      ),
      actions: [
        FTextButton(
            name: "cancellation",
            businessParent: this.businessParent,
            child: FText(
              i18nBook.common.cancellation.t,
              style: TextStyle(color: Colors.white, fontSize: 16),
            ),
            onPressed: () {
              controller.commonUserLogOff();
            }),
      ],
    );
  }

  ///手机号或邮箱验证
  FWidget _buildPasswordWordOrEmailTitle(bool isPhoneEmpty) {
    return PasswordRow(
      businessParent: this.businessParent,
      hintText: isPhoneEmpty ? Store.user.email : Store.user.phone,
      isReadOnly: true,
      isPassword: false,
      label: FText(
        isPhoneEmpty ? i18nBook.common.email.t : i18nBook.common.mobileNumber.t,
        style: TextStyle(
          fontSize: 14,
        ),
      ),
      onChanged: (value) {
        controller.state.oldPassword = value;
      },
      suffixIconOnPressed: () {
        controller.state.oldPassword = "";
      },
    );
  }

  ///验证码
  FWidget _buildVerifyCodeRow() {
    return PasswordRow(
      businessParent: this.businessParent,
      label: FText(
        i18nBook.common.verificationCode.t,
        style: TextStyle(
          fontSize: 14,
        ),
      ),
      isPassword: false,
      hintText: i18nBook.user.inputVerificationCode.t,
      onChanged: (value) {
        controller.password = value;
        controller.state.disableSubmit = false;
      },
      suffixText: i18nBook.user.getVerificationCode.t,
      suffixIconOnPressed: () {
        controller.sendVerificationCode();
        controller.state.disableSubmit = true;
      },
    );
  }

  ///原密码
  FWidget _buildOldPasswordRow() {
    return PasswordRow(
      businessParent: this.businessParent,
      hintText: i18nBook.auth.hint4Pwd.t,
      label: FText(
        i18nBook.auth.password.t,
        style: TextStyle(fontSize: 14),
      ),
      onChanged: (value) {
        controller.password = value;
        controller.state.disableSubmit = false;
      },
      suffixIconOnPressed: () {
        controller.password = "";
        controller.state.disableSubmit = true;
      },
    );
  }
}

class _DesktopLayout extends FisView<UserCenterController> implements FWidget {
  ///父级节点
  final FPage businessParent;

  _DesktopLayout(this.businessParent);
  @override
  FWidget build(BuildContext context) {
    double _fontSize = 16;
    var isPhoneEmpty = Store.user.phone == null || Store.user.phone!.isEmpty;
    var isEmailEmpty = Store.user.email == null || Store.user.email!.isEmpty;
    return FSimpleDialog(
      title: FText(
        i18nBook.common.validateIdentity.t,
        style: TextStyle(
          color: Colors.white,
          fontSize: 18,
        ),
      ),
      okString: i18nBook.common.confirm.t,
      cancelString: i18nBook.common.cancel.t,
      backgroundColor: Color.fromRGBO(230, 230, 230, 1),
      bottomButton: _buildBottomButton(),
      children: [
        FContainer(
          /// TODO:[Gavin] 多语言-UI-整合
          width: i18nBook.isCurrentChinese ? 520 : 700,
          color: Colors.white,
          child: FPadding(
            padding: EdgeInsets.symmetric(horizontal: 50, vertical: 20),
            child: FColumn(
              crossAxisAlignment: CrossAxisAlignment.start,
              children: [
                FSizedBox(height: 20),
                if (!isPhoneEmpty || !isEmailEmpty) ...[
                  _buildPhoneOrEmailTitle(isPhoneEmpty, _fontSize),
                  FSizedBox(height: 20),
                  _buildVerifyCodeRow(_fontSize),
                ] else ...[
                  _buildOldPasswordRow(_fontSize),
                ],
                FSizedBox(height: 20),
              ],
            ),
          ),
        ),
      ],
    );
  }

  ///底部操作按钮
  FWidget _buildBottomButton() {
    return FContainer(
      alignment: Alignment.center,
      padding: EdgeInsets.symmetric(vertical: 15),
      child: FFlex(
        direction: Axis.horizontal,
        mainAxisAlignment: MainAxisAlignment.spaceAround,
        mainAxisSize: MainAxisSize.min,
        children: [
          FObx(
            () {
              return FElevatedButton(
                businessParent: this.businessParent,
                name: "updatePassword",
                onPressed: controller.state.disableSubmit
                    ? null
                    : () {
                        controller.commonUserLogOff();
                      },
                style: ButtonStyle(
                  padding: MaterialStateProperty.all(
                    EdgeInsets.symmetric(
                      vertical: 15,
                      horizontal: 25,
                    ),
                  ),
                ),
                child: FText(i18nBook.common.cancellation.t),
              );
            },
          ),
          FSizedBox(
            width: 20,
          ),
          FOutlinedButton(
            name: "back",
            businessParent: this.businessParent,
            onPressed: () {
              router.back();
            },
            style: ButtonStyle(
              backgroundColor: MaterialStateProperty.all(
                Colors.white,
              ),
              padding: MaterialStateProperty.all(
                EdgeInsets.symmetric(vertical: 15, horizontal: 25),
              ),
            ),
            child: FText(
              i18nBook.common.cancel4Btn.t,
              style: TextStyle(
                color: FTheme.ins.data.colorScheme.primary,
              ),
            ),
          ),
        ],
      ),
    );
  }

  ///手机号Or邮箱
  FWidget _buildPhoneOrEmailTitle(bool isPhoneEmpty, double fontSize) {
    return FRow(
      children: [
        FContainer(
          /// TODO:[Gavin] 多语言-UI-整合
          width: i18nBook.isCurrentChinese ? 90 : 200,
          alignment: Alignment.centerLeft,
          child: FText(
            isPhoneEmpty
                ? i18nBook.common.email.t
                : i18nBook.common.mobileNumber.t,
            style: TextStyle(fontSize: fontSize),
          ),
        ),
        FSizedBox(width: 40),
        WaitingForInputPassword(
          parent: businessParent,
          hintText: isPhoneEmpty ? Store.user.email! : Store.user.phone!,
          onChanged: null,
          isPassword: false,
        ),
      ],
    );
  }

  ///验证码
  FWidget _buildVerifyCodeRow(double fontSize) {
    return FRow(
      children: [
        FContainer(
          /// TODO:[Gavin] 多语言-UI-整合
          width: i18nBook.isCurrentChinese ? 90 : 200,
          alignment: Alignment.centerLeft,
          child: FText(
            i18nBook.common.verificationCode.t,
            style: TextStyle(fontSize: fontSize),
          ),
        ),
        FSizedBox(width: 40),
        WaitingForInputPassword(
          parent: businessParent,
          hintText: i18nBook.user.inputVerifyCode.t,
          isVerifyCode: true,
          isPassword: false,
          onChanged: (val) {
            controller.password = val;
            controller.state.disableSubmit = false;
          },
          maxLength: 6,
        ),
        FSizedBox(
          width: 20,
        ),
        FContainer(
          height: 31,
          child: FObx(() {
            return FElevatedButton(
              businessParent: this.businessParent,
              name: "sendVerificationCode",
              child: FText(controller.state.isFetching
                  ? i18nBook.user.afterXSecondFetchAgain
                      .translate([controller.state.seconds.toString()])
                  : i18nBook.user.getVerificationCode.t),
              onPressed: controller.state.isFetching
                  ? null
                  : () => controller.sendVerificationCode(),
              style: ButtonStyle(),
            );
          }),
        ),
      ],
    );
  }

  ///原密码
  FWidget _buildOldPasswordRow(double fontSize) {
    return FRow(
      children: [
        FContainer(
          /// TODO:[Gavin] 多语言-UI-整合
          width: i18nBook.isCurrentChinese ? 90 : 200,
          alignment: Alignment.centerLeft,
          child: FText(
            i18nBook.auth.password.t,
            style: TextStyle(fontSize: fontSize),
          ),
        ),
        FSizedBox(width: 40),
        WaitingForInputPassword(
          parent: businessParent,
          hintText: i18nBook.auth.hint4Pwd.t,
          onChanged: (val) {
            controller.password = val;
            controller.state.disableSubmit = false;
          },
        ),
      ],
    );
  }
}
