//
//  ICUserManager.m
//  superCleaner
//
//  Created by Mazy on 2020/7/7.
//  Copyright © 2020 Mazy. All rights reserved.
//

#import "ICUserManager.h"
#import "Data.h"
#import "ZTUserInfoModel.h"

typedef void(^successBlock)(id);

static ICUserManager * manager;

@implementation ICUserManager

+ (instancetype)shared{
    static dispatch_once_t o;
    dispatch_once(&o, ^{
        manager = [[ICUserManager alloc] init];
    });
    return manager;
}

+ (instancetype)alloc{
    if (manager) {
        // 如果单例对象存在则抛出异常
        NSException *exception = [NSException exceptionWithName:[NSString stringWithFormat:@"重复创建%@单例对象 异常", [self class]] reason:@"请使用单例" userInfo:nil];
        [exception raise];
    }
    return [super alloc]; // 如果单例对象不存在 正常创建
}

- (id)copyWithZone:(struct _NSZone *)zone{
    return manager;
}

+ (id)allocWithZone:(struct _NSZone *)zone{
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        manager = [super allocWithZone:zone];
    });
    return manager;
}

- (NSDictionary *)h5_url{
    Data *data = [[Data alloc] init];
    return [[data Read] objectForKey:@"urlInfo"];
}

- (BOOL)isMember {
    
    NSString *dateString = [[NSUserDefaults standardUserDefaults] valueForKeyPath:@"expiryDate"];
    if (dateString.length <= 0) {
#ifdef DEBUG
            return false;
#else
        return self.model.isMember;
#endif
    }
    
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    formatter.dateFormat = @"yyyy-MM-dd HH:mm";
    NSDate *expiryDate = [formatter dateFromString:dateString];
    NSLog(@"expiryDate == %@", expiryDate);
    NSLog(@"currentDate %@", [NSDate date]);
    if ([expiryDate timeIntervalSinceDate:[NSDate date]] > 0) {
        return true;
    }
//    else if ([expiryDate timeIntervalSinceDate:[NSDate date]] <= 0) {
//        return false;
//    }
    return self.model.isMember;
}

- (NSString *)token{
    Data * data = [[Data alloc] init];
    return [[data Read] objectForKey:@"token"];
}

- (NSString *)uid{
    Data * data = [[Data alloc] init];
    return [[data Read] objectForKey:@"uid"];
}

// 获取用户信息
-(void)loginAndGetUserInfo: (void(^)(ZTUserInfoModel *model))success failure:(void(^)(void))failure {
    
    Data * data = [[Data alloc]init];  // 数据管理对象
    // 发送Post请求 登录请求 login
    [[ZTNetworkTools shared] postWithAction:touristLogin parameters:@{} success:^(id  _Nonnull response) {
        NSLog(@"%s:%d 返回结果:%@",__FILE__,__LINE__,response);
        
        if ([[response objectForKey:@"status"] intValue] != 200) {
            failure();
            return;
        }
        [data WirteDic: response[@"result"][@"data"][@"token"] Key:@"token"];
        NSString *token = [NSString stringWithFormat:@"%@", response[@"result"][@"data"][@"token"]];
        if (token.length > 0) {
            
            [self getUserInfo:^(ZTUserInfoModel *model) {
                success(model);
            } failure:^{
                failure();
            }];
            
        } else {
            failure();
        }
    } failure:^(NSError * _Nonnull error) {
        NSLog(@"%@", error);
        failure();
    }];
}

- (void)getUserInfo: (void(^)(ZTUserInfoModel *model))success failure:(void(^)(void))failure {
    
    [[ZTNetworkTools shared] getWithAction:userInfo parameters:@{} success:^(id  _Nonnull response) {
        NSLog(@"%@", response);
        NSString *uid = response[@"result"][@"data"][@"uid"];
        NSString *token = response[@"result"][@"data"][@"token"];
        Data * data = [[Data alloc] init];
        [data WirteDic:uid Key:@"uid"];
        [data WirteDic:token Key:@"token"];
        
        ZTUserInfoModel *model = [ZTUserInfoModel modelWithDic: response[@"result"][@"data"]];
        self.model = model;
        
        success(model);
        
    } failure:^(NSError * _Nonnull error) {
        NSLog(@"%@", error);
        failure();
    }];
}


//-(void)getSystemConfigInfo: (void(^)(ZTNovelInfoModel *model))success failure:(void(^)(void))failure {
//    [[ZTNetworkTools shared] getWithAction: getSystemConfig parameters:@{} success:^(id  _Nonnull response) {
//        NSLog(@"getSystemConfig %@", response);
//        if ([[response objectForKey:@"status"] intValue] != 200) {
//            failure();
//            return;
//        }
//        NSDictionary *dict = response[@"result"][@"data"][@"switch"];
//        ZTNovelInfoModel *model = [ZTNovelInfoModel modelWithDic: dict];
//        self.novelInfoModel = model;
//        success(model);
//    } failure:^(NSError * _Nonnull error) {
//        NSLog(@"getSystemConfig %@", error);
//        failure();
//    }];
//}


- (void)getServiceInfo{
    
    Data * data = [[Data alloc] init];
    [[ZTNetworkTools shared] getWithAction:appServiceInfo parameters:@{} success:^(id  _Nonnull response) {
        NSLog(@"===appServiceInfo===");
        if ([[response objectForKey:@"status"] integerValue] == 200){
            if ([[[response objectForKey:@"result"] objectForKey:@"data"] objectForKey:@"h5_url"]) {
                [data WirteDic:[[[response objectForKey:@"result"] objectForKey:@"data"] objectForKey:@"h5_url"] Key:@"urlInfo"];
            } else {
                [data removeDicForKey:@"urlInfo"];
            }
            if ([[[response objectForKey:@"result"] objectForKey:@"data"] objectForKey:@"config"]) {
                [data WirteDic:[[[response objectForKey:@"result"] objectForKey:@"data"] objectForKey:@"config"] Key:@"config"];
            } else {
                [data removeDicForKey:@"config"];
            }
        }
        
    } failure:^(NSError * _Nonnull error) {
        [data removeDicForKey:@"urlInfo"];
        NSLog(@"失败");
    }];
}

@end
