//
//  LoginStateManager.m
//  AIIM
//
//  Created by qitewei on 2025/6/15.
//

#import "LoginStateManager.h"
#import "UserNetApi.h"
#import "UDManager.h"
#import "APPLocker.h"
#import "GWebSocket.h"
#import "GDBManager.h"

@implementation LoginStateManager
{
    BOOL _loggedIn;
    NSString *_currentUserToken;
    NSString *_currentUserID;
}
+ (instancetype)sharedManager {
    static LoginStateManager *instance = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        instance = [[LoginStateManager alloc] init];
        [instance loadLoginStatusFromStorage];
    });
    return instance;
}

- (instancetype)init {
    self = [super init];
    if (self) {
        _currentUserToken = [UDManager.shareInstance getSDManager:gkeytoken] ?: @"";
        _currentUserID = [UDManager.shareInstance getSDManager:dkuserId] ?: @"";;
        _loggedIn = _currentUserToken.length && _currentUserID.length;
    }
    return self;
}

- (BOOL)isLoggedIn {
    return _loggedIn;
}

- (NSString *)currentUserToken {
    return _currentUserToken;
}

- (NSString *)currentUserID {
    return _currentUserID;
}

- (void)requestUserInfoWith:(void(^)(BOOL isSuccess))completion {
    [UserNetApi getUserinfo:^(int code, NSDictionary * res) {
        //NSLog(@"%@", res[@"code"]);
        NSNumber *gcode=res[@"code"];
        NSDictionary *data=res[@"data"];
       // NSLog(@"%@", data);
        if([gcode intValue]!=200){
            NSLog(@"%@",res[@"msg"]);
            [self postLogout];
            if (completion) {
                completion(NO);
            }
            return;
        }
        NSDictionary *ps = @{@"avatar": data[@"avatar"]?:@"",
                             @"deptId": data[@"deptId"]?:@"",
                             @"email":data[@"email"]?:@"",
                             @"id":data[@"id"]?:@"",
                             @"mobile":data[@"mobile"]?:@"",
                             @"name":data[@"name"]?:@"",
                             @"sex":data[@"sex"]?:@"",
                             @"username":data[@"username"]?:@"",
                            };

        self->_loggedIn = YES;
        [UDManager.shareInstance setDDManager:ps key:dkuserinfo];
        [UDManager.shareInstance setSDManager:ps[@"id"] key:dkuserId];
        
        if (completion) {
            completion(YES);
        }
    } fail:^(NSError * _Nonnull error) {
        NSLog(@"%@", error);
        if (completion) {
            completion(NO);
        }
    }];
}

- (void)logout {
    // 这里应该是实际的登出网络请求
    [UserNetApi logout:nil succ:^(int code, NSDictionary * res) {
        
    } fail:^(NSError * _Nonnull error) {
        NSLog(@"");
    }];
    [self postLogout];
}

- (void)postLogout {
    [[NSNotificationCenter defaultCenter] postNotificationName: nkonLogoutSucc object:nil];
}

- (void)cleanLoginState{
    _currentUserToken = @"";
    _currentUserID = @"";
    [[GWebSocket shareInstance] closeWebSocket];
    [UDManager.shareInstance removeUDManager:gkeytoken];
    [UDManager.shareInstance removeUDManager:dkuserloginId];
    [UDManager.shareInstance removeUDManager:dkuserinfo];
    [UDManager.shareInstance removeUDManager:dkuserId];
    [APPLocker.sharedLocker removePasswordWithError:nil];
    [self clearVOIP];
    self->_loggedIn = NO;
}

#pragma mark - 本地存储管理

- (void)saveLoginStatusToStorage {
    
}

- (void)loadLoginStatusFromStorage {
    
}

- (void)clearVOIP {
    NSString *voiptoken = [UDManager.shareInstance getSDManager:dkvoiptoken];
    NSLog(@"voiptoken:%@",voiptoken);
    if([voiptoken isKindOfClass:[NSString class]]&&voiptoken.length>10){
        NSDictionary *dic = @{
            @"voipTk":voiptoken
        };
        [UserNetApi clearVoipTk:dic succ:^(int code, NSDictionary * res) {
            NSLog(@"clearVoipTk:%@",res);
        } fail:^(NSError * _Nonnull error) {
            
        }];
    }
    
    NSString *pushtoken = [UDManager.shareInstance getSDManager:dkpushtoken];
    NSLog(@"voiptoken:%@",pushtoken);
    if([pushtoken isKindOfClass:[NSString class]]&&pushtoken.length>10){
        NSDictionary *dic = @{
            @"registrationId":pushtoken
        };
        [UserNetApi clearRegistrationId:dic succ:^(int code, NSDictionary * res) {
            NSLog(@"clearRegistrationId:%@",res);
        } fail:^(NSError * _Nonnull error) {
            
        }];
    }
}

@end
