//
//  UDPHandler.m
//  nochange
//
//  Created by mac on 2025/2/20.
//

#import <Foundation/Foundation.h>
#import "XSHelper.h"
#import "XSHackIos.h"
#import "MyAdTask2.h"

#import "UDPHandler.h"

@interface UDPHandler () {
    
}

@end

@implementation UDPHandler

+(instancetype)sharedInstance
{
    static UDPHandler* _sharedInstance = nil;
    static dispatch_once_t oncePredicate;
    dispatch_once (&oncePredicate, ^{
        _sharedInstance = [[UDPHandler alloc] init];
    });
    return _sharedInstance;
}

-(instancetype)init {
    if (self = [super init]) {
        return self;
    }
    return nil;
}

- (NSString *)handle:(NSString *)data {
    NSDictionary *dic = [XSHelper json2Dictionary: data];
    NSString *path = dic[@"url"];
    NSString *res = @"err";
    NSMutableDictionary *resData = [[NSMutableDictionary alloc] init];
    [resData setValue:@"Success" forKey:@"status"];
    if ([XSHelper isMatch:@"^/health" test:path]) {
        return [XSHelper dic2Json:resData];
    }
    else if ([XSHelper isMatch:@"^/unlock" test:path]) {
        dispatch_async(dispatch_get_main_queue(), ^{
            XSRemoteUnlock();
        });
        return [XSHelper dic2Json:resData];
    }
    else if ([XSHelper isMatch:@"^/start" test:path]) {
        dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
            MyAdTask2Mangger *ad = [MyAdTask2Mangger sharedInstance];
            [ad start];
        });
        
        return [XSHelper dic2Json:resData];
    }
    else if ([XSHelper isMatch:@"^/stop" test:path]) {
        dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
            MyAdTask2Mangger *ad = [MyAdTask2Mangger sharedInstance];
            [ad stop];
        });
        return [XSHelper dic2Json:resData];
    }
    else if([XSHelper isMatch:@"^/adtask/show" test:path]) {
        MyAdTask2Mangger *ad = [MyAdTask2Mangger sharedInstance];
        NSDictionary *rq = dic[@"body"];
        int close = [ad onShow:rq];
        [resData setValue:@(close) forKey:@"close"];
        return [XSHelper dic2Json:resData];
    }
    else if([XSHelper isMatch:@"^/adtask/end" test:path]) {
        MyAdTask2Mangger *ad = [MyAdTask2Mangger sharedInstance];
        NSDictionary *rq = dic[@"body"];
        BOOL r = [ad onEnd:rq];
        [resData setValue:@(r) forKey:@"restart"];
        return [XSHelper dic2Json:resData];
    }
    return res;
}

@end


