import 'dart:io';

import 'package:flutter/cupertino.dart';
import 'package:flutter/gestures.dart';
import 'package:flutter/material.dart';
import 'package:flutter/src/widgets/framework.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';
import 'package:get/get_core/src/get_main.dart';
import 'package:location/base/base_page.dart';
import 'package:location/data/bean/goods_bean.dart';
import 'package:location/module/member/activity/member_activity_banner_widget.dart';
import 'package:location/resource/assets.gen.dart';
import 'package:location/resource/colors.gen.dart';
import 'package:location/resource/string.gen.dart';
import 'package:location/utils/common_expand.dart';
import 'package:location/utils/project_expand.dart';
import '../../../resource/fonts.gen.dart';
import '../../../router/app_pages.dart';
import '../../../widget/activity_countdown_txt_view.dart';
import '../../../widget/activity_countdown_view.dart';
import '../../../widget/shimmer_effect.dart';
import 'member_activity_controller.dart';

class MemberActivityPage extends BasePage<MemberActivityController> {
  const MemberActivityPage({super.key});

  static void start() {
    Get.toNamed(RoutePath.memberActivity);
  }

  @override
  bool immersive() {
    return true;
  }

  @override
  bool statusBarDarkFont() {
    return false;
  }

  @override
  Widget buildBody(BuildContext context) {
    return Stack(
      children: [
        buildActivityContent(),
        buildToolbar(),
        buildMemberBottomView()
      ],
    );
  }

  Widget buildToolbar() {
    return SafeArea(
      child: SizedBox(
        width: double.infinity,
        height: 56.w,
        child: Stack(
          children: [
            Positioned(
              left: 10,
              top: 0,
              bottom: 0,
              child: GestureDetector(
                onTap: controller.onBack,
                child: Container(
                    padding: EdgeInsets.only(
                        left: 10.w, right: 12.w, top: 10.w, bottom: 10.w),
                    child: Assets.images.iconMemberActivityClose
                        .image(width: 8.w)),
              ),
            ),
            Center(
              child: Text(
                StringName.memberActivityTitle,
                style: TextStyle(
                    fontSize: 18.sp,
                    color: Colors.white,
                    fontWeight: FontWeight.bold),
              ),
            ),
            Positioned(
              right: 10.w,
              top: 0,
              bottom: 0,
              child: Obx(() {
                return Visibility(
                  visible: controller.accountRepository.isLogin.value &&
                      Platform.isIOS,
                  child: GestureDetector(
                    onTap: controller.onRecoverClick,
                    child: Row(
                      children: [
                        Assets.images.iconAppleRecoverSubscribe
                            .image(width: 14.w, height: 14.w),
                        SizedBox(width: 1.w),
                        Text(StringName.appleRecoverSubscribeTxt,
                            style: TextStyle(
                                fontSize: 11.sp,
                                color: Colors.white,
                                fontWeight: FontWeight.bold))
                      ],
                    ),
                  ),
                );
              }),
            )
          ],
        ),
      ),
    );
  }

  Widget buildActivityContent() {
    return SizedBox(
      width: double.infinity,
      height: double.infinity,
      child: SingleChildScrollView(
        child: Stack(
          children: [
            Assets.images.bgMemberActivity
                .image(width: double.infinity, fit: BoxFit.cover),
            SafeArea(
              child: Column(
                crossAxisAlignment: CrossAxisAlignment.start,
                children: [
                  SizedBox(height: 83.w),
                  Container(
                      margin: EdgeInsets.only(left: 16.w),
                      child: Assets.images.iconMemberActivityCountdown
                          .image(height: 22.w)),
                  SizedBox(height: 10.w),
                  Container(
                    margin: EdgeInsets.only(left: 16.w, bottom: 16.w),
                    child: Assets.images.imgMemberActivityFavourableTxt
                        .image(height: 20.w),
                  ),
                  buildGoodsContainer(),
                ],
              ),
            )
          ],
        ),
      ),
    );
  }

