
#import <Flutter/Flutter.h>
#import "ZhiyingCommNative.h"
#import "ZhiyingCommNativeResult.h"

@implementation ZhiyingCommNative


static id<ZhiyingCommNativeProtocol> _delegate;

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


- (void)handleMethodCall:(FlutterMethodCall*)call result:(FlutterResult)result {
  if ([@"openPage" isEqualToString:call.method]) {
    if ([_delegate respondsToSelector:@selector(openPage:)]) {
      [_delegate openPage:call.arguments];
    }
    result(@{@"success": @"1"});
  } else if ([@"openNativePage" isEqualToString:call.method]) {
    if ([_delegate respondsToSelector:@selector(openNativePage:)]) {
      [_delegate openNativePage:call.arguments];
    }
    result(@{@"success": @"1"});
  } else if ([@"getSetting" isEqualToString:call.method]) {
    NSDictionary *setting = @{};
    if ([_delegate respondsToSelector:@selector(getSetting)]) {
      setting = [_delegate getSetting];
    }
    result(setting);
  }  else if ([@"invokeMethod" isEqualToString:call.method]) {
      NSDictionary *dict = call.arguments;
      NSString *method = [dict valueForKey:@"method"];
      NSDictionary *params = [dict valueForKey:@"params"];
    if ([_delegate respondsToSelector:@selector(invokeMethod:withParams:status:)]) {
      [_delegate invokeMethod:method withParams: params status: ^(ZhiyingCommNativeResult *res) {
         result([res toDict]);
      }];
    } else {
        result([NativeCommandNotImp toDict]);
    }
  } else{
      result(FlutterMethodNotImplemented);
  }
}

+ (void)regist:(id<ZhiyingCommNativeProtocol>) delegate {
    _delegate = delegate;
}

@end
