import 'package:cpt_auth/modules/auth_login/auth_login_page.dart';
import 'package:cs_resources/generated/assets.dart';
import 'package:cs_resources/generated/l10n.dart';
import 'package:cs_resources/theme/app_colors_theme.dart';
import 'package:flutter/material.dart';
import 'package:auto_route/auto_route.dart';
import 'package:hooks_riverpod/hooks_riverpod.dart';
import 'package:router/ext/auto_router_extensions.dart';
import 'package:shared/utils/log_utils.dart';
import 'package:widgets/ext/ex_widget.dart';
import 'package:widgets/my_appbar.dart';
import 'package:widgets/my_button.dart';
import 'package:widgets/my_load_image.dart';
import 'package:widgets/my_text_field.dart';
import 'package:widgets/my_text_view.dart';
import 'package:widgets/widget_export.dart';
import 'package:plugin_basic/dialog/country_code_selecter.dart';
import '../../router/page/auth_page_router.dart';
import 'forgot_input_state.dart';
import 'forgot_input_view_model.dart';

@RoutePage()
class ForgotInputPage extends HookConsumerWidget {
  const ForgotInputPage({Key? key}) : super(key: key);

  //启动当前页面
  static void startInstance({BuildContext? context}) {
    if (context != null) {
      context.router.push(const ForgotInputPageRoute());
    } else {
      appRouter.push(const ForgotInputPageRoute());
    }
  }

  @override
  Widget build(BuildContext context, WidgetRef ref) {
    final viewModel = ref.read(forgotInputViewModelProvider.notifier);
    final state = ref.watch(forgotInputViewModelProvider);

    return Scaffold(
      appBar: MyAppBar.appBar(context, ""),
      backgroundColor: context.appColors.backgroundDefault,
      body: SingleChildScrollView(
        scrollDirection: Axis.vertical,
        physics: const BouncingScrollPhysics(),
        child: Container(
          margin: const EdgeInsets.symmetric(horizontal: 38),
          width: double.infinity,
          child: Column(
            mainAxisSize: MainAxisSize.max,
            crossAxisAlignment: CrossAxisAlignment.center,
            children: [
              //顶部图片
              const MyAssetImage(
                Assets.authForgotInputEmailPhoneImg,
                width: 260,
                height: 170,
              ).marginOnly(top: 25, bottom: 29),

              //提示文本
              MyTextView(
                S.current.forgot_text,
                fontSize: 24,
                marginBottom: 23,
                isFontMedium: true,
                textColor: context.appColors.textBlack,
              ),

              //邮箱输入框
              _buildInputLayout(
                context,
                state,
                "email",
                textInputType: TextInputType.emailAddress,
                textInputAction: TextInputAction.next,
                errorText: state.emailErrorText,
                onSubmit: (formKey, value) {
                  state.formData[formKey]!['focusNode'].unfocus();
                  FocusScope.of(context).requestFocus(state.formData['phone']!['focusNode']);
                },
              ),

              //电话号码
              Row(
                mainAxisSize: MainAxisSize.max,
                crossAxisAlignment: CrossAxisAlignment.center,
                children: [
                  //封装的国家选择控件
                  CountryCodeSelector(
                    onChanged: (countryCode) {
                     viewModel.saveCountryCode(countryCode);
                    },
                  ).marginOnly(left: 15),

                  //电话输入框
                  _buildInputLayout(
                    context,
                    state,
                    "phone",
                    fillBGColor: Colors.transparent,
                    textInputType: TextInputType.number,
                    textInputAction: TextInputAction.done,
                    errorText: state.phoneErrorText,
                    onSubmit: (formKey, value) {
                      state.formData[formKey]!['focusNode'].unfocus();
                      viewModel.submitInput();
                    },
                  ).expanded(),
                ],
              )
                  .decorated(
                    color: context.appColors.authFiledBG,
                    borderRadius: BorderRadius.circular(5.0),
                  )
                  .marginOnly(top: 15),

              MyButton(
                onPressed: viewModel.submitInput,
                text: S.current.next,
                textColor: Colors.white,
                backgroundColor: context.appColors.btnBgDefault,
                fontWeight: FontWeight.w500,
                type: ClickType.throttle,
                fontSize: 16,
                minHeight: 50,
                radius: 5,
              ).marginOnly(top: 25, bottom: 30),
            ],
          ),
        ),
      ),
    );
  }

  /// 输入框
  Widget _buildInputLayout(
    BuildContext context,
    ForgotInputState state,
    String key, {
    double marginTop = 0,
    bool? showRightIcon = false, //是否展示右侧的布局
    Widget? rightWidget, //右侧的布局
    TextInputType textInputType = TextInputType.text,
    String? errorText,
    bool obscureText = false,
    Color? fillBGColor,
    TextInputAction textInputAction = TextInputAction.done,
    Function? onSubmit,
  }) {
    return IgnoreKeyboardDismiss(
      child: MyTextField(
        key,
        fillBackgroundColor: fillBGColor ?? context.appColors.authFiledBG,
        state.formData[key]!['value'],
        hintText: state.formData[key]!['hintText'],
        hintStyle: TextStyle(
          color: context.appColors.authFiledHint,
          fontSize: 16.0,
          fontWeight: FontWeight.w500,
        ),
        controller: state.formData[key]!['controller'],
        focusNode: state.formData[key]!['focusNode'],
        margin: EdgeInsets.only(top: marginTop),
        padding: const EdgeInsets.symmetric(horizontal: 12, vertical: 3),
        showDivider: false,
        height: 44,
        style: TextStyle(
          color: context.appColors.authFiledText,
          fontSize: 16.0,
          fontWeight: FontWeight.w500,
        ),
        inputType: textInputType,
        textInputAction: textInputAction,
        onSubmit: onSubmit,
        cursorColor: context.appColors.authFiledText,
        obscureText: obscureText,
        errorText: errorText,
        showLeftIcon: true,
        showRightIcon: showRightIcon,
        rightWidget: rightWidget,
      ),
    );
  }
}