  Widget buildGoodsContainer() {
    return Container(
      width: double.infinity,
      decoration: BoxDecoration(
        gradient: LinearGradient(
            stops: const [0.0, 0.16],
            colors: ['#E4E4FF'.color, Colors.white],
            begin: Alignment.topCenter,
            end: Alignment.bottomCenter),
        borderRadius: BorderRadius.only(
            topLeft: Radius.circular(20.w), topRight: Radius.circular(20.w)),
      ),
      child: Stack(
        children: [
          Assets.images.bgMemberActivityContainer
              .image(width: double.infinity, fit: BoxFit.fill),
          Column(
            crossAxisAlignment: CrossAxisAlignment.start,
            children: [
              SizedBox(height: 14.w),
              buildFunTitleView(),
              buildMemberActivityFunctionIntroduction(),
              SizedBox(height: 8.w),
              buildGoodsListView(),
              buildPrivacyPolicyView(),
              SizedBox(height: 150.w),
            ],
          )
        ],
      ),
    );
  }

  Widget buildFunTitleView() {
    return Container(
      margin: EdgeInsets.only(left: 16.w, bottom: 18.w),
      child: Stack(
        children: [
          Container(
            width: 117.w,
            height: 8.w,
            decoration: BoxDecoration(
                gradient: LinearGradient(
                  colors: ['#AA70FE'.color, '#006A2DC4'.color],
                  begin: Alignment.centerLeft,
                  end: Alignment.centerRight,
                ),
                borderRadius: BorderRadius.only(
                    topLeft: Radius.circular(10.w),
                    bottomLeft: Radius.circular(10.w))),
            margin: EdgeInsets.only(top: 15.5.w),
          ),
          Container(
            margin: EdgeInsets.only(left: 4.w),
            child: Text(
              StringName.memberActivityFunTitle,
              style: TextStyle(
                  fontSize: 15.sp,
                  color: '#202020'.color,
                  fontWeight: FontWeight.bold),
            ),
          ),
        ],
      ),
    );
  }

  Widget buildPrivacyPolicyView() {
    return Padding(
      padding: EdgeInsets.only(left: 15.w),
      child: RichText(
          text: TextSpan(
              style: TextStyle(fontSize: 12.sp, color: ColorName.black40),
              children: [
            TextSpan(text: '购买前请先阅读'),
            TextSpan(
                recognizer: TapGestureRecognizer()
                  ..onTap = () {
                    controller.onPrivacyPolicyClick();
                  },
                text: '隐私政策',
                style: TextStyle(
                    color: ColorName.black60,
                    decoration: TextDecoration.underline)),
            TextSpan(text: '&'),
            TextSpan(
                recognizer: TapGestureRecognizer()
                  ..onTap = () {
                    controller.onTermOfServiceClick();
                  },
                text: '服务条款',
                style: TextStyle(
                    color: ColorName.black60,
                    decoration: TextDecoration.underline)),
            if (Platform.isIOS) TextSpan(text: '&'),
            if (Platform.isIOS)
              TextSpan(
                  recognizer: TapGestureRecognizer()
                    ..onTap = () {
                      controller.onRenewalAgreementClick();
                    },
                  text: '会员协议',
                  style: TextStyle(
                      color: ColorName.black60,
                      decoration: TextDecoration.underline)),
          ])),
    );
  }

  Widget buildCountdownSeparator() {
    return Container(
      margin: EdgeInsets.symmetric(horizontal: 2.w),
      child: IntrinsicHeight(
        child: Column(
          children: [
            Container(
              width: 2.w,
              height: 2.w,
              decoration: BoxDecoration(
                color: '#FF5656'.color,
                shape: BoxShape.circle,
              ),
            ),
            SizedBox(height: 3.w),
            Container(
              width: 2.w,
              height: 2.w,
              decoration: BoxDecoration(
                color: '#FF5656'.color,
                shape: BoxShape.circle,
              ),
            )
          ],
        ),
      ),
    );
  }

