//
//  IMSOpenAccount.m
//  IMSAccount
//
//  Created by Hager Hu on 01/11/2017.
//

#import "IMSOpenAccount.h"

#import <ALBBOpenAccountCloud/ALBBOpenAccountSDK.h>
#import <ALBBOpenAccountCloud/ALBBOpenAccountUser.h>
#import <IMSThingCapability/IMSThingManager.h>
#import <IMSApiClient/IMSConfiguration.h>
#import <IMSAccount/IMSAccountService.h>
#define JK_IS_STR_NIL(objStr) (![objStr isKindOfClass:[NSString class]] || objStr == nil || [objStr length] <= 0)

// Add 2 to IMSNotificationAccountLogin to solve duplicate symbol '_IMSNotificationAccountLogin' in:
//    /Users/YourUserName/Library/Developer/Xcode/DerivedData/YourIosProject-ekesvbeuoqlenafmgeclowzyvhjf/Build/Products/Debug-iphoneos/react-native-ali-smartliving/libreact-native-ali-smartliving.a(IMSOpenAccount.o)
//    YourRnProject/ios/Pods/IMSIotSmart/IMSIotSmart/IMSIotSmart.framework/IMSIotSmart(SmartOpenAccount.o)
NSString * _Nonnull const IMSNotificationAccountLogin2 = @"IMSNotificationAccountLogin";
NSString * _Nonnull const IMSNotificationAccountLogout2 = @"IMSNotificationAccountLogout";
@interface IMSOpenAccount()<SSODelegate>
@end
@implementation IMSOpenAccount

#pragma mark - IMSAccountProtocol

+ (instancetype)sharedInstance {
    static IMSOpenAccount *account;
    
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        account = [[[self class] alloc] init];
    });
    
    return account;
}

- (id)init {
    if (self = [super init]) {
// 下面这些应该是旧版本 SDK 的初始化，所以暂时注释掉，后续删除
//        // TODO: AEP平台对外只暴露线上环境，此处环境设置联调完成后需要更改
//        //[[ALBBOpenAccountSDK sharedInstance] setDebugLogOpen:YES];
//        IMSConfiguration *configuration = [IMSConfiguration sharedInstance];
//        
//        //设置安全图片
//        [[ALBBOpenAccountSDK sharedInstance] setSecGuardImagePostfix:configuration.authCode];
//        
//        //设置环境
//        TaeSDKEnvironment taeEnv = TaeSDKEnvironmentRelease;
//        if (configuration.serverEnv == IMSServerDaily) {
//            taeEnv = TaeSDKEnvironmentDaily;
//        } else if (configuration.serverEnv == IMSServerPreRelease) {
//            taeEnv = TaeSDKEnvironmentPreRelease;
//        }
//        //IMSAccountLogInfo(@"IMSConfiguration env:%d Account env:%d", configuration.serverEnv, taeEnv);
//        [[ALBBOpenAccountSDK sharedInstance] setTaeSDKEnvironment:taeEnv];
//        
//        // 与 @一宵 沟通，不存在初始化失败的情况，所以不需要抛到外层处理
//        dispatch_semaphore_t semaphore = dispatch_semaphore_create(0);
//        [[ALBBOpenAccountSDK sharedInstance] asyncInit:^{
//            //IMSAccountLogInfo(@"accountSDK success");
//            dispatch_semaphore_signal(semaphore);
//        } failure:^(NSError *error) {
//            //IMSAccountLogInfo(@"accountSDK error:%@", error);
//            dispatch_semaphore_signal(semaphore);
//        }];
//        
//        dispatch_semaphore_wait(semaphore, DISPATCH_TIME_FOREVER);
    }
    
    return self;
}

- (NSString *)accountDidLoginSuccessNotificationName {
    return IMSNotificationAccountLogin2;
}

- (NSString *)accountDidLogoutSuccessNotificationName {
    return IMSNotificationAccountLogout2;
}

