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/click_opacity.dart';
import 'package:flashlink/app/widgets/ix_image.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/ix_app_bar.dart';
import '../../../widgets/submit_btn.dart';
import '../controllers/precode_controller.dart';

class PrecodeView extends BaseView<PrecodeController> {
  const PrecodeView({super.key});

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

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

          // 说明卡片
          _buildInfoCard(),

          30.verticalSpaceFromWidth,

          // Pre Code 显示
          _buildPreCodeDisplay(),

          10.verticalSpaceFromWidth,

          // Preview/Hide 和 Copy 按钮
          _buildActionButtons(),

          40.verticalSpaceFromWidth,

          // Send to Email 按钮
          _buildEmailButton(),

          10.verticalSpaceFromWidth,

          // 说明文字
          Text(
            Strings.sendPreCodeEmailDesc.tr,
            textAlign: TextAlign.center,
            style: TextStyle(
              fontSize: 13.sp,
              height: 1.4,
              color: Get.reactiveTheme.hintColor,
            ),
          ),

          10.verticalSpaceFromWidth,

          // Save Local Copy 按钮
          _buildSaveButton(),

          12.verticalSpaceFromWidth,

          // 说明文字
          Text(
            Strings.storeLocalCopyDesc.tr,
            textAlign: TextAlign.center,
            style: TextStyle(
              fontSize: 13.sp,
              height: 1.4,
              color: Get.reactiveTheme.hintColor,
            ),
          ),
        ],
      ),
    );
  }

  /// 说明卡片
  Widget _buildInfoCard() {
    return Container(
      width: double.infinity,
      padding: EdgeInsets.all(10.w),
      decoration: BoxDecoration(
        color: Get.reactiveTheme.highlightColor,
        borderRadius: BorderRadius.circular(8.r),
      ),
      child: Column(
        children: [
          // 铃铛图标
          Icon(
            IconFont.icon67,
            color: DarkThemeColors.validTermColor,
            size: 30.w,
          ),
          10.verticalSpaceFromWidth,

          // 说明文字
          Text(
            Strings.preCodeInfoMessage.tr,
            textAlign: TextAlign.center,
            style: TextStyle(
              fontSize: 14.sp,
              color: Get.reactiveTheme.textTheme.bodyLarge!.color,
              height: 1.5,
            ),
          ),

          20.verticalSpaceFromWidth,

          // 安全提示
          Text(
            Strings.pleaseStoreSecurely.tr,
            textAlign: TextAlign.center,
            style: TextStyle(
              fontSize: 14.sp,
              color: Get.reactiveTheme.textTheme.bodyLarge!.color,
              height: 1.5,
            ),
          ),
        ],
      ),
    );
  }

  /// Pre Code 显示
  Widget _buildPreCodeDisplay() {
    return Obx(
      () => Text(
        controller.displayCode,
        style: TextStyle(
          fontSize: 34.sp,
          height: 1.2,
          color: Get.reactiveTheme.textTheme.bodyLarge!.color,
          fontWeight: FontWeight.w500,
        ),
      ),
    );
  }

  /// Preview/Hide 和 Copy 按钮
  Widget _buildActionButtons() {
    return Row(
      mainAxisAlignment: MainAxisAlignment.center,
      children: [
        // Preview/Hide 按钮
        Obx(
          () => ClickOpacity(
            onTap: controller.togglePreview,
            child: Row(
              mainAxisSize: MainAxisSize.min,
              children: [
                Text(
                  controller.isPreviewMode.value ? Strings.hide.tr : Strings.preview.tr,
                  style: TextStyle(
                    fontSize: 12.sp,
                    height: 1.7,
                    color: Get.reactiveTheme.hintColor,
                  ),
                ),
                8.horizontalSpace,
                Icon(
                  controller.isPreviewMode.value
                      ? IconFont.icon12
                      : IconFont.icon13,
                  color: Get.reactiveTheme.hintColor,
                  size: 20.w,
                ),
              ],
            ),
          ),
        ),

        20.horizontalSpace,

        Container(
          width: 1.w,
          height: 20.w,
          color: Get.reactiveTheme.dividerColor,
        ),

        20.horizontalSpace,

        // Copy 按钮
        ClickOpacity(
          onTap: controller.copyCode,
          child: Row(
            mainAxisSize: MainAxisSize.min,
            children: [
              Text(
                Strings.copy.tr,
                style: TextStyle(
                  fontSize: 12.sp,
                  height: 1.7,
                  color: Get.reactiveTheme.hintColor,
                ),
              ),
              8.horizontalSpace,
              Icon(
                IconFont.icon57,
                color: Get.reactiveTheme.hintColor,
                size: 20.w,
              ),
            ],
          ),
        ),
      ],
    );
  }

  /// Send to Email 按钮
  Widget _buildEmailButton() {
    return SubmitButton(
      prefixIcon: IXImage(
        source: Assets.preCodeEmail,
        width: 20.w,
        height: 20.w,
        sourceType: ImageSourceType.asset,
      ),
      text: Strings.sendToEmail.tr,
      bgColor: Get.reactiveTheme.highlightColor,
      onPressed: controller.sendToEmail,
    );
  }

  /// Save Local Copy 按钮
  Widget _buildSaveButton() {
    return SubmitButton(
      prefixIcon: IXImage(
        source: Assets.preCodeSaveLocal,
        width: 20.w,
        height: 20.w,
        sourceType: ImageSourceType.asset,
      ),
      text: Strings.saveLocalCopy.tr,
      bgColor: Get.reactiveTheme.highlightColor,
      onPressed: controller.saveLocalCopy,
    );
  }
}