  Widget buildMemberBottomView() {
    return Obx(() {
      if (controller.memberStatusInfo != null &&
          controller.memberStatusInfo?.permanent == true) {
        return SizedBox(); // 不显示
      }

      return Align(
          alignment: Alignment.bottomCenter,
          child: Container(
            margin: EdgeInsets.only(bottom: 15.w, left: 12.w, right: 12.w),
            child: Stack(
              children: [
                Container(
                  width: 336.w,
                  height: 47.w,
                  decoration: BoxDecoration(
                      borderRadius: BorderRadius.only(
                        topLeft: Radius.circular(30.w),
                        topRight: Radius.circular(30.w),
                      ),
                      color: '#FFFED8'.color),
                  child: Align(
                    alignment: Alignment(0.0, -0.75),
                    child: Row(
                      mainAxisAlignment: MainAxisAlignment.center,
                      children: [
                        Text(
                          StringName.memberActivityCountdown,
                          style: TextStyle(
                              fontSize: 11.sp, color: '#FF5656'.color),
                        ),
                        SizedBox(width: 4.w),
                        Obx(() {
                          return ActivityCountdownTextView(
                              timeItemHeight: 15.w,
                              contentPadding: EdgeInsets.zero,
                              timeItemWidth: 16.w,
                              textStyle: TextStyle(
                                  fontSize: 10.sp, color: Colors.white),
                              duration: controller.activityDuration ??
                                  Duration(seconds: 0),
                              separator: buildCountdownSeparator(),
                              timeBgBoxDecoration: BoxDecoration(
                                color: '#FF5656'.color,
                                borderRadius: BorderRadius.circular(3.w),
                              ));
                        }),
                        SizedBox(width: 4.w),
                        Text(
                          StringName.memberActivitySpeciallyPreferential,
                          style: TextStyle(
                              fontSize: 10.sp, color: '#FF5656'.color),
                        )
                      ],
                    ),
                  ),
                ),
                GestureDetector(
                  onTap: controller.onBuyClick,
                  child: Container(
                    margin: EdgeInsets.only(top: 24.w),
                    height: 50.w,
                    width: 336.w,
                    child: ShimmerEffect(
                      image: Assets.images.imgMemberBtnShadow.provider(),
                      shimmerWidthFactor: 0.244047619047619,
                      duration: Duration(milliseconds: 3000),
                      delay: Duration(milliseconds: 800),
                      child: Container(
                        height: 50.w,
                        width: 336.w,
                        alignment: Alignment.center,
                        decoration: BoxDecoration(
                            image: DecorationImage(
                                image: Assets.images.iconMemberSettlementBg
                                    .provider(),
                                fit: BoxFit.fill)),
                        child: Text(
                          StringName.memberVipUnlock,
                          style: TextStyle(
                              fontSize: 18.sp,
                              color: '#FFF8EF'.color,
                              fontWeight: FontWeight.bold),
                        ),
                      ),
                    ),
                  ),
                )
              ],
            ),
          ));
    });
  }

  Widget buildMemberActivityFunctionIntroduction() {
    return MemberActivityBannerWidget(
        bannerHeight: 145.w,
        viewportFraction: 0.88,
        itemCount: controller.funImages.length,
        itemBuilder: (context, index) {
          final image = controller.funImages[index];
          return Container(
            margin: EdgeInsets.symmetric(horizontal: 5.w),
            child: Image(
                image: image,
                width: double.infinity,
                height: double.infinity,
                fit: BoxFit.fill),
          );
        });
  }

  Widget buildGoodsListView() {
    return IntrinsicHeight(
      child: Obx(() {
        return Column(
          children: [
            for (int i = 0; i < controller.goodsList.length; i++)
              if (i == 0)
                buildFavourableGoodsView(controller.goodsList[i])
              else
                buildNormalGoodsView(controller.goodsList[i])
          ],
        );
      }),
    );
  }

  Widget buildFavourableGoodsView(GoodsBean goodsInfo) {
    bool isSelected = controller.selectedGoods?.id == goodsInfo.id;
    return GestureDetector(
      onTap: () {
        controller.onGoodsItemClick(goodsInfo);
      },
      child: Container(
        height: 96.w,
        width: double.infinity,
        margin: EdgeInsets.only(left: 12.w, right: 16.w, bottom: 12.w),
        decoration: BoxDecoration(
          image: DecorationImage(
            image: isSelected
                ? Assets.images.iconMemberSpecialProductsSelect.provider()
                : Assets.images.iconMemberSpecialProductsNormal.provider(),
            fit: BoxFit.fill,
          ),
        ),
        child: Stack(
          children: [
            Positioned(
              top: 10.w,
              bottom: 0,
              left: 24.w,
              child: Column(
                mainAxisAlignment: MainAxisAlignment.center,
                children: [
                  RichText(
                      text: TextSpan(
                          style: TextStyle(
                              color: isSelected
                                  ? '#FF5656'.color
                                  : "#323133".color,
                              fontWeight: FontWeight.bold),
                          children: [
                        TextSpan(
                            text: '￥',
                            style: TextStyle(fontSize: 16.sp, height: 1)),
                        TextSpan(
                            text: goodsInfo.amount.divideBy100(),
                            style: TextStyle(
                              fontSize: 28.sp,
                              height: 1,
                              //fontFamily: FontFamily.oppoSans
                            ))
                      ])),
                  Text('￥${goodsInfo.originalAmount.divideBy100()}',
                      style: TextStyle(
                          decoration: TextDecoration.lineThrough,
                          decorationColor: ColorName.black40,
                          decorationThickness: 1.0,
                          fontSize: 12.sp,
                          color: ColorName.black40))
                ],
              ),
            ),
            Positioned(
              top: 10.w,
              bottom: 0,
              left: 105.w,
              child: Column(
                crossAxisAlignment: CrossAxisAlignment.start,
                mainAxisAlignment: MainAxisAlignment.center,
                children: [
                  Text(
                    goodsInfo.name,
                    style: TextStyle(
                        fontSize: 17.sp,
                        color: "#333333".color,
                        fontWeight: FontWeight.bold),
                  ),
                  SizedBox(
                    height: 3.w,
                  ),
                  Text(
                    goodsInfo.description ?? "",
                    style: TextStyle(
                        fontSize: 11.sp,
                        color: "#9191BA".color,
                        fontWeight: FontWeight.bold),
                  ),
                ],
              ),
            ),
          ],
        ),
      ),
    );
  }

