import 'package:flutter/cupertino.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';
import 'package:location/base/base_page.dart';
import 'package:location/module/permission/permission_setting_controller.dart';
import 'package:location/resource/colors.gen.dart';
import 'package:location/resource/string.gen.dart';
import 'package:location/utils/common_expand.dart';
import 'package:location/widget/common_view.dart';

import '../../router/app_pages.dart';

class PermissionSettingPage extends BasePage<PermissionSettingController> {
  const PermissionSettingPage({super.key});

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

  @override
  Widget buildBody(BuildContext context) {
    return Column(
      children: [
        CommonView.buildAppBar(StringName.mineFunPermissionSetting,
            backOnTap: () => Get.back(), titleCenter: false),
        SizedBox(height: 12.w),
        Expanded(
            child: ListView(
          children: [
            buildPermissionItem(StringName.permissionLocationSetting,
                StringName.permissionLocationSettingSubtitle, () {
              controller.openLocationSetting();
            }),
            buildPermissionItem(StringName.permissionPhoneScreenSetting,
                StringName.permissionPhoneScreenSettingSubtitle, () {
              controller.openPhoneScreenSetting();
            }),
            buildPermissionItem(StringName.permissionNotificationSetting,
                StringName.permissionNotificationSettingSubtitle, () {
              controller.openNotificationSetting();
            }),
            Obx(() {
              return Visibility(
                visible: controller.permissionShowBattery,
                child: buildPermissionItem(
                    StringName.permissionBatteryOptimization,
                    StringName.permissionBatteryOptimizationSubtitle, () {
                  controller.openBatterySetting();
                }),
              );
            }),
            Obx(() {
              return Visibility(
                visible: controller.permissionShowBackgroundRun,
                child: buildPermissionItem(
                    StringName.permissionBackgroundOperation,
                    StringName.permissionBackgroundOperationSubtitle, () {
                  controller.openBackgroundRunSetting();
                }),
              );
            }),
            Obx(() {
              return Visibility(
                  visible: controller.permissionShowTip,
                  child: buildPermissionTips());
            })
          ],
        ))
      ],
    );
  }

  Widget buildPermissionItem(
      String title, String subTitle, VoidCallback onTap) {
    return Container(
      margin: EdgeInsets.only(bottom: 12.w, left: 12.w, right: 12.w),
      decoration: BoxDecoration(
        color: '#FAFAFA'.color,
        borderRadius: BorderRadius.circular(6.w),
      ),
      padding: EdgeInsets.symmetric(horizontal: 12.w, vertical: 22.w),
      child: Column(
        crossAxisAlignment: CrossAxisAlignment.start,
        children: [
          Row(
            children: [
              Text(
                title,
                style: TextStyle(
                    fontSize: 16.sp,
                    color: '#202020'.color,
                    fontWeight: FontWeight.bold),
              ),
              Spacer(),
              GestureDetector(
                onTap: onTap,
                child: Container(
                  decoration: BoxDecoration(
                      color: ColorName.colorPrimary,
                      borderRadius: BorderRadius.circular(100.w)),
                  padding:
                      EdgeInsets.symmetric(horizontal: 15.w, vertical: 5.w),
                  child: Text(StringName.permissionSetting,
                      style:
                          TextStyle(fontSize: 14.sp, color: ColorName.white)),
                ),
              )
            ],
          ),
          SizedBox(height: 8.w),
          Text(subTitle,
              style: TextStyle(fontSize: 14.sp, color: '#404040'.color))
        ],
      ),
    );
  }

  Widget buildPermissionTips() {
    return Container(
      decoration: BoxDecoration(
        color: '#FAFAFA'.color,
        borderRadius: BorderRadius.circular(6.w),
      ),
      padding: EdgeInsets.symmetric(vertical: 24.w, horizontal: 12.w),
      margin: EdgeInsets.only(left: 12.w, right: 12.w),
      child: Column(
        crossAxisAlignment: CrossAxisAlignment.start,
        children: [
          RichText(
              text: TextSpan(
                  style: TextStyle(fontSize: 14.sp, color: '#404040'.color),
                  children: [
                TextSpan(text: '温馨提示：'),
                TextSpan(
                    text: '请勿开启【省电模式】\n\n',
                    style: TextStyle(fontSize: 14.sp, color: '#EC5050'.color)),
                TextSpan(text: '在省电模式下，可能会导致轨迹异常等问题，建议您关闭省电模式')
              ])),
        ],
      ),
    );
  }
}
