
#import "VerifyPayPasswordEngine.h"

#import "CurrentUser.h"

@implementation VerifyPayPasswordEngine

- (void)verifyPayPassword:(NSString *)password
{
    NSString *pw = [RSA encryptString:password publicKey:PUBLIC_KEY];
    
    NSString *randomKey = [self createKeyWithLength:24];
    
    NSString *src = [NSString stringWithFormat: @"<?xml version=\"1.0\" encoding=\"UTF-8\"?><msg service=\"authPayPwd\" sysName=\"attach\" sign=\"RSA\" serialNumber=\"%@\" termId=\"%@\" termAdd=\"23.23,23.45\" termBrand=\"iPhone\" osVersion=\"%@\" appVersion=\"%@\" tokenValue=\"%@\" userId=\"%@\" channelId=\"88888888\"><payPwd>%@</payPwd><attach>attach</attach></msg>",[self getSerialNumber],[BaseEngine termId],[BaseEngine osVersion],[BaseEngine appVersion],[CurrentUser sharedInstance].token,[CurrentUser sharedInstance].ID,pw];
    
    NSString *final = [NSString stringWithFormat:@"%@|%@|%@",[self firstString],[self secondStringWithKey:randomKey],[self thirdStringWithBody:src andKey:randomKey]];
    
    [YuHTTPClient XMLDataWithUrl:SERVER_HOST_CUSTOMER parameters:final success:^(BOOL isSuccess, id item){
        
        NSDictionary *result;
        if (isSuccess) {
            ZLLog(@"%@",item);
            NSString *resString = [_DES decrypt:[item lastObject] withKey:randomKey];
            
            NSDictionary *resultDic = [self parseXMLFromYinLianConnection:resString];
            
            ZLLog(@"%@",resultDic);
            
            NSMutableDictionary *tokenDic = [[NSMutableDictionary alloc] init];
            [tokenDic setObject:resultDic[@"tokenValue"] forKey:@"tokenValue"];
            [tokenDic setObject:resultDic[@"tokenTime"] forKey:@"tokenTimes"];
            
            result = @{@"isSuccess":@YES,@"result":tokenDic};
        }else{
            result = @{@"isSuccess":@NO,@"message":item};
        }
        
        [[NSNotificationCenter defaultCenter] postNotificationName:@"VerifyPayPasswordFinish" object:result];
    }];
    
}
- (void)NotiverifyPayPassword:(NSString *)password
{
    NSString *pw = [RSA encryptString:password publicKey:PUBLIC_KEY];
    
    NSString *randomKey = [self createKeyWithLength:24];
    
    NSString *src = [NSString stringWithFormat: @"<?xml version=\"1.0\" encoding=\"UTF-8\"?><msg service=\"authPayPwd\" sysName=\"attach\" sign=\"RSA\" serialNumber=\"%@\" termId=\"%@\" termAdd=\"23.23,23.45\" termBrand=\"iPhone\" osVersion=\"%@\" appVersion=\"%@\" tokenValue=\"%@\" userId=\"%@\" channelId=\"88888888\"><payPwd>%@</payPwd><attach>attach</attach></msg>",[self getSerialNumber],[BaseEngine termId],[BaseEngine osVersion],[BaseEngine appVersion],[CurrentUser sharedInstance].token,[CurrentUser sharedInstance].ID,pw];
    
    NSString *final = [NSString stringWithFormat:@"%@|%@|%@",[self firstString],[self secondStringWithKey:randomKey],[self thirdStringWithBody:src andKey:randomKey]];
    
    [YuHTTPClient XMLDataWithUrl:SERVER_HOST_AUTH parameters:final success:^(BOOL isSuccess, id item){
        
        NSDictionary *result;
        if (isSuccess) {
            ZLLog(@"%@",item);
            NSString *resString = [_DES decrypt:[item lastObject] withKey:randomKey];
            
            NSDictionary *resultDic = [self parseXMLFromYinLianConnection:resString];
            
            ZLLog(@"%@",resultDic);
            
            NSMutableDictionary *tokenDic = [[NSMutableDictionary alloc] init];
            [tokenDic setObject:resultDic[@"tokenValue"] forKey:@"tokenValue"];
            [tokenDic setObject:resultDic[@"tokenTime"] forKey:@"tokenTimes"];
            
            result = @{@"isSuccess":@YES,@"result":tokenDic};
        }else{
            result = @{@"isSuccess":@NO,@"message":item};
        }
        
        [[NSNotificationCenter defaultCenter] postNotificationName:@"NotiVerifyPayPasswordFinish" object:result];
    }];
    
}
+(VerifyPayPasswordEngine*)shareEngine{
    static VerifyPayPasswordEngine*payEngine=nil;
    static dispatch_once_t tradeToken;
    dispatch_once(&tradeToken, ^{
        if (payEngine==nil) {
            payEngine=[[VerifyPayPasswordEngine alloc] init];
        }
    });
    return payEngine;
}
@end
