import 'package:cs_resources/generated/l10n.dart';
import 'package:cs_resources/theme/app_colors_theme.dart';
import 'package:domain/entity/rewards_detail_entity.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:auto_route/auto_route.dart';
import 'package:flutter_hooks/flutter_hooks.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:shared/utils/color_utils.dart';
import 'package:widgets/ext/ex_widget.dart';
import 'package:widgets/load_state_layout.dart';
import 'package:widgets/my_appbar.dart';
import 'package:widgets/my_load_image.dart';
import 'package:widgets/my_text_view.dart';
import 'package:widgets/shatter/picker_container.dart';
import 'package:widgets/utils/dark_theme_util.dart';
import 'package:widgets/widget_export.dart';
import 'package:cs_resources/generated/assets.dart';

import '../../../router/page/rewards_page_router.dart';
import './rewards_confirm_vm.dart';

@RoutePage()
class RewardsConfirmPage extends HookConsumerWidget {
  final int? id;

  const RewardsConfirmPage({Key? key, @PathParam('id') required this.id}) : super(key: key);

  //启动当前页面
  static void startInstance({
    BuildContext? context,
    int? id,
  }) {
    if (context != null) {
      context.router.push(RewardsConfirmPageRoute(id: id));
    } else {
      appRouter.push(RewardsConfirmPageRoute(id: id));
    }
  }

  // list item
  Widget _buildSaleItem(BuildContext context, WidgetRef ref, _vm, detailInfo) {
    String title = detailInfo!.title ?? "";
    List? resources = detailInfo!.resources ?? [];
    return Column(
      children: [
        Container(
            decoration: BoxDecoration(
              color: context.appColors.whiteBG,
              borderRadius: const BorderRadius.all(Radius.circular(6.0)),
              boxShadow: [BoxShadow(color: context.appColors.itemBGShadow)],
            ),
            width: MediaQuery.of(context).size.width - 30,
            // height: 420,
            // margin: const EdgeInsets.only(left: 15, right: 15, top: 12.5),
            child: Column(
              crossAxisAlignment: CrossAxisAlignment.start,
              mainAxisAlignment: MainAxisAlignment.start,
              children: [
                resources!.length > 0
                    ? MyLoadImage(
                        resources[0] ?? '',
                        width: MediaQuery.of(context).size.width,
                        height: 150,
                      )
                    : const SizedBox.shrink(),
                Column(
                  crossAxisAlignment: CrossAxisAlignment.start,
                  mainAxisAlignment: MainAxisAlignment.start,
                  children: [
                    Text(
                      maxLines: 1, // 设置最大行数为2
                      overflow: TextOverflow.ellipsis, // 超出部分用省略号表示
                      title,
                      style: TextStyle(fontSize: 17.0, color: context.appColors.textBlack, fontWeight: FontWeight.w500),
                    ),
                  ],
                ).paddingOnly(left: 15, right: 15, top: 10, bottom: 30),
              ],
            )),
      ],
    ).marginOnly(left: 15, bottom: 15, right: 15);
  }

  Widget _buildDeal(BuildContext context, WidgetRef ref, _vm) {
    return Column(
      children: [
        Container(
          decoration: BoxDecoration(
            color: context.appColors.whiteBG,
            borderRadius: const BorderRadius.all(Radius.circular(6.0)),
            boxShadow: [BoxShadow(color: context.appColors.itemBGShadow)],
          ),
          width: MediaQuery.of(context).size.width - 30,
          child: Column(
            crossAxisAlignment: CrossAxisAlignment.start,
            children: [
              Row(
                mainAxisSize: MainAxisSize.min,
                mainAxisAlignment: MainAxisAlignment.start,
                crossAxisAlignment: CrossAxisAlignment.center,
                children: [
                  MyAssetImage(
                    Assets.rewardsRewardsDetailDeal,
                    width: 25,
                    height: 25,
                    color: DarkThemeUtil.multiColors(context, AppColorsTheme.colorPrimary, darkColor: Colors.white),
                  ).marginOnly(right: 5),
                  Text(
                    S.current.redeem_deal_at,
                    style: TextStyle(fontSize: 15.0, color: context.appColors.textBlack, fontWeight: FontWeight.w500),
                  )
                ],
              ),
              Column(
                mainAxisAlignment: MainAxisAlignment.spaceBetween,
                crossAxisAlignment: CrossAxisAlignment.center,
                children: [
                  Text(
                    '60 paya lebar rd, #09-13/17 paya lebar square, singapore 409051',
                    maxLines: 2, // 设置最大行数为2
                    overflow: TextOverflow.ellipsis, // 超出部分用省略号表示
                    style: TextStyle(
                        fontSize: 15.0,
                        color: DarkThemeUtil.multiColors(context, ColorUtils.string2Color('#54638C'), darkColor: Colors.white),
                        fontWeight: FontWeight.w400),
                  ),
                ],
              ),
            ],
          ).paddingOnly(left: 15, right: 15, top: 12, bottom: 20),
        ),
      ],
    ).marginOnly(left: 15, bottom: 15, right: 15);
  }

