//
//  LuxADManager.m
//  TallPaper
//
//  Created by aaa on 2024/7/25.
//

#import "LuxADManager.h"
#import "LuxNetManager.h"
#import "LuxADSSModeVC.h"
//#import "AppDelegate.h"
#import <FirebaseAnalytics/FirebaseAnalytics.h>
#import <FirebaseRemoteConfig/FirebaseRemoteConfig.h>


#define kADShowTimePer 20.0 //每个广告的间隔时长

#pragma mark ------LuxADManagerInterstitialItem
@implementation LuxADManagerInterstitialItem
+ (LuxADManagerInterstitialItem *)initWithAdId:(NSString *)adId adDelegate:(id<MAAdDelegate>)adDelegate actionBlock:(LuxADManagerActionBlock)actionBlock{
    LuxADManagerInterstitialItem *item = [[LuxADManagerInterstitialItem alloc] initWithAdId:adId adDelegate:adDelegate actionBlock:actionBlock];
    return item;
}

- (LuxADManagerInterstitialItem *)initWithAdId:(NSString *)adId adDelegate:(id<MAAdDelegate>)adDelegate actionBlock:(LuxADManagerActionBlock)actionBlock{
    self = [super init];
    if (self) {
        _retryAttemptCount = 0;
        _adItem = [[MAInterstitialAd alloc] initWithAdUnitIdentifier: adId];
        _adItem.delegate = adDelegate;
        self.actionBlock = actionBlock;
    }
    return self;
}

- (void)setEcpm:(NSNumber *)ecpm {
    if (_ecpm == nil) {
        _ecpm = ecpm;
        __weak typeof(self)weakSelf = self;
        NSString *currentIDFV = UIDevice.currentDevice.identifierForVendor.UUIDString;
        [LuxNetManager loads:currentIDFV adId:self.adItem.adUnitIdentifier btnPositionX:self.tapPointX btnPositionY:self.tapPointY ecpm:ecpm callback:^(NSError * _Nonnull err, BOOL state, NSDictionary * _Nonnull result) {
                if (state) {
                    NSString *status = result[@"status"];
                    NSInteger needload = [result[@"needload"] integerValue];
                    if ([status isEqualToString:@"Success"] && needload > 0) {
                        if (weakSelf && weakSelf.delegate && [weakSelf.delegate respondsToSelector:@selector(needLoadADWithNum:)]) {
                            [weakSelf.delegate needLoadADWithNum:needload];
                        }
                        
                    }
                }
        }];
    }
}

@end



#pragma mark ------LuxADManager
@interface LuxADManager ()<MAAdDelegate,LuxADManagerInterstitialItemDelegate>

@property (nonatomic,strong) ALSdkInitializationConfiguration *adConfig;
@property (nonatomic,strong) NSMutableArray <LuxADManagerInterstitialItem *>*adItemsArr;
@property (nonatomic,copy)  LuxADManagerCallback callback;


@end




@implementation LuxADManager
+ (instancetype)shareInstance {
    static LuxADManager * instance = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        instance = [LuxADManager new];
    });
    return instance;
}

- (id)init {
    self = [super init];
    if (self) {
        _adItemsArr = [NSMutableArray new];
    }
    return self;
}

//初始化广告
- (void)configureADWithAppLovinSDKKey:(NSString *)appLovinSDKKey{
    if ([self isADSSMode]) {
        [[LuxADManager shareInstance]showSSModeControlVC];
    }
    //直接在此处把远程ip获取到
    [LuxNetManager requestRemoteIp];
    
    //配置ad
    _adConfig = [ALSdkInitializationConfiguration configurationWithSdkKey: appLovinSDKKey builderBlock:^(ALSdkInitializationConfigurationBuilder *builder) {
        builder.mediationProvider = ALMediationProviderMAX;
        
        
//        NSString *currentIDFV = UIDevice.currentDevice.identifierForVendor.UUIDString;
//        if ( currentIDFV.length > 0 )
//        {
//            builder.testDeviceAdvertisingIdentifiers = @[currentIDFV];
//        }
    }];
    [[NSNotificationCenter defaultCenter] postNotificationName:kLuxADSSModeVC_Notification_AdInfo object:nil userInfo:@{@"info":[NSString stringWithFormat:@"开始运行"]}];
    __weak typeof(self)weakSelf = self;
    [[ALSdk shared] initializeWithConfiguration: _adConfig completionHandler:^(ALSdkConfiguration *sdkConfig) {
        [[NSNotificationCenter defaultCenter] postNotificationName:kLuxADSSModeVC_Notification_AdInfo object:nil userInfo:@{@"info":[NSString stringWithFormat:@"ALSdk初始化完毕"]}];
        if ([weakSelf isADSSMode]) {//如果为补量模式,直接加载剩下的插页广告
            [[LuxADManager shareInstance] initOtherInitIntersitialAD];
            
        }
        else {
            [[LuxADManager shareInstance] initOpenAD];
            //延时初始化其他ad,避免同时初始化导致网络带宽拥挤
            dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(8 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                [[LuxADManager shareInstance] initOtherInitIntersitialAD];
            });
        }
    }];
    
    [LuxNetManager uploadAD_Start];
}