  Widget buildNormalGoodsView(GoodsBean goodsInfo) {
    bool isSelected = controller.selectedGoods?.id == goodsInfo.id;
    return GestureDetector(
      onTap: () {
        controller.onGoodsItemClick(goodsInfo);
      },
      child: Container(
        width: double.infinity,
        height: 72.w,
        margin: EdgeInsets.only(left: 16.w, right: 16.w, bottom: 12.w),
        child: Stack(
          children: [
            Container(
                width: double.infinity,
                height: double.infinity,
                decoration: BoxDecoration(
                    gradient: isSelected
                        ? LinearGradient(
                            colors: ['#FFFFFF'.color, '#F3EAFF'.color],
                            begin: Alignment.topLeft,
                            end: Alignment.bottomRight,
                            stops: const [0.4, 1.0])
                        : null,
                    borderRadius: BorderRadius.circular(12.w),
                    border: Border.all(
                      color: isSelected ? '#7A13C6'.color : '#DDDDDD'.color,
                      width: isSelected ? 2.5.w : 1.w,
                    ))),
            Stack(
              children: [
                Positioned(
                  top: 4.w,
                  bottom: 0,
                  left: 20.w,
                  child: Column(
                    mainAxisAlignment: MainAxisAlignment.center,
                    children: [
                      RichText(
                          text: TextSpan(
                              style: TextStyle(
                                  fontFamily: FontFamily.oppoSans,
                                  color: isSelected
                                      ? '#FF5656'.color
                                      : "#323133".color,
                                  fontWeight: FontWeight.bold),
                              children: [
                            TextSpan(
                                text: '￥',
                                style: TextStyle(fontSize: 16.sp, height: 1)),
                            TextSpan(
                                text: goodsInfo.amount.divideBy100(),
                                style: TextStyle(
                                  fontSize: 28.sp,
                                  height: 1,
                                  //fontFamily: FontFamily.oppoSans
                                ))
                          ])),
                      Text('￥${goodsInfo.originalAmount.divideBy100()}',
                          style: TextStyle(
                              decoration: TextDecoration.lineThrough,
                              decorationColor: ColorName.black40,
                              decorationThickness: 1.0,
                              fontSize: 12.sp,
                              color: ColorName.black40))
                    ],
                  ),
                ),
                Positioned(
                  top: 0.w,
                  bottom: 0,
                  left: 101.w,
                  child: Column(
                    crossAxisAlignment: CrossAxisAlignment.start,
                    mainAxisAlignment: MainAxisAlignment.center,
                    children: [
                      Text(
                        goodsInfo.name,
                        style: TextStyle(
                            fontSize: 17.sp,
                            color: "#333333".color,
                            fontWeight: FontWeight.bold),
                      ),
                      SizedBox(
                        height: 3.w,
                      ),
                      Text(
                        goodsInfo.description ?? "",
                        style: TextStyle(
                            fontSize: 11.sp,
                            color: "#9191BA".color,
                            fontWeight: FontWeight.bold),
                      ),
                    ],
                  ),
                ),
              ],
            ),
          ],
        ),
      ),
    );
  }
}
