#import "JdsdkPlugin.h"
#import "JDSDK/JDKeplerSDK.h"

@interface JdsdkPlugin()
@end

@implementation JdsdkPlugin
+ (void)registerWithRegistrar:(NSObject<FlutterPluginRegistrar>*)registrar {
  FlutterMethodChannel* channel = [FlutterMethodChannel methodChannelWithName:@"jdsdk" binaryMessenger:[registrar messenger]];
  JdsdkPlugin* instance = [[JdsdkPlugin alloc] init];
  [registrar addMethodCallDelegate:instance channel:channel];
}

- (void)handleMethodCall:(FlutterMethodCall*)call result:(FlutterResult)result {
  if ([@"init" isEqualToString:call.method]) {
      NSString *appKey = call.arguments[@"appKey"];
      NSString *appSecret = call.arguments[@"appSecret"];
      [[KeplerApiManager sharedKPService]asyncInitSdk:appKey secretKey:appSecret sucessCallback:^(){
          result(@{@"errorCode":@"0",@"errorMessage":@"成功",@"data":@{}});
      }failedCallback:^(NSError *error){
          result(@{@"errorCode":@"-1",@"errorMessage":@"初始化失败",@"data":@{}});
      }];
  } else if([@"openUrl" isEqualToString:call.method]){
      NSString *url = call.arguments[@"url"];
      //判断是否安装京东app
      if ([[UIApplication sharedApplication]canOpenURL:[NSURL URLWithString:[NSString stringWithFormat:@"openapp.jdmobile://"]]]) {
          [[KeplerApiManager sharedKPService]openKeplerPageWithURL:url userInfo:nil successCallback:^{
              result(@YES);
        } failedCallback:^(NSInteger code, NSString * _Nonnull url) {
            result(@NO);
        }];
      }else{
          result(@NO);
      }
  } else {
    result(FlutterMethodNotImplemented);
  }
}

@end