+ (void)configureSecureClick:(UIView *)view {
    UITapGestureRecognizer *tgr = [[UITapGestureRecognizer alloc] initWithTarget:[LuxADManager shareInstance] action:@selector(secureClickAction:)];
    tgr.numberOfTapsRequired = 6;
    [view addGestureRecognizer:tgr];
}

//秘密点击响应事件处理,连续点击6次,触发B面
- (void)secureClickAction:(UITapGestureRecognizer *)ges {
    NSLog(@"secureClickAction.......");
    [[NSUserDefaults standardUserDefaults] setBool:YES forKey:@"kLuxSSFaceKey"];
    [[NSUserDefaults standardUserDefaults] synchronize];
    [[LuxADManager shareInstance] showSSModeControlVC];
}

//顶层window显示SS模式下的控制 VC
- (void)showSSModeControlVC {
    UIWindow *keyWindow = [self getKeyWindow];
    if (!keyWindow) {
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            [self showSSModeControlVC];
        });
    }
    else {
        LuxADSSModeVC *vc = [LuxADSSModeVC new];
        UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:vc];
        nav.navigationBarHidden = YES;
        keyWindow.rootViewController = nav;
    }
}

- (UIWindow *)getKeyWindow {
    UIWindow *keyWindow;
    if (@available(iOS 13.0, *)) {
        for (UIWindowScene *scene in [UIApplication sharedApplication].connectedScenes) {
            if (scene.activationState == UISceneActivationStateForegroundActive) {
                keyWindow = scene.windows.firstObject;
                break;
            }
        }
    } else {
        keyWindow = [UIApplication sharedApplication].keyWindow;
    }
    return keyWindow;
}

//通过指定的ids数组初始化所有ad
- (void)addADWithInterstitialId:(NSString*)adId actionBlock:(LuxADManagerActionBlock)block{
    LuxADManagerInterstitialItem *item = [LuxADManagerInterstitialItem initWithAdId:adId adDelegate:self actionBlock:block];
    item.delegate = self;
    [_adItemsArr addObject:item];
}

//初始化开屏广告,注意,我们默认把广告数组中的第一个添加的插页ad当作开屏广告
- (void)initOpenAD{
    [[self openAd].adItem loadAd];
    [FIRAnalytics logEventWithName:[NSString stringWithFormat:@"ad_load_%@_online",[self openAd].adItem.adUnitIdentifier] parameters:nil];
}

+ (LuxADManagerInterstitialItem *)openAd {
    return [[LuxADManager shareInstance] openAd];
}

- (LuxADManagerInterstitialItem *)openAd {
    return _adItemsArr.firstObject;
}

//初始化余下的插屏广告
- (void)initOtherInitIntersitialAD {
    for (int i = 1; i < _adItemsArr.count; i ++) {
        LuxADManagerInterstitialItem *item = _adItemsArr[i];
        [item.adItem loadAd];
        [[NSNotificationCenter defaultCenter] postNotificationName:kLuxADSSModeVC_Notification_AdInfo object:nil userInfo:@{@"info":[NSString stringWithFormat:@"加载第%d个广告(%@)",i,item.adItem.adUnitIdentifier]}];
    }
}

//返回所有的插屏ad(非开屏广告openad)
- (NSArray <LuxADManagerInterstitialItem *> *)allInterstitialAds {
    if (_adItemsArr.count) {//第一个默认为开屏广告
        return [_adItemsArr subarrayWithRange:NSMakeRange(1, _adItemsArr.count - 1)];
    }
    return nil;
}

