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

#import "GameHelper.h"
#import "CrossPlatformCmd.h"
#import <sys/sysctl.h>
#import <mach/mach.h>
#import "TencentQQ.h"
#import "IAPManager.h"

@implementation GameHelper

extern "C"
{
    extern void SendPlatformMsgToUnity(int msgId,
                                       int paramInt1, int paramInt2, int paramInt3,
                                       NSString *paramString1, NSString *paramString2, NSString *paramString3)
    {
        NSMutableDictionary *dic = [NSMutableDictionary dictionaryWithObjectsAndKeys:
                                    [NSNumber numberWithInt:msgId], @"MsgId",
                                    [NSNumber numberWithInt:paramInt1], @"ParamInt1",
                                    [NSNumber numberWithInt:paramInt2], @"ParamInt2",
                                    [NSNumber numberWithInt:paramInt3], @"ParamInt3",
                                    paramString1, @"ParamString1",
                                    paramString2, @"ParamString2",
                                    paramString3, @"ParamString3"
                                    , nil];
        
        NSData *jsonData = [NSJSONSerialization dataWithJSONObject:dic options:0 error:NULL];
        NSString *jsonString = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
        NSLog(@"iOS send message to unity, msg id: %d json: %@", msgId, jsonString);
        UnitySendMessage("[Platform]", "OnMessage", jsonString.UTF8String);
    }
}

extern "C"
{
    extern void InnerSendUnityMessageToPlatform(int msgId,
                                           int paramInt1, int paramInt2, int paramInt3,
                                           char *paramString1, char *paramString2, char *paramString3)
    {
        NSString *string1 = [[NSString alloc] initWithUTF8String:paramString1];
        NSString *string2 = [[NSString alloc] initWithUTF8String:paramString2];
        NSString *string3 = [[NSString alloc] initWithUTF8String:paramString3];
        
        switch(msgId)
        {
            case PLATFORM_MSG_TENCENT_QQ_LOGIN:
                {
                    TencentQQ* tencentQQ = [TencentQQ Instance];
                    [tencentQQ Login];
                }
                break;
            case PLATFORM_MSG_TENCENT_QQ_LOGOUT:
                {
                    TencentQQ* tencentQQ = [TencentQQ Instance];
                    [tencentQQ Logout];
                }
                break;
            case PLATFORM_MSG_TENCENT_WX_LOGIN:
                break;
            case PLATFORM_MSG_TENCENT_WX_LOGOUT:
                break;
            case PLATFORM_MSG_APPLE_PAY:
                {
                    IAPManager *iapManager = [IAPManager Instance];
                    [iapManager BuyProduct:string1];
                }
                break;
            default:
                break;
        }
    }
}

extern "C"
{
    extern int InnerGetIntFormPlatform(int msgId)
    {
        switch(msgId)
        {
            default:
                return -1;
        }
    }
}

extern "C"
{
    extern char* InnerGetStringFormPlatform(int msgId)
    {
        switch(msgId)
        {
            case GET_STRING_MSG_TENCENT_QQ_CHECK_AUTHOR_VALID:
                {
                    TencentQQ* tencentQQ = [TencentQQ Instance];
                    if([tencentQQ CheckAuthorVaild] == true)
                    {
                        const char* str = "true";
                        char* res = (char*)malloc(strlen(str)+1);
                        strcpy(res, str);
                        return res;
                    }
                    else
                    {
                        const char* str = "false";
                        char* res = (char*)malloc(strlen(str)+1);
                        strcpy(res, str);
                        return res;
                    }
                }
                break;
            case GET_STRING_MSG_TENCENT_QQ_REFRESH_SESSION:
                {
                    TencentQQ* tencentQQ = [TencentQQ Instance];
                    NSString *jsonString = [tencentQQ GetAuthor];
                    const char* str = [jsonString UTF8String];
                    char* res = (char*)malloc(strlen(str)+1);
                    return res;
                }
                break;
            case GET_STRING_MSG_APPLE_CAN_MAKE_PAYMENT:
                {
                    IAPManager* iapManager = [IAPManager Instance];
                    if([iapManager CanMakePayment] == true)
                    {
                        const char* str = "true";
                        char* res = (char*)malloc(strlen(str)+1);
                        strcpy(res, str);
                        return res;
                    }
                    else
                    {
                        const char* str = "false";
                        char* res = (char*)malloc(strlen(str)+1);
                        strcpy(res, str);
                        return res;
                    }
                }
                break;
            default:
                return nil;
        }
    }
}

extern "C"
{
    extern long InnerGetLongFormPlatform(int msgId)
    {
        switch(msgId)
        {
            case GET_LONG_MSG_SYSTEM_TOTAL_MEMORY:
                return [GameHelper GetTotalMemnorySize];
            case GET_LONG_MSG_SYSTEM_AVAILABLE_MEMORY:
                return [GameHelper GetAvailableMemory];
            case GET_LONG_MSG_SYSTEM_USED_MEMORY:
                return [GameHelper GetUseMemory];
            default:
                return -1;
        }
    }
}

extern "C"
{
    extern long InnerGetLongFormPlatform2(int msgId,
                                     int paramInt1, int paramInt2, int paramInt3,
                                     NSString *paramString1, NSString *paramString2, NSString *paramString3)
    {
        switch(msgId)
        {
            default:
                return -1;
        }
    }
}

+(long)GetTotalMemnorySize
{
    return [NSProcessInfo processInfo].physicalMemory;
}

+(long)GetAvailableMemory
{
    vm_statistics_data_t vmStats;
    mach_msg_type_name_t infoCount = HOST_VM_INFO_COUNT;
    kern_return_t kernRenture = host_statistics(mach_host_self(), HOST_VM_INFO, (host_info_t)&vmStats, &infoCount);
    if (kernRenture != KERN_SUCCESS) {
        return NSNotFound;
    }
    return (vm_page_size* vmStats.free_count);
}

+(long)GetUseMemory
{
    task_basic_info_data_t taskInfo;
    mach_msg_type_name_t infoCount = TASK_BASIC_INFO_COUNT;
    kern_return_t kernRenture = task_info(mach_host_self(), TASK_BASIC_INFO, (task_info_t)&taskInfo, &infoCount);
    if (kernRenture != KERN_SUCCESS) {
        return NSNotFound;
    }
    return taskInfo.resident_size;
}

@end
