/********* IFLYPlugin.m Cordova Plugin Implementation *******/

#import <Cordova/CDV.h>
#import "SSCiflyTool.h"
#import <iflyMSC/iflyMSC.h>

@interface IFLYPlugin : CDVPlugin {
  // Member variables go here.
}
@property(nonatomic, strong) SSCiflyTool * tool;

- (void)openSpeechDialog:(CDVInvokedUrlCommand*)command;
@end

@implementation IFLYPlugin

- (void)pluginInitialize {
    //设置sdk的log等级，log保存在下面设置的工作路径中
    [IFlySetting setLogFile:LVL_ALL];
    //    打开输出在console的log开关
    [IFlySetting showLogcat:YES];
    //设置sdk的工作路径
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
    NSString *cachePath = [paths objectAtIndex:0];
    [IFlySetting setLogFilePath:cachePath];
    //注册id
    NSString *initString = [[NSString alloc] initWithFormat:@"appid=%@",@"5a4ed8cd"];
    [IFlySpeechUtility createUtility:initString];
}

- (void)openSpeechDialog:(CDVInvokedUrlCommand*)command
{
    self.tool = nil;
    self.tool = [SSCiflyTool shareTool];
    self.tool.master = [self.viewController view];
    
    __weak __typeof(self) weakSelf = self;
    [self.tool startRecognizer:^(IFlySpeechError *error, NSString *result) {
        
        CDVPluginResult *pluginResult = nil;
        __strong __typeof(self) strongSelf = weakSelf;
        
        if (error) {
             pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR];
        }else{
             pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsDictionary:@{@"result": result}];
        }
         [strongSelf.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId];
        
    }];

}

@end
