import 'dart:io';

import 'package:flutter_tool_android/flutter_tool_android.dart';
import 'package:get/get.dart';
import 'package:injectable/injectable.dart';
import 'package:location/base/base_controller.dart';
import 'package:location/data/repositories/phone_event_repository.dart';
import 'package:location/resource/string.gen.dart';
import 'package:location/utils/notification_util.dart';
import 'package:location/utils/toast_util.dart';
import 'package:permission_handler/permission_handler.dart';
import '../../dialog/common_confirm_dialog_impl.dart';
import '../../utils/permission_util.dart';

@injectable
class PermissionSettingController extends BaseController {
  final RxBool _permissionShowBattery = false.obs;
  final RxBool _permissionShowBackgroundRun = false.obs;
  final RxBool _permissionShowTip = false.obs;

  bool get permissionShowBattery => _permissionShowBattery.value;

  bool get permissionShowBackgroundRun => _permissionShowBackgroundRun.value;

  bool get permissionShowTip => _permissionShowTip.value;

  @override
  void onReady() {
    super.onReady();
    if (Platform.isAndroid) {
      _permissionShowBattery.value = true;
      _permissionShowBackgroundRun.value = true;
      _permissionShowTip.value = true;
    }
  }

  void openLocationSetting() async {
    bool isGranted = await PermissionUtil.checkLocationPermission();
    if (!isGranted) {
      isGranted = await PermissionUtil.requestLocationPermission();
    }
    if (isGranted) {
      isGranted = await PermissionUtil.checkShowLocationAlways();
      if (isGranted) {
        ToastUtil.show(StringName.permissionSettingSuccess);
      } else {
        isGranted = await PermissionUtil.requestShowLocationAlways();
        if (isGranted) {
          ToastUtil.show(StringName.permissionSettingSuccess);
        } else {
          ToastUtil.show(StringName.permissionLocationSettingSubtitle);
        }
      }
    } else {
      permissionRefuseDialog(settingClick: () {
        openAppSettings();
      });
      ToastUtil.show(StringName.permissionRequestFail);
    }
  }

  void openBatterySetting() async {
    bool isIgnore = await FlutterToolAndroid.isIgnoringBatteryOptimizations();
    if (isIgnore) {
      ToastUtil.show(StringName.permissionSettingSuccess);
    } else {
      FlutterToolAndroid.openBatteryOptimizationSettings();
    }
  }

  void openBackgroundRunSetting() async {
    FlutterToolAndroid.openAppBackgroundSetting();
  }

  void openPhoneScreenSetting() async {
    bool hasPermission =
        await PhoneEventRepository.hasUseStatisticsPermission();
    if (hasPermission) {
      ToastUtil.show(StringName.permissionSettingSuccess);
      return;
    }
    //打开app设置页
    hasPermission = await PhoneEventRepository.requestUseStatisticsPermission();
    if (hasPermission) {
      ToastUtil.show(StringName.permissionSettingSuccess);
    } else {
      ToastUtil.show(StringName.permissionRequestFail);
    }
  }

  void openNotificationSetting() async {
    bool hasPermission = await NotificationUtil.hasNotificationPermission();
    if (hasPermission) {
      ToastUtil.show(StringName.permissionSettingSuccess);
      return;
    }
    hasPermission = await NotificationUtil.requestNotificationPermission();
    if (hasPermission) {
      ToastUtil.show(StringName.permissionSettingSuccess);
    } else {
      ToastUtil.show(StringName.permissionRequestFail);
    }
  }
}
