//
//  XFTool.m
//  Pods-HBuilder
//
//  Created by 张丰 on 2023/6/21.
//

#import "XFTool.h"
#import <UIKit/UIKit.h>
#import <UserNotifications/UserNotifications.h>
#import <AVFoundation/AVFoundation.h>

@interface XFTool()
@property(nonatomic,assign)NSInteger count;
@end

@implementation XFTool
static XFTool *_instance = nil;

+ (XFTool *)shareInstance {
    // static修饰标记变量
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        _instance = [[self alloc] init];
    });
    return _instance;
}
+ (void)callAction:(NSDictionary *)options {
//    if([self isBackground] == false) { return; }
    dispatch_async(dispatch_get_main_queue(), ^{
        [XFTool.shareInstance callActionAfter:options];
    });
}

+ (BOOL)isBackground {
    NSLog(@"co%@",NSThread.currentThread);
    UIApplicationState state = [UIApplication sharedApplication].applicationState;
    BOOL result = (state == UIApplicationStateBackground);
    return result;
}
- (void)callActionAfter:(NSDictionary *)options {
//    if(XFTool.isBackground == false) { return; }
    UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter];
    UNMutableNotificationContent *content = [[UNMutableNotificationContent alloc] init];
    // 标题
    content.title = options[@"title"];
    content.subtitle = options[@"subtitle"];
    // 内容
    content.body = options[@"body"];
    // 添加自定义声音/Users/zhangfeng/Library/Developer/Xcode/DerivedData/uniPlugins-fabjwlfyxknxmzacjqfdxzfqfkgd/Build/Products/Release-iphoneos/UniPluginDemo.app/Pandora/apps/__UNI__C5C1DF0/www/static/xiaomi.mp3
    
    content.sound = [UNNotificationSound soundNamed:@"Pandora/apps/__UNI__7B16FF4/www/static/xiaomi.mp3"];
//    content.sound = [UNNotificationSound soundNamed:@"Pandora/apps/__UNI__7B16FF4/www/static/xiaomi.mp3"];
    // 角标 （我这里测试的角标无效，暂时没找到原因）
    // 多少秒后发送,可以将固定的日期转化为时间
    NSTimeInterval time = [[NSDate dateWithTimeIntervalSinceNow:1] timeIntervalSinceNow];

    // repeats，是否重复，如果重复的话时间必须大于60s，要不会报错
    UNTimeIntervalNotificationTrigger *trigger = [UNTimeIntervalNotificationTrigger triggerWithTimeInterval:time repeats:NO];

    /*
     //如果想重复可以使用这个,按日期
     // 周一早上 8：00 上班
     NSDateComponents *components = [[NSDateComponents alloc] init];
     // 注意，weekday默认是从周日开始
     components.weekday = 2;
     components.hour = 8;
     UNCalendarNotificationTrigger *calendarTrigger = [UNCalendarNotificationTrigger triggerWithDateMatchingComponents:components repeats:YES];
     */
    // 添加通知的标识符，可以用于移除，更新等操作
    NSString *identifier = NSUUID.UUID.UUIDString;
    UNNotificationRequest *request = [UNNotificationRequest requestWithIdentifier:identifier content:content trigger:trigger];

    [center addNotificationRequest:request withCompletionHandler:^(NSError *_Nullable error) {
        NSLog(@"成功添加推送");
    }];
    [self vibrationTimerActon];
}

//收到推送消息后，会先执行此方法
- (void)vibrationTimerActon {
    self.count ++;
    if(self.count > 8) {
        self.count = 0;
        return;
    }
    dispatch_time_t time = dispatch_time(DISPATCH_TIME_NOW, (ino64_t)(1 * NSEC_PER_SEC));
    dispatch_after(time, dispatch_get_main_queue(), ^{
        AudioServicesPlaySystemSound(kSystemSoundID_Vibrate);
        if(XFTool.isBackground) {
            [self vibrationTimerActon];
        }
    });
}
@end
