//
//  TencentQQ.mm
//  Unity-iPhone
//
//  Created by os on 2025/5/25.
//

#import <TencentOpenAPI/TencentOAuth.h>
#import "TencentQQ.h"
#import "GameHelper.h"
#import "CrossPlatformCmd.h"

@implementation TencentQQ

static TencentQQ *instance;

+ (instancetype) Instance
{
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        instance = [[TencentQQ alloc] init];
    });
    return instance;
}

- (void)Init
{
    NSLog(@"QQ init successful!");
    [[TencentOAuth sharedInstance] setupAppId:@"102740522" enableUniveralLink:YES universalLink:nil delegate:self];
}

- (void)Login
{
    NSArray* permission = [NSArray arrayWithObjects:kOPEN_PERMISSION_GET_INFO,kOPEN_PERMISSION_GET_USER_INFO,kOPEN_PERMISSION_GET_SIMPLE_USER_INFO, nil];
    [[TencentOAuth sharedInstance] authorize:permission];
}

- (void)Logout
{
    [[TencentOAuth sharedInstance] logout:self];
}

- (BOOL)CheckAuthorVaild
{
    NSString *openId = [TencentOAuth sharedInstance].getCachedOpenID;
    NSString *token = [TencentOAuth sharedInstance].getCachedToken;
    
    NSUserDefaults *login = [NSUserDefaults standardUserDefaults];
    NSString *unionId = [login objectForKey:@"QQunionid"];
    
    NSDate *expries = [TencentOAuth sharedInstance].getCachedExpirationDate;
    [TencentOAuth sharedInstance].openId = openId;
    [TencentOAuth sharedInstance].accessToken = token;
    [TencentOAuth sharedInstance].expirationDate = expries;
    [TencentOAuth sharedInstance].unionid = unionId;
    
    NSLog(@"QQ CheckAuthorVaild: %d", [TencentOAuth sharedInstance].isCachedTokenValid);
    return [TencentOAuth sharedInstance].isCachedTokenValid;
}

- (NSString*)GetAuthor
{
    NSString* openId = [TencentOAuth sharedInstance].openId;
    NSString* token = [TencentOAuth sharedInstance].accessToken;
    NSString* unionId = [TencentOAuth sharedInstance].unionid;
    NSString* refreshToken = @"";
    NSDateFormatter *format = [[NSDateFormatter alloc] init];
    format.dateFormat = @"yyyy-MM-dd HH:mm:ss";
    NSString* expires = [format stringFromDate:[TencentOAuth sharedInstance].expirationDate];
    NSString* payToken = @"";
    NSString* pf = @"";
    NSString* pfKey = @"";
    NSString* expir = @"";
    NSMutableDictionary *dic = [NSMutableDictionary dictionaryWithObjectsAndKeys:
                                openId,@"OpenId",
                                token,@"Token",
                                unionId,@"UnionId",
                                refreshToken,@"RefreshToken",
                                expires,@"Expires",
                                payToken,@"PayToken",
                                pf,@"Pf",
                                pfKey,@"PfKey",
                                expir,@"Expir",
                                nil];
    NSData *jsonData = [NSJSONSerialization dataWithJSONObject:dic options:0 error:NULL];
    NSString *jsonString = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
    NSLog(@"QQ GetAuthor: [%@]", jsonString);
    
    NSUserDefaults *login = [NSUserDefaults standardUserDefaults];
    [login setObject:unionId forKey:@"QQunionid"];
    [login synchronize];
    return jsonString;
}

- (void)tencentDidLogin
{
    if([TencentOAuth sharedInstance].accessToken && [TencentOAuth sharedInstance].accessToken.length != 0)
    {
        NSLog(@"QQ login successful!");
        BOOL request = [TencentOAuth sharedInstance].RequestUnionId;
        if(!request)
        {
            NSLog(@"Request union id failed!");
        }
    }
    else
    {
        NSLog(@"QQ login failed!");
        SendPlatformMsgToUnity(UNITY_MSG_TENCENT_QQ_LOGIN_CALLBACK, TENCENT_QQ_LOGIN_CALLBACK_FAILED, 0, 0, @"", @"", @"");
    }
}

- (void)tencentDidNotLogin:(BOOL)cancelled
{
    if (cancelled)
    {
        NSLog(@"QQ login cancelled!");
        SendPlatformMsgToUnity(UNITY_MSG_TENCENT_QQ_LOGIN_CALLBACK, TENCENT_QQ_LOGIN_CALLBACK_CANCELLED, 0, 0, @"", @"", @"");
    }
    else
    {
        NSLog(@"QQ login failed by cancelled!");
        SendPlatformMsgToUnity(UNITY_MSG_TENCENT_QQ_LOGIN_CALLBACK, TENCENT_QQ_LOGIN_CALLBACK_FAILED, 0, 0, @"", @"", @"");
    }
}

- (void)tencentDidNotNetWork
{
    NSLog(@"QQ login not network!");
    SendPlatformMsgToUnity(UNITY_MSG_TENCENT_QQ_LOGIN_CALLBACK, TENCENT_QQ_LOGIN_CALLBACK_FAILED, 0, 0, @"", @"", @"");
}

- (void)didGetUnionID
{
    NSString *jsonString = [self GetAuthor];
    NSLog(@"Get QQ Author successful!");
    SendPlatformMsgToUnity(UNITY_MSG_TENCENT_QQ_LOGIN_CALLBACK, TENCENT_QQ_LOGIN_CALLBACK_SUCCESS, 0, 0, jsonString, @"", @"");
}

@end
