import 'dart:io';

import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';
import 'package:location/base/base_page.dart';
import 'package:location/resource/assets.gen.dart';
import 'package:location/resource/colors.gen.dart';
import 'package:location/resource/string.gen.dart';
import 'package:location/utils/common_expand.dart';

import '../../router/app_pages.dart';
import '../../widget/common_view.dart';
import '../add_friend/add_friend_page.dart';
import 'friend_controller.dart';
import 'friend_list_item.dart';

class FriendPage extends BasePage<FriendController> {
  const FriendPage({super.key});

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

  @override
  bool immersive() {
    return true;
  }

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

  @override
  Widget buildBody(BuildContext context) {
    return Stack(
      children: [
        Assets.images.bgPageBackground.image(width: double.infinity),
        Positioned(
            top: 24.w,
            right: 19.w,
            child: SafeArea(child: Obx(() {
              return Opacity(
                  opacity: 1 - controller.opacity,
                  child: Assets.images.iconGuard.image(width: 118.w));
            }))),
        SafeArea(
          child: Column(
            children: [
              buildHeadView(),
              Expanded(
                child: Obx(() {
                  return ListView(
                    controller: controller.scrollController,
                    children: [
                      SizedBox(height: 18.w),
                      Padding(
                        padding: EdgeInsets.only(left: 12.w),
                        child: Text(StringName.friendTitle,
                            style: TextStyle(
                                fontSize: 16.sp,
                                color: ColorName.black90,
                                fontWeight: FontWeight.bold)),
                      ),
                      SizedBox(height: 10.w),
                      Obx(() {
                        return buildFriendItem(controller.mineUserInfo, false,
                            onEditClick: () =>
                                controller.onEditClick(controller.mineUserInfo),
                            viewTraceClick: () => controller
                                .onViewTraceClick(controller.mineUserInfo));
                      }),
                      Obx(() {
                        return controller.virtualFriendInfo == null
                            ? SizedBox.shrink()
                            : buildFriendItem(
                                controller.virtualFriendInfo!, false,
                                viewTraceClick: () =>
                                    controller.onViewTraceClick(
                                        controller.virtualFriendInfo!));
                      }),
                      ...controller.friendsList.map((e) => Obx(() {
                            return buildFriendItem(
                                e,
                                (controller.memberStatusInfo.value == null ||
                                    controller
                                            .memberStatusInfo.value?.expired ==
                                        true),
                                onEditClick: () => controller.onEditClick(e),
                                viewTraceClick: () =>
                                    controller.onViewTraceClick(e));
                          }))
                    ],
                  );
                }),
              ),
              Container(
                width: double.infinity,
                color: "#FFFFFF".color,
                padding: EdgeInsets.symmetric(vertical: 12.w, horizontal: 15.w),
                child: TextButton(
                    style: ButtonStyle(
                        backgroundColor:
                            WidgetStatePropertyAll("#7B7DFF".color),
                        shape: WidgetStatePropertyAll(RoundedRectangleBorder(
                          borderRadius: BorderRadius.all(Radius.circular(10.w)),
                        ))),
                    onPressed: () => AddFriendPage.show(),
                    child: Text(
                      "添加好友",
                      style: TextStyle(fontSize: 14.sp, color: ColorName.white),
                    )),
              )
            ],
          ),
        )
      ],
    );
  }

  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()),
          Obx(() {
            return Opacity(
              opacity: controller.opacity,
              child: Text(StringName.friendTitle,
                  style: TextStyle(
                      fontSize: 16.sp,
                      color: ColorName.black90,
                      fontWeight: FontWeight.bold)),
            );
          }),
          GestureDetector(
              onTap: controller.newsClick,
              child: Platform.isIOS ? Assets.images.iconFriendNews.image(width: 24.w, height: 24.w) : Assets.images.iconFriendNewsAndroid.image(width: 24.w, height: 24.w))
        ],
      ),
    );
  }
}
