//
//  CTShowAdTypeQuest.m
//  Open
//
//  Created by 雷俊博 on 2019/10/29.
//  Copyright © 2019 黄江涛 . All rights reserved.
//

#import "CTShowAdTypeQuest.h"
#import "CTAPIBaseManager.h"

@implementation CTShowAdTypeQuest

- (void)startRequestAdType
{
    NSString *url = [NSString stringWithFormat:@"%@/socialsecurity/advert/getAdvert",IPDIZHI];
    NSDictionary *infoDictionary = [[NSBundle mainBundle] infoDictionary];
    NSString *app_Version = [infoDictionary objectForKey:@"CFBundleShortVersionString"];
    NSDictionary *params = @{
               @"fromModel":@2,
               @"adverType":@1001,
               @"version": [app_Version stringByReplacingOccurrencesOfString:@"." withString:@""],
               @"terminalInfo":@"kksh"
           };
    
    [NetworkRequest SBAuthPost:url Params:params Success:^(id _Nonnull responseObject) {
        
        NSLog(@"广告类型responseObject:%@",responseObject);
        
        if ([responseObject[@"code"] isKindOfClass:[NSNumber class]]) {
            NSNumber *code = (NSNumber *)responseObject[@"code"];
            if (code.intValue == 0) {
                NSDictionary *result = responseObject[@"result"];
                if ([result isKindOfClass:[NSDictionary class]] && result.allKeys.count > 0) {
                    NSNumber *factoryIndex = result[@"factoryIndex"];
//                    NSString *factoryName = result[@"factoryName"];
                    CTAdType type = CTAdTypeUnKnow;

                    if (factoryIndex.integerValue == 1001001) {
                        type = CTAdTypeADmobile;
                    }else if (factoryIndex.integerValue == 1001002){
                        type = CTAdTypeJunYun;
                    }else if (factoryIndex.integerValue == 1001003){
                        type = CTAdTypeShengYang;
                    }else if (factoryIndex.integerValue == 1001004){
                        type = CTAdTypeChuanShanJia;
                    }else if (factoryIndex.integerValue == 1001005){
                        type = CTAdTypeTengXun;
                    }else if (factoryIndex.integerValue == 1001006){
                        type = CTAdTypeBaidu;
                    }else if (factoryIndex.integerValue == 1001007){
                        type = CTAdTypeKuaiShou;
                    }


                    [[NSUserDefaults standardUserDefaults]setInteger:factoryIndex.integerValue forKey:@"adType"];
//                    NSDate *lastOldTime = [[NSUserDefaults standardUserDefaults]objectForKey:@"adShowFirstDate"];
//
//                    BOOL needUpDate = YES;
//
//                    if (lastOldTime && [self isSameDay:[NSDate date] date2:lastOldTime]) {
//                        needUpDate = NO;
//                    }else{
//                        needUpDate = YES;
//                    }
//
//                    if (needUpDate) {
//                        [[NSUserDefaults standardUserDefaults]setInteger:factoryIndex.integerValue forKey:@"adType"];
//                        NSDate *oldDate = [NSDate date];
//                        [[NSUserDefaults standardUserDefaults]setObject:oldDate forKey:@"adShowFirstDate"];
//                        [[NSUserDefaults standardUserDefaults]synchronize];
//                    }
                    return;
//                           if (self.delegate && [self.delegate respondsToSelector:@selector(loadAdTypeApiSuccessWithAdType:adName:adIndex:)]) {
//                               [self.delegate loadAdTypeApiSuccessWithAdType:type adName:factoryName adIndex:factoryIndex.integerValue];
//                               return;
//                           }
                }

            }
            

        }
        
    } Failure:^(NSError* error) {
        NSLog(@"广告类型error:%@",error);
        
    }];
    
//           [CTAPIBaseManager POST4:url params:nil completionHandler:^(id  _Nonnull responseObject, NSError * _Nonnull error) {
//               if ([responseObject[@"code"] isKindOfClass:[NSNumber class]]) {
//                   NSNumber *code = (NSNumber *)responseObject[@"code"];
//                   if (code.intValue == 0) {
//                       NSDictionary *result = responseObject[@"result"];
//                       if ([result isKindOfClass:[NSDictionary class]] && result.allKeys.count > 0) {
//                           NSNumber *factoryIndex = result[@"factoryIndex"];
//                           NSString *factoryName = result[@"factoryName"];
//                           CTAdType type = CTAdTypeUnKnow;
//
//                           if (factoryIndex.integerValue == 1001001) {
//                               type = CTAdTypeADmobile;
//                           }else if (factoryIndex.integerValue == 1001002){
//                               type = CTAdTypeJunYun;
//                           }else if (factoryIndex.integerValue == 1001003){
//                               type = CTAdTypeShengYang;
//                           }else if (factoryIndex.integerValue == 1001004){
//                               type = CTAdTypeChuanShanJia;
//                           }else if (factoryIndex.integerValue == 1001005){
//                               type = CTAdTypeTengXun;
//                           }else if (factoryIndex.integerValue == 1001006){
//                               type = CTAdTypeBaidu;
//                           }
//
//
//
//                           NSDate *lastOldTime = [[NSUserDefaults standardUserDefaults]objectForKey:@"adShowFirstDate"];
//
//                           BOOL needUpDate = YES;
//
//                           if (lastOldTime && [self isSameDay:[NSDate date] date2:lastOldTime]) {
//                               needUpDate = NO;
//                           }else{
//                               needUpDate = YES;
//                           }
//
//                           if (needUpDate) {
//                               [[NSUserDefaults standardUserDefaults]setInteger:factoryIndex.integerValue forKey:@"adType"];
//                               NSDate *oldDate = [NSDate date];
//                               [[NSUserDefaults standardUserDefaults]setObject:oldDate forKey:@"adShowFirstDate"];
//                               [[NSUserDefaults standardUserDefaults]synchronize];
//                           }
//                           return;
////                           if (self.delegate && [self.delegate respondsToSelector:@selector(loadAdTypeApiSuccessWithAdType:adName:adIndex:)]) {
////                               [self.delegate loadAdTypeApiSuccessWithAdType:type adName:factoryName adIndex:factoryIndex.integerValue];
////                               return;
////                           }
//                       }
//
//                   }
//
//               }
//
//               if (self.delegate && [self.delegate respondsToSelector:@selector(loadAdTypeApiFailed)]) {
//                   [self.delegate loadAdTypeApiFailed];
//               }
//           }];
}

// 判断是否是同一天
- (BOOL)isSameDay:(NSDate*)date1 date2:(NSDate*)date2
{
    NSCalendar* calendar = [NSCalendar currentCalendar];
    unsigned unitFlags = NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit;
    NSDateComponents* comp1 = [calendar components:unitFlags fromDate:date1];
    NSDateComponents* comp2 = [calendar components:unitFlags fromDate:date2];
    return [comp1 day] == [comp2 day] &&
    [comp1 month] == [comp2 month] &&
    [comp1 year] == [comp2 year];
}

@end
