import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';
import 'package:keyboard/base/base_view.dart';
import 'package:keyboard/module/user_info/enums/gender_type.dart';
import '../../../resource/assets.gen.dart';
import '../../../resource/colors.gen.dart';
import '../../../resource/string.gen.dart';
import '../../../widget/gradient_btn.dart';
import '../widget/intimacy_analyse_report_widget.dart';
import '../widget/intimacy_user_widget.dart';
import 'intimacy_analyse_report_view_controller.dart';

/// 亲密度分析-分析报告Tab页
class IntimacyAnalyseReportView
    extends BaseView<IntimacyAnalyseReportController> {
  const IntimacyAnalyseReportView({super.key});

  @override
  Color backgroundColor() {
    return Colors.transparent;
  }

  @override
  Widget buildBody(BuildContext context) {
    return Stack(
      children: [
        Positioned.fill(
          child: SizedBox(
            width: double.infinity,
            height: double.infinity,
            child: SingleChildScrollView(
              child: Obx(() {
                return Column(
                  children: [
                    SizedBox(height: 35.h),
                    // 亲密度档案
                    Row(
                      mainAxisAlignment: MainAxisAlignment.center,
                      children: [_buildIntimacyArchives()],
                    ),
                    SizedBox(height: 12.h),
                    // 切换亲密度
                    _buildIntimacyArchivesSwitchLayout(),
                    SizedBox(height: 18.h),
                    // 报告预览
                    IntimacyAnalyseReportWidget(
                      reportContent: controller.reportPreviewData.value,
                      isPreview: true,
                    ),
                    SizedBox(height: 120.h),
                  ],
                );
              }),
            ),
          ),
        ),
        // 底部阴影
        Positioned(
          left: 0,
          right: 0,
          bottom: 0,
          child: Container(
            child: Assets.images.bgIntimacyAnalyseReportPreviewMask.image(
              height: 126,
            ),
          ),
        ),
        // 解锁按钮
        Positioned.fill(
          child: Align(
            alignment: Alignment.bottomCenter,
            child: _buildBottomLayout(context),
          ),
        ),
      ],
    );
  }

  /// 亲密档案
  Widget _buildIntimacyArchives() {
    return Obx(() {
      // 是否要显示添加档案的布局
      bool isNeedShowAddProfileLayout =
          controller.currentKeyboardInfo.value == null;

      // 当前用户的头像
      String myAvatarUrl = controller.userInfo.value?.imageUrl ?? "";
      // 对方用户的头像
      String taAvatarUrl = controller.currentKeyboardInfo.value?.imageUrl ?? "";
      GenderType? myGender = GenderType.fromType(
        controller.userInfo.value?.gender,
      );
      GenderType? taGender = GenderType.fromType(
        controller.currentKeyboardInfo.value?.gender,
      );

      double layoutWidth = 185.w;
      return Stack(
        children: [
          IntimacyUserWidget(
            width: layoutWidth,
            height: 100.h,
            avatarSize: 96.0,
            myAvatar: myAvatarUrl,
            taAvatar: taAvatarUrl,
            myGender: myGender,
            taGender: taGender,
            avatarBorderWidth: 3.0,
            loveSize: 42.w,
          ),
          isNeedShowAddProfileLayout
              ? SizedBox(
                width: layoutWidth,
                child: Row(
                  mainAxisAlignment: MainAxisAlignment.spaceBetween,
                  mainAxisSize: MainAxisSize.min,
                  children: [
                    // 自己
                    Container(
                      margin: EdgeInsets.only(left: 12.w, top: 83.h),
                      child: _buildUserNameLabel(StringName.mySelf),
                    ),
                    // 添加
                    Container(
                      margin: EdgeInsets.only(right: 12.w, top: 83.h),
                      child: _buildAddProfileLabel(
                        StringName.intimacyAnalyseAdd,
                      ),
                    ),
                  ],
                ),
              )
              : SizedBox.shrink(),
        ],
      );
    });
  }

  /// 用户名称标签
  Widget _buildUserNameLabel(String name) {
    return Container(
      padding: EdgeInsets.only(left: 22.w, right: 22.w, top: 3.h, bottom: 8.h),
      decoration: BoxDecoration(
        image: DecorationImage(
          image: Assets.images.bgIntimacyAnalyseUserName.provider(),
          fit: BoxFit.fill,
        ),
      ),
      child: Text(
        name,
        style: TextStyle(
          color: ColorName.black80,
          fontSize: 14.sp,
          fontWeight: FontWeight.w400,
        ),
      ),
    );
  }

  /// 添加档案的按钮
  Widget _buildAddProfileLabel(String name) {
    return GestureDetector(
      onTap: () {
        controller.jump2ProfilePage();
      },
      child: Container(
        padding: EdgeInsets.only(
          left: 22.w,
          right: 22.w,
          top: 3.h,
          bottom: 8.h,
        ),
        decoration: BoxDecoration(
          image: DecorationImage(
            image: Assets.images.bgIntimacyAnalyseUserName.provider(),
            fit: BoxFit.fill,
          ),
        ),
        child: Text(
          name,
          style: TextStyle(
            color: const Color(0xFF8651FF),
            fontSize: 14.sp,
            fontWeight: FontWeight.w500,
          ),
        ),
      ),
    );
  }

  /// 亲密档案切换布局
  Widget _buildIntimacyArchivesSwitchLayout() {
    return Obx(() {
      String taName = controller.currentKeyboardInfo.value?.name ?? "";
      if (taName.isEmpty) {
        return SizedBox.shrink();
      }
      return GestureDetector(
        onTap: () {
          controller.clickIntimacyArchivesSwitch();
        },
        child: Row(
          mainAxisAlignment: MainAxisAlignment.center,
          children: [
            Text(
              // 自己&对方的名称
              "${StringName.mySelf}${StringName.andSymbol}$taName",
              style: TextStyle(
                color: ColorName.black80,
                fontSize: 13.sp,
                fontWeight: FontWeight.w500,
              ),
            ),
            SizedBox(width: 2.5.w),
            Assets.images.iconIntimacyAnalyseArrow.image(width: 15, height: 15),
          ],
        ),
      );
    });
  }

  /// 底部布局
  Widget _buildBottomLayout(BuildContext context) {
    return Column(
      // 高度包裹内容，否则会撑满父组件
      mainAxisSize: MainAxisSize.min,
      crossAxisAlignment: CrossAxisAlignment.start,
      children: [
        _buildReportPreviewBubble(),
        _buildUnlockBtn(context),
        SizedBox(height: 20.h),
      ],
    );
  }

  /// 报告预览布局-气泡
  Widget _buildReportPreviewBubble() {
    return Container(
      margin: EdgeInsets.only(left: 16, bottom: 3),
      padding: EdgeInsets.only(bottom: 3, right: 15),
      decoration: BoxDecoration(
        image: DecorationImage(
          image: Assets.images.bgIntimacyAnalyseReportPreviewBubble.provider(),
          fit: BoxFit.fill,
        ),
      ),
      child: Stack(
        children: [
          // 人物图片
          Assets.images.iconIntimacyAnalyseReportPreviewBubbleSymbol.image(
            width: 47,
            height: 28,
          ),
          Container(
            padding: EdgeInsets.only(left: 38, top: 7, bottom: 8),
            child: Text(
              StringName.intimacyAnalyseReportPreviewBubbleText,
              // 超出则显示省略号
              overflow: TextOverflow.ellipsis,
              // 单行显示
              maxLines: 1,
              style: TextStyle(
                color: ColorName.black65,
                fontSize: 11.sp,
                fontWeight: FontWeight.w400,
              ),
            ),
          ),
        ],
      ),
    );
  }

  /// 解锁按钮
  Widget _buildUnlockBtn(BuildContext context) {
    return Obx(() {
      return Container(
        margin: EdgeInsets.symmetric(horizontal: 16.w),
        width: double.maxFinite,
        child: GradientTextBtn.withUnlock(
          controller.memberInfo.value?.isMember ?? false,
          text: StringName.intimacyAnalyseAnalysisNow,
          onPressed: () {
            controller.clickUnlockBtn(context);
          },
        ),
      );
    });
  }
}
