import 'package:flutter/cupertino.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/request_friend_info.dart';
import 'package:location/resource/assets.gen.dart';
import 'package:location/resource/colors.gen.dart';
import 'package:location/utils/common_expand.dart';
import 'package:location/utils/date_util.dart';
import '../../../resource/string.gen.dart';
import '../../../router/app_pages.dart';
import '../../../widget/common_view.dart';
import '../news_list_item.dart';
import 'news_pending_list_controller.dart';

class NewsPendingListPage extends BasePage<NewsPendingListController> {
  const NewsPendingListPage({super.key});

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

  @override
  Color backgroundColor() {
    return '#F7F7F7'.color;
  }

  @override
  Widget buildBody(BuildContext context) {
    return Column(
      children: [
        buildHeadView(),
        Expanded(child: Obx(() {
          return ListView.builder(
              itemBuilder: buildPendingItem,
              itemCount: controller.pendingList.length);
        }))
      ],
    );
  }

  Widget buildHeadView() {
    return Container(
      margin: EdgeInsets.symmetric(horizontal: 12.w, vertical: 14.w),
      child: Row(
        crossAxisAlignment: CrossAxisAlignment.center,
        mainAxisAlignment: MainAxisAlignment.spaceBetween,
        children: [
          GestureDetector(
              onTap: controller.back, child: CommonView.getBackBtnView()),
          Text(StringName.messageNewFriendTitle,
              style: TextStyle(
                  fontSize: 18.sp,
                  color: '#202020'.color,
                  fontWeight: FontWeight.bold)),
          SizedBox(width: 24.w, height: 24.w)
        ],
      ),
    );
  }

  ///1:待处理
  // 2:接受
  // 3:拒绝
  Widget buildPendingItem(BuildContext context, int index) {
    final item = controller.pendingList[index];
    if (item.status == 1) {
      return buildTodoItem(item);
    } else {
      return buildMessageItem(Assets.images.iconDefaultFriendAvatar.provider(),
          title: item.userPhone,
          content: getMessageContentTxt(item.status),
          contentTextStyle: getMessageContentTextStyle(item.status),
          createTime: item.createTime,
          statusWidget: getMessageStatusWidget(item.status));
    }
  }

  Widget buildTodoItem(RequestFriendInfo item) {
    return Container(
      decoration: BoxDecoration(
          color: Colors.white, borderRadius: BorderRadius.circular(14.w)),
      padding:
          EdgeInsets.only(top: 16.w, left: 10.w, right: 17.w, bottom: 20.w),
      margin: EdgeInsets.only(left: 12.w, right: 12.w, bottom: 10.w),
      child: Column(
        children: [
          Row(
            children: [
              Assets.images.iconDefaultFriendAvatar
                  .image(width: 48.w, height: 48.w),
              SizedBox(width: 9.w),
              Expanded(
                child: Column(
                  crossAxisAlignment: CrossAxisAlignment.start,
                  children: [
                    RichText(
                        text: TextSpan(
                            style: TextStyle(
                                fontSize: 14.sp,
                                color: '#404040'.color,
                                fontWeight: FontWeight.bold),
                            children: [
                          TextSpan(text: '用户 '),
                          TextSpan(
                              text: item.userPhone,
                              style: TextStyle(color: ColorName.colorPrimary)),
                          TextSpan(text: ' 向您发出了好友申请')
                        ])),
                    SizedBox(height: 5.w),
                    Text(
                      DateUtil.fromMillisecondsSinceEpoch(
                          'yyyy-MM-dd HH:mm', item.createTime),
                      style: TextStyle(fontSize: 13.sp, color: '#A7A7A7'.color),
                    ),
                  ],
                ),
              )
            ],
          ),
          SizedBox(height: 20.w),
          Row(
            children: [
              SizedBox(width: 57.w),
              Expanded(
                  child: GestureDetector(
                onTap: () => controller.onRefuseFriend(item),
                child: Container(
                  padding: EdgeInsets.symmetric(vertical: 8.w),
                  decoration: BoxDecoration(
                    border: Border.all(color: '#E2E2E2'.color, width: 1.w),
                    borderRadius: BorderRadius.circular(100.w),
                  ),
                  child: Center(
                    child: Text(StringName.messageFriendRefuse,
                        style: TextStyle(
                            fontSize: 15.sp,
                            color: '#A7A7A7'.color,
                            fontWeight: FontWeight.bold)),
                  ),
                ),
              )),
              SizedBox(width: 12.w),
              Expanded(
                  child: GestureDetector(
                onTap: () => controller.onAgreeFriend(item),
                child: Container(
                  padding: EdgeInsets.symmetric(vertical: 8.w),
                  decoration: BoxDecoration(
                    color: ColorName.colorPrimary,
                    borderRadius: BorderRadius.circular(100.w),
                  ),
                  child: Center(
                    child: Text(StringName.messageFriendAgree,
                        style: TextStyle(
                            fontSize: 15.sp,
                            color: ColorName.white,
                            fontWeight: FontWeight.bold)),
                  ),
                ),
              ))
            ],
          )
        ],
      ),
    );
  }
}