- (NSString *)accountType {
    return @"OA_SESSION";
}

- (NSString *)token {
    return [ALBBOpenAccountSession sharedInstance].sessionID;
}

- (BOOL)isLogin {
    return [[ALBBOpenAccountSession sharedInstance] isLogin];
}

- (void)logout {
    [[NSNotificationCenter defaultCenter] postNotificationName:IMSNotificationAccountLogout2 object:nil];
    [[ALBBOpenAccountSession sharedInstance] logout];

    // SDK 在使用过程中会保存本地通信加速的相关数据到手机沙盒目录，当手机账号登出时可以清理这些缓存
    [kIMSThingManager clearLocalCache];
}

- (NSDictionary *)currentSession {
    NSMutableDictionary *info = [@{} mutableCopy];
    
    ALBBOpenAccountSession *session = [ALBBOpenAccountSession sharedInstance];
    if ([session isLogin]) {
        [info addEntriesFromDictionary:@{
                                         ACCOUNT_SESSION_KEY: session.sessionID ? :@"",
                                         }];
        
        ALBBOpenAccountUser *user = [ALBBOpenAccountSession sharedInstance].getUser;
        NSString *nickName = user.displayName;
        if (!nickName || [nickName length] == 0) {
            nickName = @"";
        }
        [info addEntriesFromDictionary:@{
                                             ACCOUNT_USER_ID_KEY: user.accountId ? :@"",
                                             ACCOUNT_NICKNAME_KEY: nickName ? :@"",
                                             ACCOUNT_AVATAR_URL_KEY: user.avatarUrl ? :@"",
                                             @"mobile":user.mobile ? :@""
                                             }];
    }
    
    return info;
}
- (NSDictionary *)getCurrentSession{
  return [self currentSession];
}
- (void)updateAccountProfileWithNickName:(NSString *)nickName
                               avatarUrl:(NSString *)avatarUrl
                                  gender:(NSString *)gender
                                response:(void(^)(BOOL isSuccess,NSError *error))response{
  id<ALBBOpenAccountService> accountService = ALBBService(ALBBOpenAccountService);
  
  NSMutableDictionary *accountProfile = [NSMutableDictionary dictionary];
  
  if (JK_IS_STR_NIL(nickName)) {
    [accountProfile setObject:nickName forKey:@"displayName"];
  }
  if (JK_IS_STR_NIL(avatarUrl)) {
    [accountProfile setObject:avatarUrl forKey:@"avatarUrl"];
  }
  if (JK_IS_STR_NIL(gender)) {
    [accountProfile setObject:gender forKey:@"gender"];
  }
  
  [accountService updateAccountProfile:accountProfile.copy Callback:^(NSError *error) {
      response(error==nil,error);
  }];
  
}

#pragma mark - IMSAccountUIProtocol

- (void)showLoginWithController:(UIViewController *)controller
                        success:(void (^)(NSDictionary *response))success
                        failure:(void (^)(NSError *error))failure {
  
    id<ALBBOpenAccountUIService> uiService = ALBBService(ALBBOpenAccountUIService);
    
    [uiService presentLoginViewController:controller success:^(ALBBOpenAccountSession *currentSession) {
        //登录成功回调
        
        if (success) {
            success([self currentSession]);
            [[NSNotificationCenter defaultCenter] postNotificationName:IMSNotificationAccountLogin2 object:nil];
        }
        
    } failure:^(NSError *error) {
        //登录失败回调
        if (failure) {
            failure(error);
        }
    }];
}
- (void)thirdLoginWithAuthCode:(NSString *)code{
    id<ALBBOpenAccountSSOService> ssoService = ALBBService(ALBBOpenAccountSSOService);
    [ssoService oauthWithThirdParty:code delegate:self];
}
- (void)openAccountOAuthError:(NSError *)error Session:(ALBBOpenAccountSession *)session{
  self.thirdLoginResult(error, [self currentSession]);
}
@end
