import 'package:flutter/foundation.dart';
import 'package:flutter/services.dart';
import 'package:mobile_use_statistics/src/event/event.dart';

import 'mobile_use_statistics_platform_interface.dart';

/// An implementation of [MobileUseStatisticsPlatform] that uses method channels.
class MethodChannelMobileUseStatistics extends MobileUseStatisticsPlatform {
  /// The method channel used to interact with the native platform.
  @visibleForTesting
  final methodChannel = const MethodChannel('mobile_use_statistics');

  @override
  Future<List<Event>?> getLockScreenStatistics({
    required int startTime,
    required int endTime,
  }) async {
    final result = await methodChannel.invokeListMethod(
      'getLockScreenStatistics',
      <String, dynamic>{'startTime': startTime, 'endTime': endTime},
    );
    return result
        ?.map((e) => Event.fromJson(Map<String, dynamic>.from(e as Map)))
        .toList();
  }

  @override
  Future<bool> hasUseStatisticsPermission() async {
    final hasPermission = await methodChannel.invokeMethod<bool>(
      'hasUseStatisticsPermission',
    );
    return hasPermission ?? false;
  }

  @override
  Future<bool> requestUseStatisticsPermission() async {
    final hasPermission = await methodChannel.invokeMethod<bool>(
      'requestUseStatisticsPermission',
    );
    return hasPermission ?? false;
  }

  ///app启动开启
  @override
  Future<void>appLaunchAnActivationPermission() async {
    await methodChannel.invokeMethod<bool>(
      'appLaunchAnActivationPermission',
    );
  }

  ///app进入后台
  Future<void>appEntersTheBacken() async {
    await methodChannel.invokeMethod<bool>(
      'appEntersTheBackenPermission',
    );
  }
}
