import 'package:custom_notification/custom_notification.dart';
import 'package:electronic_assistant/module/talk/view.dart';
import 'package:flutter_local_notifications/flutter_local_notifications.dart';
import 'package:get/get_utils/src/platform/platform.dart';

class NotificationUtil {
  NotificationUtil._();

  static String formatDuration(double seconds) {
    final duration = Duration(seconds: seconds.toInt());
    final hours = duration.inHours.toString().padLeft(2, '0');
    final minutes = (duration.inMinutes % 60).toString().padLeft(2, '0');
    final secs = (duration.inSeconds % 60).toString().padLeft(2, '0');
    return '$hours:$minutes:$secs';
  }

  static void showRecordNotification(
      int notificationId, bool isRecording, double recordDuration,
      {required String channelId, required String channelName}) {
    if (GetPlatform.isAndroid) {
      CustomNotification.showRecordNotification(
          notificationId, isRecording, formatDuration(recordDuration),
          channelId: channelId, channelName: channelName);
    }
  }

  static int _analyseNotificationId = 2000;

  static int getAnalyseNotificationId() {
    return _analyseNotificationId++;
  }

  static void showAnalyseSuccessNotification(String talkId) async {
    final FlutterLocalNotificationsPlugin flutterLocalNotificationsPlugin =
        FlutterLocalNotificationsPlugin();

    const AndroidInitializationSettings initializationSettingsAndroid =
        AndroidInitializationSettings('@mipmap/ic_launcher');

    DarwinInitializationSettings initializationSettingsIOS =
        DarwinInitializationSettings(
            requestSoundPermission: true,
            requestBadgePermission: true,
            requestAlertPermission: true,
            onDidReceiveLocalNotification:
                (int id, String? title, String? body, String? payload) async {
              if (payload != null) {
                TalkPage.startById(payload);
              }
            });

    InitializationSettings initializationSettings = InitializationSettings(
      android: initializationSettingsAndroid,
      iOS: initializationSettingsIOS,
    );

    await flutterLocalNotificationsPlugin.initialize(
      initializationSettings,
      onDidReceiveNotificationResponse:
          (NotificationResponse notificationResponse) async {
        if (notificationResponse.payload != null) {
          TalkPage.startById(notificationResponse.payload!);
        }
      },
    );
    const AndroidNotificationDetails androidPlatformChannelSpecifics =
        AndroidNotificationDetails(
      'talk_analyse_success',
      '谈话分析通知',
      channelDescription: '谈话分析成功',
      importance: Importance.max,
      priority: Priority.high,
    );

    // ios的通知
    const String darwinNotificationCategoryPlain = 'plainCategory';
    const DarwinNotificationDetails iOSPlatformChannelSpecifics =
        DarwinNotificationDetails(
      categoryIdentifier: darwinNotificationCategoryPlain,
    );
    const NotificationDetails platformChannelSpecifics = NotificationDetails(
        android: androidPlatformChannelSpecifics,
        iOS: iOSPlatformChannelSpecifics);

    await flutterLocalNotificationsPlugin.show(
      getAnalyseNotificationId(),
      '小听谈话分析完成',
      '老板，谈话分析已完成，快点击查看吧。',
      platformChannelSpecifics,
      payload: talkId,
    );
  }

// static void showAnalyseResultNotification(
//     int notificationId, bool isSuccess, String talkId,
//     {required String channelId, required String channelName}) {
//   CustomNotification.showAnalyseResultNotification(
//       notificationId, isSuccess, talkId,
//       channelId: channelId, channelName: channelName);
// }
}
