import 'package:cs_resources/generated/l10n.dart';
import 'package:cs_resources/theme/app_colors_theme.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:auto_route/auto_route.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/my_appbar.dart';
import 'package:widgets/my_load_image.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_address_vm.dart';

@RoutePage()
class RewardsAddressPage extends HookConsumerWidget {
  const RewardsAddressPage({Key? key}) : super(key: key);

  //启动当前页面
  static void startInstance({BuildContext? context}) {
    if (context != null) {
      context.router.push(const RewardsAddressPageRoute());
    } else {
      appRouter.push(const RewardsAddressPageRoute());
    }
  }

  Widget _buildItemLeftSection(BuildContext context, WidgetRef ref, item, _vm) {
    return Column(
      children: [
        Column(
          crossAxisAlignment: CrossAxisAlignment.start,
          children: [
            Text(
              maxLines: 1, // 设置最大行数为2
              overflow: TextOverflow.ellipsis, // 超出部分用省略号表示
              'Delibowl Group',
              style: TextStyle(fontSize: 16.0, color: context.appColors.textBlack, fontWeight: FontWeight.w500),
            ),
            Row(
              crossAxisAlignment: CrossAxisAlignment.center,
              mainAxisAlignment: MainAxisAlignment.center,
              children: [
                 MyAssetImage(
                  Assets.rewardsRewardsIconAddes,
                  width: 15,
                  height: 19,
                  color: DarkThemeUtil.multiColors(context, AppColorsTheme.colorPrimary, darkColor: Colors.white),
                ).marginOnly(right: 10),
                Expanded(
                  child: Text(
                    maxLines: 2, // 设置最大行数为2
                    overflow: TextOverflow.ellipsis,
                    softWrap: true, // 启用自动换行
                    '176 Orchard Rd, #02-29/30 Song Fa Bak Kut Teh The Centrepoint, Singapore 238843',
                    style: TextStyle(
                        fontSize: 15.0,
                        color: DarkThemeUtil.multiColors(context, Color.fromARGB(255, 69, 33, 33), darkColor: Colors.white),
                        fontWeight: FontWeight.w400),
                  ),
                ),
              ],
            ).marginOnly(top: 10, bottom: 10),
            Container(
              height: 40,
              decoration: BoxDecoration(
                color: Colors.white,
                borderRadius: const BorderRadius.all(Radius.circular(6.0)),
                border: Border.all(color: context.appColors.textPrimary, width: 1.0),
              ),
              child: Center(
                child: Text(
                  '+65 26593458',
                  style: TextStyle(fontSize: 15.0, color: context.appColors.textPrimary, fontWeight: FontWeight.w500),
                ),
              ),
            ),
          ],
        ),
      ],
    );
  }

  // listitem
  Widget _buildSaleItem(BuildContext context, WidgetRef ref, item, _vm) {
    return Container(
      decoration: BoxDecoration(
        color: context.appColors.whiteBG,
        borderRadius: const BorderRadius.all(Radius.circular(6.0)),
        boxShadow: const [BoxShadow(color: Color.fromRGBO(184, 191, 217, 0.3), blurRadius: 6)],
      ),
      child: Column(
        children: [
          SizedBox(
            width: MediaQuery.of(context).size.width - 30,
            height: 160,
            // margin: const EdgeInsets.only(left: 15, right: 15, top: 12.5),
            child: Column(
              crossAxisAlignment: CrossAxisAlignment.start,
              mainAxisAlignment: MainAxisAlignment.start,
              children: [_buildItemLeftSection(context, ref, item, _vm)],
            ).paddingOnly(left: 20, right: 20, top: 15),
          ).constrained(
            minHeight: 160,
          ),
        ],
      ),
    ).marginOnly(left: 15, bottom: 15, right: 15);
  }

  // list
  Widget _buildSaleList(BuildContext context, WidgetRef ref, _vm) {
    List itemsList = _vm.state.list.toList();
    return ListView.builder(
      itemCount: itemsList.length,
      itemBuilder: (context, index) {
        return _buildSaleItem(context, ref, itemsList[index], _vm);
      },
    );
  }

  @override
  Widget build(BuildContext context, WidgetRef ref) {
    final _vm = ref.read(rewardsAddressVmProvider.notifier);
    final state = ref.watch(rewardsAddressVmProvider);
    return Scaffold(
      appBar: MyAppBar.appBar(
        context,
        S.current.redeem_deal_at,
        backgroundColor: context.appColors.whiteBG,
      ),
      body: Container(
        child: Column(
          children: [
            Expanded(
              child: EasyRefresh(
                // 上拉加载
                onLoad: () async {
                  Log.d("----onLoad");
                  _vm.onLoadData();
                },
                // 下拉刷新
                onRefresh: () async {
                  Log.d("----onRefresh");
                  _vm.refreshListData();
                },
                child: Container(color: context.appColors.backgroundDark, padding: const EdgeInsets.only(top: 15), child: _buildSaleList(context, ref, _vm)),
              ),
            )
          ],
        ),
      ),
    );
  }
}