  Widget _buildPackage(BuildContext context, WidgetRef ref, _vm, detailInfo) {
    String redeemedStart = detailInfo.redeemedStart ?? "";
    String redeemedEnd = detailInfo.redeemedEnd ?? "";
    String redeemedDate = '$redeemedStart-$redeemedEnd';
    return Column(
      children: [
        Container(
          decoration: BoxDecoration(
            color: context.appColors.whiteBG,
            borderRadius: const BorderRadius.all(Radius.circular(6.0)),
            boxShadow: [BoxShadow(color: context.appColors.itemBGShadow)],
          ),
          width: MediaQuery.of(context).size.width - 30,
          child: Column(
            crossAxisAlignment: CrossAxisAlignment.start,
            children: [
              Row(
                mainAxisSize: MainAxisSize.min,
                mainAxisAlignment: MainAxisAlignment.start,
                crossAxisAlignment: CrossAxisAlignment.center,
                children: [
                   MyAssetImage(
                    Assets.rewardsRewardsIconDatd,
                    width: 15,
                    height: 15,
                     color: DarkThemeUtil.multiColors(context, AppColorsTheme.colorPrimary, darkColor: Colors.white),
                  ).marginOnly(right: 10),
                  Text(
                    S.current.redeem_from,
                    style: TextStyle(fontSize: 15.0, color: context.appColors.textBlack, fontWeight: FontWeight.w500),
                  )
                ],
              ).marginOnly(bottom: 10),
              Column(
                crossAxisAlignment: CrossAxisAlignment.start,
                children: [
                  Text(
                    redeemedDate,
                    style: TextStyle(
                        fontSize: 15.0,
                        color: DarkThemeUtil.multiColors(context, ColorUtils.string2Color('#54638C'), darkColor: Colors.white),
                        fontWeight: FontWeight.w400),
                  ),
                ],
              ),
            ],
          ).paddingOnly(left: 15, right: 15, top: 12, bottom: 20),
        ),
      ],
    ).marginOnly(left: 15, bottom: 15, right: 15);
  }

