//
//  OJAPayManager.m
//  HiSports
//
//  Created by luxiaoming on 16/10/14.
//  Copyright © 2016年 com.ouj. All rights reserved.
//

#import "OJAPayManager.h"
#import "OJAPayWXService.h"
#import "OJAPayAliService.h"

@interface OJAPayManager ()

@property (nonatomic, copy, readwrite) NSString *kWeChatAppKey;
@property (nonatomic, copy, readwrite) NSString *kAliAppScheme;



@property (nonatomic, strong) OJAPayWXService *wxService;
@property (nonatomic, strong) OJAPayAliService *aliService;





@end

@implementation OJAPayManager

+ (instancetype)defaultManager {
    static OJAPayManager *sharedInstance = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        sharedInstance = [[OJAPayManager alloc] init];
    });
    return sharedInstance;
}

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

- (void)setupWithWeChatAppKey:(NSString *)weChatAppKey aliAppScheme:(NSString *)aliAppScheme {
    if (weChatAppKey && weChatAppKey.length > 0) {
        self.kWeChatAppKey = weChatAppKey;
    }
    if (aliAppScheme && aliAppScheme.length > 0) {
        self.kAliAppScheme = aliAppScheme;
    }
}



- (BOOL)handleOpenUrl:(NSURL *)url {
    if ([url.scheme isEqualToString:self.kWeChatAppKey] && [url.host isEqualToString:@"pay"]) {
        return [self.wxService handleOpenUrl:url];
    } else if ([url.host isEqualToString:@"safepay"]
               || [url.host isEqualToString:@"platformapi"]) {
        return [self.aliService handleOpenUrl:url];
    } else {
        return NO;
    }
}

- (void)payForPreOrder:(OJAPayPreOrderModel *)preOrderModel withPayType:(OJAPayType)payType completion:(OJAPayCompletion)completion {
    if (payType == OJAPayTypeWeXin) {
        [self.wxService payForPreOrder:preOrderModel completion:completion];
    }
    else if (payType == OJAPayTypeAli) {
        [self.aliService payForPreOrder:preOrderModel completion:completion];
    }
    else {
        
    }
}


#pragma mark - Property

- (OJAPayWXService *)wxService {
    if (!_wxService) {
        _wxService = [[OJAPayWXService alloc] init];
    }
    return _wxService;
}

- (OJAPayAliService *)aliService {
    if (!_aliService) {
        _aliService = [[OJAPayAliService alloc] init];
    }
    return _aliService;
}

@end
