import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';
import 'package:flashlink/app/base/base_view.dart';
import 'package:flashlink/app/widgets/submit_btn.dart';
import 'package:flashlink/config/theme/theme_extensions/theme_extension.dart';

import '../../../../../config/theme/dark_theme_colors.dart';
import '../../../../../config/translations/strings_enum.dart';
import '../../../../constants/assets.dart';
import '../../../../constants/iconfont/iconfont.dart';
import '../../../../widgets/info_card.dart';
import '../../../../widgets/ix_app_bar.dart';
import '../../../../widgets/ix_image.dart';
import '../../../../widgets/ix_text_field.dart';
import '../controllers/precode_sendemail_controller.dart';

class PrecodeSendemailView extends BaseView<PrecodeSendemailController> {
  const PrecodeSendemailView({super.key});

  @override
  PreferredSizeWidget? get appBar =>
      IXAppBar(title: Strings.sendPreCodeToEmail.tr);

  @override
  Widget buildContent(BuildContext context) {
    return Padding(
      padding: EdgeInsets.symmetric(horizontal: 14.w),
      child: SingleChildScrollView(
        child: Column(
          children: [
            10.verticalSpaceFromWidth,

            // 邮箱输入框
            _buildEmailInput(),

            20.verticalSpaceFromWidth,

            // 发送按钮
            _buildSendButton(),

            12.verticalSpaceFromWidth,

            // 说明文字
            Text(
              Strings.codeBackedUpMessage.tr,
              textAlign: TextAlign.center,
              style: TextStyle(
                fontSize: 14.sp,
                color: Get.reactiveTheme.hintColor.withValues(alpha: 0.6),
              ),
            ),
            // 信息卡片列表
            _buildInfoCardList(),
          ],
        ),
      ),
    );
  }

  /// 邮箱输入框
  Widget _buildEmailInput() {
    return IXTextField(
      hintText: Strings.enterYourEmail.tr,
      controller: controller.emailController,
      focusNode: controller.emailFocusNode,
      keyboardType: TextInputType.emailAddress,
      textInputAction: TextInputAction.done,
      onChanged: (value) => controller.email.value = value,
    );
  }

  /// 发送按钮
  Widget _buildSendButton() {
    return Obx(() {
      final canSend = controller.canSend;
      final isSending = controller.isSending.value;
      return SubmitButton(
        prefixIcon: IXImage(
          source: Assets.preCodeEmailTipWhite,
          width: 20.w,
          height: 20.w,
          sourceType: ImageSourceType.asset,
        ),
        enabled: canSend,
        isLoading: isSending,
        text: Strings.sendYourEmail.tr,
        onPressed: controller.sendEmail,
      );
    });
  }

  /// 信息卡片列表（带连接线）
  Widget _buildInfoCardList() {
    return InfoCard(
      title: '',
      items: [
        InfoItem(
          icon: IconFont.icon23,
          title: Strings.yourPreCredential.tr,
          description: Strings.yourPreCredentialDesc.tr,
          iconColor: DarkThemeColors.primaryColor,
        ),
        InfoItem(
          imageSource: Assets.preCodeEmailTipBlue,
          title: Strings.secureEmailBackup.tr,
          description: Strings.secureEmailBackupDesc.tr,
          iconColor: DarkThemeColors.primaryColor,
        ),
        InfoItem(
          icon: IconFont.icon43,
          title: Strings.sendAndSave.tr,
          description: Strings.sendAndSaveDesc.tr,
          iconColor: DarkThemeColors.primaryColor,
        ),
      ],
    );
  }
}