  Widget _buildNotice(BuildContext context, WidgetRef ref, _vm, detailInfo) {
    final state = ref.watch(rewardsConfirmVmProvider);
    int number = state.number ?? 0;
    int amount = state.amount ?? 0;
    int point = detailInfo.point ?? 0;
    int originalPoint = detailInfo.originalPoint ?? 0;
    return Column(
      children: [
        Container(
          decoration: BoxDecoration(
            color: context.appColors.whiteBG,
            borderRadius: const BorderRadius.all(Radius.circular(6.0)),
            boxShadow: [BoxShadow(color: context.appColors.itemBGShadow)],
          ),
          width: MediaQuery.of(context).size.width - 30,
          child: Column(
            crossAxisAlignment: CrossAxisAlignment.start,
            children: [
              Row(
                mainAxisAlignment: MainAxisAlignment.spaceBetween,
                crossAxisAlignment: CrossAxisAlignment.center,
                children: [
                  Text(
                    "${S.current.quantity}:",
                    style: TextStyle(fontSize: 15.0, color: context.appColors.tabTextUnSelectedPrimary, fontWeight: FontWeight.w400),
                  ),
                  Row(
                    children: [
                      MyAssetImage(
                        Assets.rewardsRewardsJian,
                        width: 21,
                        height: 21,
                        color: DarkThemeUtil.multiColors(context, AppColorsTheme.colorPrimary, darkColor: Colors.white),
                      ).onTap(() {
                        _vm.numberDec();
                      }),

                      Text(
                        '$number',
                        style: TextStyle(fontSize: 19.0, color: context.appColors.tabTextUnSelectedPrimary, fontWeight: FontWeight.w500),
                      ).marginOnly(left: 15, right: 15),

                      MyAssetImage(
                        Assets.rewardsRewardsJia,
                        width: 21,
                        height: 21,
                        color: DarkThemeUtil.multiColors(context, AppColorsTheme.colorPrimary, darkColor: Colors.white),
                      ).onTap(() {
                        _vm.numberAdd();
                      }),
                    ],
                  ),
                ],
              ),
              Row(
                mainAxisAlignment: MainAxisAlignment.spaceBetween,
                crossAxisAlignment: CrossAxisAlignment.center,
                children: [
                  Text(
                    '${S.current.points}:',
                    style: TextStyle(fontSize: 15.0, color: context.appColors.tabTextUnSelectedPrimary, fontWeight: FontWeight.w400),
                  ),
                  Row(
                    children: [
                      Text(
                        '$originalPoint',
                        style: TextStyle(
                            decoration: TextDecoration.lineThrough,
                            decorationColor: DarkThemeUtil.multiColors(context, ColorUtils.string2Color('#54638C'), darkColor: Colors.white),
                            decorationStyle: TextDecorationStyle.solid,
                            fontSize: 14.0,
                            color: DarkThemeUtil.multiColors(context, ColorUtils.string2Color('#54638C'), darkColor: Colors.white),
                            fontWeight: FontWeight.w400),
                      ),
                      Text(
                        '$point',
                        style: TextStyle(fontSize: 19.0, color: context.appColors.tabTextUnSelectedPrimary, fontWeight: FontWeight.w500),
                      ).marginOnly(left: 10),
                    ],
                  ),
                ],
              ).marginOnly(top: 30, bottom: 30),
              Row(
                mainAxisAlignment: MainAxisAlignment.spaceBetween,
                crossAxisAlignment: CrossAxisAlignment.center,
                children: [
                  Text(
                    '${S.current.amount}:',
                    style: TextStyle(fontSize: 15.0, color: context.appColors.tabTextUnSelectedPrimary, fontWeight: FontWeight.w400),
                  ),
                  Row(
                    children: [
                      Text(
                        '$amount',
                        style: TextStyle(
                            fontSize: 19.0,
                            color: DarkThemeUtil.multiColors(context, AppColorsTheme.colorPrimary, darkColor: Colors.white),
                            fontWeight: FontWeight.w500),
                      ),
                    ],
                  ),
                ],
              ),
            ],
          ).paddingOnly(left: 15, right: 15, top: 30, bottom: 30),
        ),
      ],
    ).marginOnly(left: 15, bottom: 15, right: 15);
  }

  @override
  Widget build(BuildContext context, WidgetRef ref) {
    final _vm = ref.read(rewardsConfirmVmProvider.notifier);
    final state = ref.watch(rewardsConfirmVmProvider);
    RewardsDetailEntity? detailInfo = state.detailInfo;
    useEffect(() {
      // 组件挂载时执行 - 执行接口请求
      Future.microtask(() => _vm.initPageData(id: id));
      return () {
        // 组件卸载时执行
        Log.d("property_news_page 组件卸载时执行");
      };
    }, []);
    return Scaffold(
      appBar: MyAppBar.appBar(
        context,
        S.current.confirm_details,
        backgroundColor: context.appColors.whiteBG,
      ),
      body: LoadStateLayout(
          state: state.loadingState,
          errorMessage: state.errorMessage,
          errorRetry: () {
            _vm.retryRequest(id: id);
          },
          successWidget: Column(
            children: [
              Expanded(
                  child: SingleChildScrollView(
                      scrollDirection: Axis.vertical,
                      physics: const BouncingScrollPhysics(),
                      clipBehavior: Clip.none,
                      child: Column(
                        children: [
                          Container(
                              color: context.appColors.backgroundDark,
                              padding: const EdgeInsets.only(top: 15),
                              child: Column(
                                children: [
                                  _buildSaleItem(context, ref, _vm, detailInfo),
                                  _buildDeal(context, ref, _vm),
                                  _buildPackage(context, ref, _vm, detailInfo),
                                  _buildNotice(context, ref, _vm, detailInfo),
                                ],
                              )),
                        ],
                      ))),
              Container(
                height: 50,
                color: context.appColors.btnBgDefault,
                child: Flex(
                  direction: Axis.horizontal,
                  children: [
                    Expanded(
                      flex: 1,
                      child: Container(
                        color: context.appColors.btnBgDefault,
                        height: 100,
                        child: Row(
                          mainAxisAlignment: MainAxisAlignment.center,
                          crossAxisAlignment: CrossAxisAlignment.center,
                          children: [
                            MyTextView(
                              S.current.redeem,
                              fontSize: 16,
                              textColor: Colors.white,
                              isFontMedium: true,
                            ),
                          ],
                        ),
                      ).onTap(() {
                        // 去详情
                        _vm.doDeleteAccount();
                      }),
                    ),
                  ],
                ),
              )
            ],
          )),
    );
  }
}