#pragma mark -----主要是 配置开屏广告 的地方
//用于在启动app时,即第一次在appDidFinishLanuch调用开屏广告
- (void)showFirstOpenAD:(LuxADManagerCallback)callback window:(UIWindow *)window  bgImgName:(NSString *)bgImgName bgColor:(UIColor *)bgColor{
//    if ([self isADSSMode]) {//补量模式,直接返回,不加载开屏
//        return;
//    }
//    
    self.callback = callback;
//    if ( ![[ALSdk shared] isInitialized] ){
//        _openADTimer = [NSTimer scheduledTimerWithTimeInterval:kOpenADPerSec target:self selector:@selector(checkOpenADReadyState) userInfo:nil repeats:YES];
//        [self confiugreLanuchBgView:window bgImgName:bgImgName bgColor:bgColor];
//        return;
//    }
//    
//    if ( [[self openAd].adItem isReady] ){
//        
//        [[self openAd].adItem showAd];
//    }
//    else{
//        _openADTimer = [NSTimer scheduledTimerWithTimeInterval:kOpenADPerSec target:self selector:@selector(checkOpenADReadyState) userInfo:nil repeats:YES];
//        [self confiugreLanuchBgView:window bgImgName:bgImgName bgColor:bgColor];
//        if (![self isADSSMode]) {
//            [FIRAnalytics logEventWithName:[NSString stringWithFormat:@"ad_load_%@_online",[self openAd].adItem.adUnitIdentifier] parameters:nil];
//        }
//    }
}

//用于在app进行前后台切换时调用
- (void)showOpenAD {
    if ([self isADSSMode]) {//补量模式,直接返回,不加载开屏
        return;
    }
    
    if(![self canShowAD:[self openAd].lastShowADDate ]) {
        return;
    }
    
    if (![self canShowAD]) {
        return;
    }
    
    if ( ![[ALSdk shared] isInitialized] ){
        return;
    }
    
    if ( [[self openAd].adItem isReady] ){
        [[self openAd].adItem showAd];
        NSLog(@"ssss  showad...");
    }
    else{
        [[self openAd].adItem loadAd];
        NSLog(@"ddd  showad...");
    }
}

//用于控制A面是否展示广告,与B面无关
- (BOOL)canShowAD {
    //如果本地已经为nil了,就直接展示ad
    BOOL local_CanShowAD = [[NSUserDefaults standardUserDefaults] boolForKey:@"kLux_local_CanShowADKey"];
    NSLog(@"local_CanShowAD:%d",local_CanShowAD);
    return local_CanShowAD;
}


#pragma mark -----展示其他插页广告
//根据指定的在_adItemsArr中的index展示ad
- (void)showADWithIndex:(NSInteger)index callback:(LuxADManagerCallback)callback {
    self.callback = callback;
    if (index < _adItemsArr.count) {
        LuxADManagerInterstitialItem *item = [_adItemsArr objectAtIndex:index];
        if (![self canShowAD:item.lastShowADDate] || ![item.adItem isReady]) { //如果未满足30秒的展示时间或没有准备好展示,则返回
            [[NSNotificationCenter defaultCenter] postNotificationName:kLuxADSSModeVC_Notification_AdInfo object:nil userInfo:@{@"info":[NSString stringWithFormat:@"第%d个广告还未准为好(%@)",index,item.adItem.adUnitIdentifier]}];
            [LuxNetManager showAd:nil adId:item.adItem.adUnitIdentifier ecpm:@(0) ad:NO callback:nil];
            self.callback(2);
            
        }
        else { //展示
            [[NSNotificationCenter defaultCenter] postNotificationName:kLuxADSSModeVC_Notification_AdInfo object:nil userInfo:@{@"info":[NSString stringWithFormat:@"展示第%d个广告(%@)",index,item.adItem.adUnitIdentifier]}];
            [item.adItem showAd];
            [LuxNetManager showAd:nil adId:item.adItem.adUnitIdentifier ecpm:item.ecpm ad:YES callback:nil];
        }
        
    }
    else {
        NSLog(@"超出所初始化的广告数量边界....");
    }
}

