import 'package:flutter_local_notifications/flutter_local_notifications.dart';
import 'package:intl/intl.dart';

import 'Data.dart';
import 'User.dart' as User;

class NotificationManager{
  static void RescheduleNotifications() async{

    print("Rescheduling notifications");
    var androidInitilize = new AndroidInitializationSettings('@mipmap/ic_launcher');
    var iOSinitilize = new IOSInitializationSettings();

    var initilizationsSettings =
    new InitializationSettings(android: androidInitilize, iOS: iOSinitilize);
    var fltrNotification = new FlutterLocalNotificationsPlugin();
    fltrNotification.initialize(initilizationsSettings, onSelectNotification: notificationSelected);

    await fltrNotification.cancelAll();

    int notification_interval = await Settings.getNotificationInterval();
    bool adaptive_notification = await Settings.getAdaptiveNotification();
    bool adaptive_notification_availabel = await Settings.adaptiveNotificationAvailable();

    var androidDetails = const AndroidNotificationDetails("Xperience", "TaskTracker", importance: Importance.max);
    var iSODetails = new IOSNotificationDetails();
    var generalNotificationDetails =
    new NotificationDetails(android: androidDetails, iOS: iSODetails);

    if(adaptive_notification && adaptive_notification_availabel){
      print("Smart!, Using adaptive huh?");
      //Calculate you little thing! CALCULATE!'
      DateFormat timeFormat = DateFormat("HH:mm");
      DateTime baseline = DateTime(2000,2,2,0,0,0);
      Map<int,List<int>> timeMap = <int,List<int>>{};

      int totalDays = User.activities[0].startTime.difference(User.activities[User.activities.length-1].startTime).inDays;
      int groupingThreshold = 60;
      print("Iterating thru ${User.activities.length} activities of $totalDays");
      for (var element in User.activities) {
        if(element.startTime.difference(DateTime.now()).inDays <30){
          //Eligible for adaptation
          DateTime thisTime =baseline.add(Duration(hours: element.startTime.hour, minutes: element.startTime.minute));
          int thisMin = thisTime.difference(baseline).inMinutes;
          int? chosenOne = null;
          int bestChoiceDiffer = groupingThreshold;

          timeMap.forEach((key, value) {
            if(key > thisMin-bestChoiceDiffer && key < thisMin+bestChoiceDiffer){
              //Eligible to apply
              chosenOne=key;
              bestChoiceDiffer = (key - thisMin).abs();
            }
          });

          if(chosenOne==null){
            //There were no candidates
            timeMap.putIfAbsent(thisMin, () => [thisMin]);
          }else{
            List<int> tempList = timeMap[chosenOne]!;
            tempList.add(thisMin);
            int newKey = 0;
            for (var element in tempList) {
              newKey+=element;
            }
            newKey = (newKey / tempList.length).toInt();
            timeMap.remove(chosenOne);
            timeMap.putIfAbsent(newKey, () => tempList);
          }
        }
      }
      print("Time Map : ${timeMap.length}");
      print(timeMap);
      timeMap.forEach((key, value) {
        DateTime now = DateTime.now();
        DateTime todayStart = DateTime(now.year,now.month,now.day,0,0,0);
        DateTime thisTime = todayStart.add(Duration(minutes: key));
        if(value.length < (totalDays.clamp(0, 30) / 3).ceil()){
          print("${timeFormat.format(thisTime)} this happened only ${value.length} times, Ignoring...");
        }else {
          print(timeFormat.format(thisTime));
          if(thisTime.isBefore(DateTime.now())) {
            thisTime = thisTime.add(Duration(days: 1));
          }
          print("Scheduling at $thisTime");
          fltrNotification.schedule(key, "Don't forget to add new activity", "Adaptive notifications suggests that you may have done by now to track.", thisTime, generalNotificationDetails);

        }
      });
    }else if(notification_interval>0) {

      var scheduledTime = DateTime.now().add(Duration(hours: notification_interval));
      fltrNotification.schedule(1, "What did you do in last $notification_interval hours?", "Click here to track your last activities...",
        scheduledTime, generalNotificationDetails,);
      print("Sent notification schedule");
    }

    await Future.delayed(Duration(seconds: 2));
   try{ final List<PendingNotificationRequest> pendingNotificationRequests =await fltrNotification.pendingNotificationRequests();
    print("Notifications");
    pendingNotificationRequests.forEach((element) {
      print("${element.id} : ${element.title} -> ${element.body} \n payload:${element.payload}");
    });}catch(e){
     print("Error printing notifications \n $e");
   }
  }

  static void notificationSelected(val){
    print("Selected notifications : $val");
  }
}