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/user_info.dart';
import 'package:location/resource/colors.gen.dart';
import 'package:location/utils/common_expand.dart';
import '../../../resource/assets.gen.dart';
import '../../../resource/string.gen.dart';
import '../../../router/app_pages.dart';
import '../../../utils/common_style.dart';
import '../../../widget/common_view.dart';
import '../../../widget/gradient_switch.dart';
import 'common_point_setting_controller.dart';

class CommonPointSettingPage extends BasePage<CommonPointSettingController> {
  const CommonPointSettingPage({super.key});

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

  @override
  bool immersive() {
    return true;
  }

  @override
  Widget buildBody(BuildContext context) {
    return Stack(
      children: [
        Assets.images.bgPageBackground.image(width: double.infinity),
        SafeArea(
          child: Column(
            children: [buildHeaderView(), Expanded(child: buildContentView())],
          ),
        ),
      ],
    );
  }

  Widget buildHeaderView() {
    return CommonView.buildAppBar(StringName.commonPointSettingTitle,
        backOnTap: controller.onBack);
  }

  Widget buildContentView() {
    return CustomScrollView(
      slivers: [
        SliverToBoxAdapter(child: Obx(() {
          return Column(
            children: [
              buildZoneSettingView(),
              Container(width: 328.w, height: 1.w, color: ColorName.black10),
              SizedBox(height: 10.w),
              if (controller.isZoneAlert)
                buildOpenSettingItem(StringName.commonPointSettingAllFriends,
                    controller.isNotificationAllFriends,
                    onTap: controller.onSettingAllFriendsClick),
              if (controller.isZoneAlert)
                buildOpenSettingItem(
                    StringName.commonPointSettingSomeGoodFriends,
                    !controller.isNotificationAllFriends,
                    desc: controller.friendsList.isEmpty
                        ? StringName.commonPointSettingSomeGoodFriendsDesc
                        : null,
                    onTap: controller.onSettingSomeGoodFriendsClick),
              SizedBox(height: 20.w),
            ],
          );
        })),
        Obx(() {
          if (!controller.isZoneAlert || controller.isNotificationAllFriends) {
            return SliverToBoxAdapter();
          }
          return SliverList.builder(
              itemBuilder: (ctx, index) =>
                  buildSomeGoodFriendsItem(controller.friendsList[index]),
              itemCount: controller.friendsList.length);
        }),
      ],
    );
  }

  Widget buildSomeGoodFriendsItem(UserInfo info) {
    return Container(
        height: 52.w,
        decoration: BoxDecoration(
            color: '#F6F6F8'.color, borderRadius: BorderRadius.circular(10.w)),
        padding: EdgeInsets.symmetric(horizontal: 10.w),
        margin: EdgeInsets.only(left: 16.w, right: 16.w, bottom: 12.w),
        child: Row(
          children: [
            buildCustomAvatarOrDefaultAvatarView(
                size: 36.w, avatar: info.avatar, isMine: false),
            SizedBox(width: 8.w),
            Text((info.getUserNickName()),
                style: TextStyle(fontSize: 13.sp, color: ColorName.black90)),
            Spacer(),
            Assets.images.iconAvatarSelected.image(width: 18.w, height: 18.w)
          ],
        ));
  }

  Widget buildOpenSettingItem(String title, bool isChecked,
      {String? desc, VoidCallback? onTap}) {
    return GestureDetector(
      behavior: HitTestBehavior.translucent,
      onTap: onTap,
      child: Container(
        padding: EdgeInsets.symmetric(horizontal: 16.w, vertical: 12.w),
        child: Column(
          crossAxisAlignment: CrossAxisAlignment.start,
          children: [
            Row(
              children: [
                Text(title,
                    style:
                        TextStyle(fontSize: 12.sp, color: ColorName.black60)),
                Spacer(),
                isChecked
                    ? Assets.images.iconAvatarSelected
                        .image(width: 18.w, height: 18.w)
                    : Assets.images.iconCbUnCheck
                        .image(width: 18.w, height: 18.w)
              ],
            ),
            if (desc != null) SizedBox(height: 6.h),
            if (desc != null)
              Text(
                desc,
                style: TextStyle(fontSize: 10.sp, color: '#FF7272'.color),
              ),
          ],
        ),
      ),
    );
  }

  Widget buildZoneSettingView() {
    return Container(
      padding: EdgeInsets.symmetric(horizontal: 16.w, vertical: 20.w),
      child: Row(
        children: [
          Column(
            crossAxisAlignment: CrossAxisAlignment.start,
            children: [
              Text(StringName.commonPointSettingNotificationTitle,
                  style: TextStyle(
                      fontSize: 15.sp,
                      color: Colors.black,
                      fontWeight: FontWeight.bold)),
              SizedBox(height: 6.h),
              Text(StringName.commonPointSettingNotificationDesc,
                  style: TextStyle(fontSize: 11.sp, color: ColorName.black30)),
            ],
          ),
          Spacer(),
          Obx(() {
            return GradientSwitch(
                unselectedGradient:
                    LinearGradient(colors: ['#D4D8EB'.color, '#D4D8EB'.color]),
                selectedGradient: LinearGradient(
                    colors: [ColorName.colorPrimary, ColorName.colorPrimary]),
                indicatorSize: 20.w,
                width: 46.w,
                height: 25.w,
                value: controller.isZoneAlert,
                onChanged: controller.zoneSettingFuture);
          })
        ],
      ),
    );
  }
}