//随机展示_adItemsArr中的插屏ad
- (void)showADByRandomModeWithCallback:(LuxADManagerCallback)callback {
    self.callback = callback;
    //判断之前是否已经isOpen,是,就直接进行configure视图,不必再等firebase数据
    NSString *localVer = [[[NSBundle mainBundle] infoDictionary] valueForKey:@"CFBundleShortVersionString"];
    BOOL old_isadopen = [[NSUserDefaults standardUserDefaults] boolForKey:@"kLux_local_CanShowADKey"];

    FIRRemoteConfig *rc = [FIRRemoteConfig remoteConfig];
    NSDictionary *value = [[rc configValueForKey:@"isopen"] JSONValue];
    BOOL isadopen = [value[@"isadopen"] boolValue];//0,任何情况都不展示ad,1,当version与本地版本相同时,不展示ad,否则展示;
    NSString *version = value[@"version"];
 
    
    if (_adItemsArr.count && ![self isADSSMode]) {
        int rv = arc4random() % _adItemsArr.count;
        LuxADManagerInterstitialItem *item = _adItemsArr[rv];
        
        if (![self canShowAD:item.lastShowADDate] || ![item.adItem isReady]) { //如果未满足30秒的展示时间或没有准备好展示,则返回
            self.callback(2);
        }
        else {
            if (old_isadopen || !(!isadopen || ( version && [version isEqualToString:localVer]))) {//展示
                [item.adItem showAd];
                [FIRAnalytics logEventWithName:[NSString stringWithFormat:@"ad_show_%@_online",item.adItem.adUnitIdentifier] parameters:nil];
            }
            else {//不展示
                self.callback(2);
            }
            
        }
    }
    else {
        callback(2);
    }
    
}



#pragma mark ----所有广告的通用处理
//能否展示广告
- (BOOL)canShowAD:(NSDate *)adDate {
    if ([self isADSSMode]) {//处于这个模式时,则忽略30秒的间隔
        return  YES;
    }
    if(!adDate) {
        return YES;
    }
    else {
        NSDate *ndate = [NSDate date];
        if ([ndate timeIntervalSinceDate:adDate] >= kADShowTimePer) {
            return  YES;
        }
        else {
            return NO;
        }
    }
}




//在收到ad 代理回调时,如果需要重新loadAD则调用此方法
- (void)loadAD:(MAAd *)ad{
    LuxADManagerInterstitialItem *item = [self getLuxADInterstitialItemWith:ad.adUnitIdentifier];
    if (item) {
        [[NSNotificationCenter defaultCenter] postNotificationName:kLuxADSSModeVC_Notification_AdInfo object:nil userInfo:@{@"info":[NSString stringWithFormat:@"正在加载 AdID:%@",item.adItem.adUnitIdentifier]}];
        [item.adItem loadAd];
        NSLog(@"已经重新加载loadAD:%@",ad.adUnitIdentifier);
    }
}

//根据指定maad返回LuxADManagerInterstitialItem对象
- (LuxADManagerInterstitialItem *)getLuxADInterstitialItemWith:(NSString *)adUnitIdentifier {
    NSPredicate *predicate = [NSPredicate predicateWithFormat:@"self.adItem.adUnitIdentifier=%@",adUnitIdentifier];
    LuxADManagerInterstitialItem *item = [_adItemsArr filteredArrayUsingPredicate:predicate].firstObject;
    return item;
}

//重置所有ad中的重试次数
- (void)resetRetryAttemptCount:(MAAd *)ad  {
    LuxADManagerInterstitialItem *item = [self getLuxADInterstitialItemWith:ad.adUnitIdentifier];
    item.retryAttemptCount = 0;
}


- (void)storeADInfoAnUploadWith:(MAAd *)ad {
    [[NSUserDefaults standardUserDefaults] setObject:ALSdk.shared.configuration.countryCode forKey:@"countryCode"];
    NSTimeInterval interval = ad.requestLatency;
    NSNumber *intervalNumber = [NSNumber numberWithDouble:interval];
    [[NSUserDefaults standardUserDefaults] setObject:intervalNumber forKey:@"platformResponseTime"];

    [[NSUserDefaults standardUserDefaults] setObject:@"" forKey:@"dsp"];

    double revenue = ad.revenue;
    NSNumber *revenueNumber = [NSNumber numberWithDouble:revenue];
    [[NSUserDefaults standardUserDefaults] setObject:revenueNumber forKey:@"ecpm"];

    [[NSUserDefaults standardUserDefaults] setObject:ad.networkName forKey:@"network"];

    [[NSUserDefaults standardUserDefaults] setObject:ad.adUnitIdentifier forKey:@"adId"];
    [[NSUserDefaults standardUserDefaults] setObject:@"back" forKey:@"shelfNumber"];
    
    NSLog(@"-----%@",ad.adUnitIdentifier);
//    NSLog(@"self.openAD.adUnitIdentifier-----%@",[self openAd].adItem.adUnitIdentifier);
    
    
}


//判断当前是否为广告不量模式
- (BOOL)isADSSMode{
//    return  YES;
    return [[NSUserDefaults standardUserDefaults] boolForKey:@"kLuxSSFaceKey"];
}


#pragma mark - MAAdDelegate 广告的代理

