//
//  extra.m
//  arena
//  额外拓展的接口
//  Created by 张琳 on 2017/10/13.
//  Copyright © 2017年 江苏斑马软件技术有限公司. All rights reserved.
//

#import "extra.h"

@implementation extra

/* 导出模块 */
RCT_EXPORT_MODULE(extra);

/// 跳转到其他微应用
/// - Parameter openUri: 跳转参数
/// - Parameter findEvents: 结果回调
RCT_EXPORT_METHOD(openUri:(NSDictionary *)param findEvents:(RCTResponseSenderBlock)callback)
{
    NSLog(@"原生接收到的RN参数:%@",param);
    
    NSMutableDictionary * finalParam = [[NSMutableDictionary alloc]init];
    [finalParam setValue:@"arena.extra.openUri" forKey:@"method"];
    [finalParam setValue:param forKey:@"params"];
    [finalParam setValue:@"rn" forKey:@"type"];
    
    
    void (^successBlock)(id) = ^(id result){
        callback(@[[NSNull null], result]);
    };
    
    void (^failedBlock)(id) = ^(id result){
        callback(@[result,[NSNull null]]);
    };
    
    NSDictionary *dic = @{@"success":successBlock , @"failed":failedBlock};
    
    /* 主要这里必须使用主线程发送,不然有可能失效 */
    dispatch_async(dispatch_get_main_queue(), ^{
        /* 把收到的RN的参数用通知发送出去 */
        [[NSNotificationCenter defaultCenter] postNotificationName:@"dispatch" object:dic userInfo:finalParam];
    });
}

/// 分享
/// - Parameter share: 分享参数
/// - Parameter findEvents: 分享结果回调
RCT_EXPORT_METHOD(share:(NSDictionary *)param findEvents:(RCTResponseSenderBlock)callback)
{
    NSLog(@"原生接收到的RN参数:%@",param);
    
    NSMutableDictionary * finalParam = [[NSMutableDictionary alloc]init];
    [finalParam setValue:@"arena.extra.share" forKey:@"method"];
    [finalParam setValue:param forKey:@"params"];
    [finalParam setValue:@"rn" forKey:@"type"];
    
    
    void (^successBlock)(id) = ^(id result){
        callback(@[[NSNull null], result]);
    };
    
    void (^failedBlock)(id) = ^(id result){
        callback(@[result,[NSNull null]]);
    };
    
    NSDictionary *dic = @{@"success":successBlock , @"failed":failedBlock};
    
    /* 主要这里必须使用主线程发送,不然有可能失效 */
    dispatch_async(dispatch_get_main_queue(), ^{
        /* 把收到的RN的参数用通知发送出去 */
        [[NSNotificationCenter defaultCenter] postNotificationName:@"dispatch" object:dic userInfo:finalParam];
    });
}

/// 退出登录
/// - Parameter logout: 退出登录的参数
/// - Parameter findEvents: 退出登录的回调
RCT_EXPORT_METHOD(logout:(NSDictionary *)param findEvents:(RCTResponseSenderBlock)callback)
{
    NSLog(@"原生接收到的RN参数:%@",param);
    
    NSMutableDictionary * finalParam = [[NSMutableDictionary alloc]init];
    [finalParam setValue:@"arena.extra.logout" forKey:@"method"];
    [finalParam setValue:param forKey:@"params"];
    [finalParam setValue:@"rn" forKey:@"type"];
    
    
    void (^successBlock)(id) = ^(id result){
        callback(@[[NSNull null], result]);
    };
    
    void (^failedBlock)(id) = ^(id result){
        callback(@[result,[NSNull null]]);
    };
    
    NSDictionary *dic = @{@"success":successBlock , @"failed":failedBlock};
    
    /* 主要这里必须使用主线程发送,不然有可能失效 */
    dispatch_async(dispatch_get_main_queue(), ^{
        /* 把收到的RN的参数用通知发送出去 */
        [[NSNotificationCenter defaultCenter] postNotificationName:@"dispatch" object:dic userInfo:finalParam];
    });
}

/// 获取APP版本号
/// - Parameter getVersion: 获取版本号的参数
/// - Parameter findEvents: 获取版本号的回调
RCT_EXPORT_METHOD(getVersion:(NSDictionary *)param findEvents:(RCTResponseSenderBlock)callback)
{
    NSLog(@"原生接收到的RN参数:%@",param);
    
    NSMutableDictionary * finalParam = [[NSMutableDictionary alloc]init];
    [finalParam setValue:@"arena.extra.version" forKey:@"method"];
    [finalParam setValue:param forKey:@"params"];
    [finalParam setValue:@"rn" forKey:@"type"];
    
    
    void (^successBlock)(id) = ^(id result){
        callback(@[[NSNull null], result]);
    };
    
    void (^failedBlock)(id) = ^(id result){
        callback(@[result,[NSNull null]]);
    };
    
    NSDictionary *dic = @{@"success":successBlock , @"failed":failedBlock};
    
    /* 主要这里必须使用主线程发送,不然有可能失效 */
    dispatch_async(dispatch_get_main_queue(), ^{
        /* 把收到的RN的参数用通知发送出去 */
        [[NSNotificationCenter defaultCenter] postNotificationName:@"dispatch" object:dic userInfo:finalParam];
    });
}

/// 数据回调
/// - Parameter setResult: 数据回调的参数
/// - Parameter findEvents: 数据回调的回调结果
RCT_EXPORT_METHOD(setResult:(NSDictionary *)param findEvents:(RCTResponseSenderBlock)callback)
{
    NSLog(@"原生接收到的RN参数:%@",param);
    
    NSMutableDictionary * finalParam = [[NSMutableDictionary alloc]init];
    [finalParam setValue:@"arena.extra.setResult" forKey:@"method"];
    [finalParam setValue:param forKey:@"params"];
    [finalParam setValue:@"rn" forKey:@"type"];
    
    
    void (^successBlock)(id) = ^(id result){
        callback(@[[NSNull null], result]);
    };
    
    void (^failedBlock)(id) = ^(id result){
        callback(@[result,[NSNull null]]);
    };
    
    NSDictionary *dic = @{@"success":successBlock , @"failed":failedBlock};
    
    /* 主要这里必须使用主线程发送,不然有可能失效 */
    dispatch_async(dispatch_get_main_queue(), ^{
        /* 把收到的RN的参数用通知发送出去 */
        [[NSNotificationCenter defaultCenter] postNotificationName:@"dispatch" object:dic userInfo:finalParam];
    });
}

@end
