import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:yumi/app_localizations.dart';
import 'package:yumi/ui_kit/components/sc_page_list.dart';
import 'package:yumi/ui_kit/components/appbar/socialchat_appbar.dart';
import 'package:yumi/ui_kit/components/sc_compontent.dart';
import 'package:yumi/ui_kit/components/text/sc_text.dart';
import 'package:yumi/ui_kit/components/sc_tts.dart';
import 'package:yumi/app/routes/sc_fluro_navigator.dart';
import 'package:yumi/shared/data_sources/sources/local/user_manager.dart';
import 'package:yumi/shared/data_sources/sources/repositories/sc_user_repository_impl.dart';
import 'package:yumi/shared/business_logic/models/res/follow_user_res.dart';
import 'package:yumi/modules/index/main_route.dart';

class FollowUserListPage extends SCPageList {
  @override
  _FollowUserListPageState createState() => _FollowUserListPageState();
}

class _FollowUserListPageState
    extends SCPageListState<FollowUserRes, FollowUserListPage> {
  String? lastId;

  @override
  void initState() {
    super.initState();
    enablePullUp = true;
    backgroundColor = Colors.transparent;
    loadData(1);
  }

  @override
  Widget build(BuildContext context) {
    return Stack(
      children: [
        Image.asset(
          "sc_images/person/sc_icon_edit_userinfo_bg.png",
          width: ScreenUtil().screenWidth,
          height: ScreenUtil().screenHeight,
          fit: BoxFit.fill,
        ),
        Scaffold(
          backgroundColor: Colors.transparent,
          resizeToAvoidBottomInset: false,
          appBar: SocialChatStandardAppBar(
            title: SCAppLocalizations.of(context)!.followList,
            actions: [],
          ),
          body: buildList(context),
        ),
      ],
    );
  }

  @override
  Widget buildItem(FollowUserRes res) {
    return GestureDetector(
      child: Container(
        decoration: BoxDecoration(),
        child: Row(
          children: [
            SizedBox(width: 10.w),
            head(
              url: res.userProfile?.userAvatar ?? "",
              width: 55.w,
              headdress: res.userProfile?.getHeaddress()?.sourceUrl,
            ),
            SizedBox(width: 5.w),
            Expanded(
              child: Column(
                crossAxisAlignment: CrossAxisAlignment.start,
                children: [
                  Row(
                    children: [
                      socialchatNickNameText(
                        maxWidth: 160.w,
                        res.userProfile?.userNickname ?? "",
                        fontSize: 14.sp,
                        textColor: Colors.white,
                        type: res.userProfile?.getVIP()?.name ?? "",
                        needScroll:
                            (res.userProfile?.userNickname?.characters.length ??
                                0) >
                            14,
                      ),
                      SizedBox(width: 3.w),
                      res.userProfile?.getVIP() != null
                          ? netImage(
                            url: res.userProfile?.getVIP()?.cover ?? "",
                            width: 25.w,
                            height: 25.w,
                          )
                          : Container(),
                      SizedBox(width: 3.w),
                      Container(
                        width: (res.userProfile?.age ?? 0) > 999 ? 58.w : 48.w,
                        height: 24.w,
                        decoration: BoxDecoration(
                          image: DecorationImage(
                            image: AssetImage(
                              res.userProfile?.userSex == 0
                                  ? "sc_images/login/sc_icon_sex_woman_bg.png"
                                  : "sc_images/login/sc_icon_sex_man_bg.png",
                            ),
                          ),
                        ),
                        child: Row(
                          mainAxisSize: MainAxisSize.min,
                          mainAxisAlignment: MainAxisAlignment.center,
                          children: [
                            xb(res.userProfile?.userSex),
                            text(
                              "${res.userProfile?.age}",
                              textColor: Colors.white,
                              fontSize: 15.sp,
                              fontWeight: FontWeight.w600,
                            ),
                            SizedBox(width: 3.w),
                          ],
                        ),
                      ),
                    ],
                  ),
                  Row(
                    children: [
                      GestureDetector(
                        child: SizedBox(
                          child: text(
                            "ID:${res.userProfile?.getID()}",
                            fontSize: 12.sp,
                            textColor: Colors.white,
                            fontWeight: FontWeight.bold,
                          ),
                        ),
                        onTap: () {
                          Clipboard.setData(
                            ClipboardData(text: res.userProfile?.getID() ?? ""),
                          );
                          SCTts.show(
                            SCAppLocalizations.of(context)!.copiedToClipboard,
                          );
                        },
                      ),
                      SizedBox(width: 8.w),
                      getWealthLevel(
                        res.userProfile?.wealthLevel ?? 0,
                        width: 58.w,
                        height: 30.w,
                      ),
                      SizedBox(width: 8.w),
                      getUserLevel(
                        res.userProfile?.charmLevel ?? 0,
                        width: 58.w,
                        height: 30.w,
                      ),
                    ],
                  ),
                ],
              ),
            ),
            SizedBox(width: 15.w),
          ],
        ),
      ),
      onTap: () {
        SCNavigatorUtils.push(
          context,
          "${SCMainRoute.person}?isMe=${AccountStorage().getCurrentUser()?.userProfile?.id == res.userProfile?.id}&tageId=${res.userProfile?.id}",
        );
      },
    );
  }

  @override
  builderDivider() {
    // return Divider(
    //   height: 1.w,
    //   color: Color(0xff3D3277).withOpacity(0.5),
    //   indent: 15.w,
    // );
    return Container(height: 8.w);
  }

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