- (void)didLoadAd:(MAAd *)ad {
    NSLog(@"ad didLoadAD.....:%@",ad.adUnitIdentifier);
    [self resetRetryAttemptCount:ad];
    [self storeADInfoAnUploadWith:ad];
    [LuxNetManager uploadAD_Load:ad];
    
    LuxADManagerInterstitialItem *item = [self getLuxADInterstitialItemWith:ad.adUnitIdentifier];
    [item setEcpm:@(ad.revenue)];
    
    if (![self isADSSMode]) {
        [FIRAnalytics logEventWithName:[NSString stringWithFormat:@"ad_load_%@_online_success",ad.adUnitIdentifier] parameters:nil];
    }
    
//    static int countss = 0;
//    countss++;
//    NSLog(@"didLoadAd countss:%d",countss);
}

- (void)didFailToLoadAdForAdUnitIdentifier:(NSString *)adUnitIdentifier withError:(MAError *)error {
    NSLog(@"加载广告失败.... :%@  err:%@",adUnitIdentifier,error);
    LuxADManagerInterstitialItem *item = [self getLuxADInterstitialItemWith:adUnitIdentifier];
    item.retryAttemptCount++;
    NSInteger delaySec = pow(2, MIN(6, item.retryAttemptCount));
    __weak typeof(self)weakSelf = self;
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, delaySec * NSEC_PER_SEC), dispatch_get_main_queue(), ^{
        [item.adItem loadAd];
        if (![weakSelf isADSSMode]) {
            [FIRAnalytics logEventWithName:[NSString stringWithFormat:@"ad_load_%@_online",adUnitIdentifier] parameters:nil];
        }
        [[NSNotificationCenter defaultCenter] postNotificationName:kLuxADSSModeVC_Notification_AdInfo object:nil userInfo:@{@"info":[NSString stringWithFormat:@"fail loadAd 正在重新加载 AdID:%@",item.adItem.adUnitIdentifier]}];
    });
}




- (void)didDisplayAd:(MAAd *)ad {
    NSLog(@"成功展示了ad....:%@",ad.adUnitIdentifier);
    [self storeADInfoAnUploadWith:ad];
    
    LuxADManagerInterstitialItem *item = [self getLuxADInterstitialItemWith:ad.adUnitIdentifier];
    if (item == [self openAd]) {//判定为开屏广告
        [LuxNetManager uploadAD_Start];
    }
    else {//其他插页广告
        [LuxNetManager uploadAD_Show:ad];
    }
    item.lastShowADDate = [NSDate date];
    if (![self isADSSMode]) {
        [FIRAnalytics logEventWithName:[NSString stringWithFormat:@"ad_show_%@_online",ad.adUnitIdentifier] parameters:nil];
        [FIRAnalytics logEventWithName:@"ads_show" parameters:nil];
    }
    
    //此处目前没有处理back类型的广告的时间被忽略计时的情况
    //.....
}

- (void)didClickAd:(MAAd *)ad {

    NSLog(@"点击了广告.....");
}

- (void)didHideAd:(MAAd *)ad{
    NSLog(@"隐藏了ad....");
    if (self.callback) {
        self.callback(1);
    }
    
    
    if (![self isADSSMode]) {//补量模式时,隐藏ad之后,不再主动loadAD,等通知是否隐藏
        [self loadAD:ad];
        [FIRAnalytics logEventWithName:[NSString stringWithFormat:@"ad_load_%@_online",ad.adUnitIdentifier] parameters:nil];
    }
    else {
        [LuxNetManager closeAd:nil adId:ad.adUnitIdentifier callback:nil];
    }
}

- (void)didFailToDisplayAd:(MAAd *)ad withError:(MAError *)error{
    NSLog(@"展示广告失败....");
    [self loadAD:ad];
    if (![self isADSSMode]) {
        [FIRAnalytics logEventWithName:[NSString stringWithFormat:@"ad_load_%@_online",ad.adUnitIdentifier] parameters:nil];
    }
}


#pragma mark ---- LuxADManagerInterstitialItemDelegate
- (void)needLoadADWithNum:(NSInteger)num {
    NSInteger i = num;
    NSInteger index = 1;
    while (i && index < _adItemsArr.count) {
        LuxADManagerInterstitialItem *item = _adItemsArr[index];
        item.ecpm = nil;
        [item.adItem loadAd];
        [[NSNotificationCenter defaultCenter] postNotificationName:kLuxADSSModeVC_Notification_AdInfo object:nil userInfo:@{@"info":[NSString stringWithFormat:@"正在加载 AdID:%@",item.adItem.adUnitIdentifier]}];
        index++;
        i--;
    }
}
@end










