import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_map/flutter_map.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';
import 'package:location/base/base_page.dart';
import 'package:location/data/bean/user_info.dart';
import 'package:location/module/main/main_controller.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 'main_friend_item.dart';

class MainPage extends BasePage<MainController> {
  const MainPage({super.key});

  static start({bool? isNotClear, Map<String, dynamic>? arguments}) {
    if (isNotClear == null || !isNotClear) {
      Get.offAllNamed(RoutePath.mainTab, arguments: arguments);
    } else {
      Get.toNamed(RoutePath.mainTab, arguments: arguments);
    }
  }

  @override
  bool immersive() {
    return true;
  }

  @override
  Widget buildBody(BuildContext context) {
    return PopScope(
      canPop: false,
      onPopInvokedWithResult: (bool didPop, dynamic result) {
        controller.onAppBack();
      },
      child: Stack(
        children: [
          Padding(
            padding: EdgeInsets.only(bottom: 50.h),
            child: SizedBox(
                width: double.infinity,
                child: MapWidget(
                  controller: controller.mapController,
                  onMarkerTap: controller.onMarkerTap,
                )),
          ),
          Align(
            alignment: Alignment.bottomCenter,
            child: IntrinsicHeight(
              child: Column(
                children: [
                  Row(
                    crossAxisAlignment: CrossAxisAlignment.end,
                    mainAxisAlignment: MainAxisAlignment.spaceBetween,
                    children: [
                      Visibility(
                        visible: false,
                        child: Container(
                            margin: EdgeInsets.only(bottom: 24.w, left: 12.w),
                            child: Assets.images.iconMainMapClock
                                .image(width: 50.w)),
                      ),
                      Align(
                        alignment: Alignment.bottomRight,
                        child: Column(
                          children: [
                            GestureDetector(
                              onTap: controller.onRefreshFriendLocationClick,
                              child: Container(
                                  margin: EdgeInsets.only(right: 12.w),
                                  child: Assets
                                      .images.iconMainRefreshFriendLocation
                                      .image(width: 42.w, height: 42.w)),
                            ),
                            SizedBox(height: 14.w),
                            GestureDetector(
                              onTap: controller.onCurrentLocationClick,
                              child: Container(
                                  margin: EdgeInsets.only(right: 12.w),
                                  child: Assets
                                      .images.iconMainRefreshMineLocation
                                      .image(width: 42.w, height: 42.w)),
                            ),
                            SizedBox(height: 20.w)
                          ],
                        ),
                      )
                    ],
                  ),
                  Container(
                    decoration: BoxDecoration(
                      color: '#F9F9F9'.color,
                      borderRadius: BorderRadius.only(
                        topLeft: Radius.circular(20.w),
                        topRight: Radius.circular(20.w),
                      ),
                    ),
                    child: Column(
                      children: [
                        SizedBox(height: 5.w),
                        Container(
                          width: 32.w,
                          height: 3.w,
                          decoration: BoxDecoration(
                              color: '#D9D9D9'.color,
                              borderRadius:
                                  BorderRadius.all(Radius.circular(49.w))),
                        ),
                        SizedBox(height: 12.w),
                        buildSelectFriendInfoView(),
                        Obx(() {
                          return Visibility(
                              visible: controller.selectedFriend != null,
                              child: SizedBox(height: 13.w));
                        }),
                        buildTabContainer()
                      ],
                    ),
                  )
                ],
              ),
            ),
          ),
          SafeArea(
            child: Container(
              margin: EdgeInsets.only(top: 26.w),
              child: Row(
                children: [
                  Expanded(child: buildMainFriendList()),
                  GestureDetector(
                    onTap: () {
                      controller.onAddFriendClick();
                    },
                    child: Container(
                        margin: EdgeInsets.only(right: 16.w, left: 8.w),
                        child: Assets.images.iconMainAddFriend
                            .image(width: 60.w, height: 60.w)),
                  )
                ],
              ),
            ),
          )
        ],
      ),
    );
  }

  Container buildTabContainer() {
    return Container(
        decoration: BoxDecoration(
          color: ColorName.white,
          borderRadius: BorderRadius.only(
            topLeft: Radius.circular(20.w),
            topRight: Radius.circular(20.w),
          ),
        ),
        padding: EdgeInsets.only(top: 13.w, bottom: 23.w),
        child: buildMainFunList());
  }

  Widget buildMainFunList() {
    return Row(
      children: [
        Expanded(
            child: buildFunItem(
                Assets.images.iconMainFriendGuard.provider(),
                StringName.mainFriendListTab,
                () => controller.onFriendClick())),
        Expanded(child: Obx(() {
          return buildFunItem(Assets.images.iconMainNews.provider(),
              StringName.mainNewsTab, () => controller.onNewsClick(),
              isShowDot: controller.hasUnreadMessage);
        })),
        Expanded(
            child: buildFunItem(
                Assets.images.iconMainHelp.provider(),
                StringName.mainHelpTab,
                () => controller.onUrgentContactClick())),
        Expanded(
            child: buildFunItem(Assets.images.iconMainMine.provider(),
                StringName.mainMineTab, () => controller.onMineClick()))
      ],
    );
  }

  Widget buildFunItem(ImageProvider imgProvider, String title, Function() onTap,
      {bool? isShowDot}) {
    return GestureDetector(
      onTap: onTap,
      child: Column(
        children: [
          Stack(children: [
            SizedBox(
                width: 44.w, height: 44.w, child: Image(image: imgProvider)),
            Visibility(
              visible: isShowDot ?? false,
              child: Positioned(
                top: 6.w,
                right: 6.w,
                child: Container(
                  width: 10.w,
                  height: 10.w,
                  decoration: BoxDecoration(
                    shape: BoxShape.circle,
                    color: '#FF333D'.color, // 背景颜色
                  ),
                ),
              ),
            )
          ]),
          Text(title,
              style: TextStyle(
                  fontSize: 12.sp,
                  color: ColorName.black70,
                  fontWeight: FontWeight.bold))
        ],
      ),
    );
  }

  Widget buildSelectFriendInfoView() {
    return Obx(() {
      UserInfo? userInfo = controller.selectedFriend;
      if (userInfo == null) {
        return SizedBox.shrink();
      }
      return mainSelectedFriendItem(
          userInfo,
          (controller.memberStatusInfo.value == null ||
              controller.memberStatusInfo.value?.expired == true),
          viewTraceClick: () => controller.onViewTraceClick(userInfo));
    });
  }

  Widget buildMainFriendList() {
    return SizedBox(
      height: 58.w,
      child: Obx(() {
        return ListView(
          physics: const BouncingScrollPhysics(
              parent: AlwaysScrollableScrollPhysics()),
          padding: EdgeInsets.only(left: 12.w),
          scrollDirection: Axis.horizontal,
          children: [
            for (UserInfo userInfo in controller.integrateList)
              Obx(() {
                return mainFriendItem(
                    userInfo, controller.selectedFriend?.id == userInfo.id,
                    onTap: () {
                  controller.onSelectUserClick(userInfo);
                });
              })
          ],
        );
      }),
    );
  }
}
