import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:yumi/app_localizations.dart';
import 'package:yumi/shared/tools/sc_date_utils.dart';
import 'package:yumi/app/constants/sc_global_config.dart';
import 'package:yumi/ui_kit/components/appbar/socialchat_appbar.dart';
import 'package:yumi/shared/data_sources/sources/repositories/sc_user_repository_impl.dart';

import '../../../shared/business_logic/models/res/sc_gold_record_res.dart';
import '../../../ui_kit/components/sc_page_list.dart';
import '../../../ui_kit/components/text/sc_text.dart';

class GoldRecordPage extends SCPageList {
  @override
  _GoldRecordPageState createState() => _GoldRecordPageState();
}

class _GoldRecordPageState
    extends SCPageListState<SCGoldRecordRes, GoldRecordPage> {
  String? lastId;

  @override
  void initState() {
    super.initState();
    enablePullUp = true;
    isShowDivider = false;
    backgroundColor = SCGlobalConfig.businessLogicStrategy.getGoldRecordPageListBackgroundColor();
    loadData(1);
  }

  @override
  Widget build(BuildContext context) {
    return Stack(
      children: [
        Image.asset(
          SCGlobalConfig.businessLogicStrategy.getGoldRecordPageBackgroundImage(),
          width: ScreenUtil().screenWidth,
          height: ScreenUtil().screenHeight,
          fit: BoxFit.fill,
        ),
        Scaffold(
          backgroundColor: SCGlobalConfig.businessLogicStrategy.getGoldRecordPageScaffoldBackgroundColor(),
          resizeToAvoidBottomInset: false,
          appBar: SocialChatStandardAppBar(
            title: SCAppLocalizations.of(context)!.goldList,
            actions: [],
          ),
          body: SafeArea(top: false,child: buildList(context),),
        ),
      ],
    );
  }

  @override
  Widget buildItem(SCGoldRecordRes res) {
    return GestureDetector(
      child: Container(
        decoration: BoxDecoration(
          borderRadius: BorderRadius.circular(8),
          color: SCGlobalConfig.businessLogicStrategy.getGoldRecordPageContainerBackgroundColor(),
          border: Border.all(color: SCGlobalConfig.businessLogicStrategy.getGoldRecordPageBorderColor(), width: 1.w),
        ),
        margin: EdgeInsets.symmetric(horizontal: 10.w, vertical: 3.w),
        padding: EdgeInsets.symmetric(vertical: 5.w),
        child: Row(
          children: [
            SizedBox(width: 10.w),
            Container(
              constraints: BoxConstraints(maxWidth: 220.w),
              child: text(
                res.title ?? "",
                fontSize: 13.sp,
                textColor: SCGlobalConfig.businessLogicStrategy.getGoldRecordPagePrimaryTextColor(),
                fontWeight: FontWeight.w600,
              ),
            ),
            Expanded(
              child: Column(
                crossAxisAlignment: CrossAxisAlignment.end,
                children: [
                  text(
                    "${res.type == 0 ? "+" : "-"}${res.quantity} ${SCAppLocalizations.of(context)!.coins}",
                    fontSize: 13.sp,
                    textColor: SCGlobalConfig.businessLogicStrategy.getGoldRecordPagePrimaryTextColor(),
                    fontWeight: FontWeight.w600,
                  ),
                  SizedBox(height: 4.w),
                  text(
                    SCMDateUtils.formatDateTime2(
                      DateTime.fromMillisecondsSinceEpoch(res.createTime ?? 0),
                    ),
                    fontSize: 11.sp,
                    textColor: SCGlobalConfig.businessLogicStrategy.getGoldRecordPageSecondaryTextColor(),
                  ),
                ],
              ),
            ),
            SizedBox(width: 12.w),
          ],
        ),
      ),
      onTap: () {},
    );
  }

  ///加载数据
  @override
  loadPage({
    required int page,
    required Function(List<SCGoldRecordRes>) onSuccess,
    Function? onErr,
  }) async {
    if (page == 1) {
      lastId = null;
    }
    try {
      var goldList = await SCAccountRepository().goldRecord(lastId: lastId);
      if (goldList.isNotEmpty) {
        lastId = goldList.last.id;
      }
      onSuccess(goldList);
    } catch (e) {
      if (onErr != null) {
        onErr();
      }
    }
  }
}
