#import "ReadyUI.h"
#import <MessageSDKFramework/MessageSDK.h>


@implementation ReadyUI{
    MessageSDK *mycometchat;
}

- (void)seturl:(CDVInvokedUrlCommand*)command{
    
    [[NSUserDefaults standardUserDefaults] setObject:[NSString stringWithFormat:@"%@",[[command arguments] objectAtIndex:0]] forKey:@"siteurl"];
    
    [self.commandDelegate sendPluginResult:[CDVPluginResult
                                            resultWithStatus:CDVCommandStatus_OK] callbackId:command.callbackId];
}

- (void)loginWithUsername:(CDVInvokedUrlCommand*)command{
    
    mycometchat = [[MessageSDK alloc] init];
    
    [mycometchat loginWithURL:[[NSUserDefaults standardUserDefaults] objectForKey:@"siteurl"] username:[NSString stringWithFormat:@"%@",[[command arguments] objectAtIndex:0]] password:[NSString stringWithFormat:@"%@",[[command arguments] objectAtIndex:1]] observer:self.viewController success:^(NSDictionary *response) {
        
        NSDictionary *successDict = @{@"success":response};
        
        [self.commandDelegate sendPluginResult:[CDVPluginResult
                                                resultWithStatus:CDVCommandStatus_OK messageAsDictionary:successDict] callbackId:command.callbackId];
        
    } userinfo:^(NSDictionary *response){
        
        NSDictionary *successDict = @{@"success":response};
        
        [self.commandDelegate sendPluginResult:[CDVPluginResult
                                                resultWithStatus:CDVCommandStatus_OK messageAsDictionary:successDict] callbackId:command.callbackId];
        
    } chatroominfo:^(NSDictionary *response){
        
        NSDictionary *successDict = @{@"success":response};
        
        [self.commandDelegate sendPluginResult:[CDVPluginResult
                                                resultWithStatus:CDVCommandStatus_OK messageAsDictionary:successDict] callbackId:command.callbackId];
        
    } onMessageReceive:^(NSDictionary *response){
        
        NSDictionary *successDict = @{@"success":response};
        
        [self.commandDelegate sendPluginResult:[CDVPluginResult
                                                resultWithStatus:CDVCommandStatus_OK messageAsDictionary:successDict] callbackId:command.callbackId];
        
    }failure:^(NSError *error) {
        
        NSDictionary *errorDict = @{@"error":error};
        
        NSLog(@"Login Error : %@",errorDict);
        
        //[self.commandDelegate sendPluginResult:[CDVPluginResult
        //resultWithStatus:CDVCommandStatus_OK messageAsDictionary:errorDict] callbackId:command.callbackId];
        
    }];
    
    
    
}

- (void)loginWithUserid:(CDVInvokedUrlCommand*)command{
    
    mycometchat = [[MessageSDK alloc] init];
    
    [mycometchat loginWithURL:[[NSUserDefaults standardUserDefaults] objectForKey:@"siteurl"] userid:[NSString stringWithFormat:@"%@",[[command arguments] objectAtIndex:0]] observer:self.viewController success:^(NSDictionary *response) {
        
        NSDictionary *successDict = @{@"success":response};
        
        [self.commandDelegate sendPluginResult:[CDVPluginResult
                                                resultWithStatus:CDVCommandStatus_OK messageAsDictionary:successDict] callbackId:command.callbackId];
        
    } userinfo:^(NSDictionary *response){
        
    } chatroominfo:^(NSDictionary *response){
        
    } onMessageReceive:^(NSDictionary *response){
        
    }failure:^(NSError *error) {
        
        NSDictionary *errorDict = @{@"error":error};
        
        [self.commandDelegate sendPluginResult:[CDVPluginResult
                                                resultWithStatus:CDVCommandStatus_OK messageAsDictionary:errorDict] callbackId:command.callbackId];
        
    }];
    
}

- (void)logout:(CDVInvokedUrlCommand*)command{
    
    [[NSUserDefaults standardUserDefaults] removeObjectForKey:@"siteurl"];
    mycometchat = [[MessageSDK alloc] init];
    [mycometchat logoutWithSuccess:^(NSDictionary *response) {
        
        NSDictionary *successDict = @{@"success":response};
        
        [self.commandDelegate sendPluginResult:[CDVPluginResult
                                                resultWithStatus:CDVCommandStatus_OK messageAsDictionary:successDict] callbackId:command.callbackId];
        
    } failure:^(NSError *error) {
        
        NSDictionary *errorDict = @{@"error":error};
        
        [self.commandDelegate sendPluginResult:[CDVPluginResult
                                                resultWithStatus:CDVCommandStatus_OK messageAsDictionary:errorDict] callbackId:command.callbackId];
        
    }];
    
    [self.commandDelegate sendPluginResult:[CDVPluginResult
                                            resultWithStatus:CDVCommandStatus_OK] callbackId:command.callbackId];
    
}

- (void)chatWithMe:(CDVInvokedUrlCommand*)command{
    
    mycometchat = [[MessageSDK alloc] init];
    [mycometchat chatWith:[[command arguments] objectAtIndex:0] setBackButton:YES observer:self.viewController success:^(NSDictionary *response) {
        
        NSDictionary *successDict = @{@"success":response};
        
        [self.commandDelegate sendPluginResult:[CDVPluginResult
                                                resultWithStatus:CDVCommandStatus_OK messageAsDictionary:successDict] callbackId:command.callbackId];
        
    } failure:^(NSError *error) {
        
        NSDictionary *errorDict = @{@"error":error};
        
        [self.commandDelegate sendPluginResult:[CDVPluginResult
                                                resultWithStatus:CDVCommandStatus_OK messageAsDictionary:errorDict] callbackId:command.callbackId];
        
    }];
    
    
}

- (void)changeLang:(CDVInvokedUrlCommand*)command{
    
    mycometchat = [[MessageSDK alloc] init];
    
    [mycometchat changeLanguage:[[command arguments] objectAtIndex:0] success:^(NSDictionary *response){
        
        NSDictionary *successDict = @{@"success":response};
        
        [self.commandDelegate sendPluginResult:[CDVPluginResult
                                                resultWithStatus:CDVCommandStatus_OK messageAsDictionary:successDict] callbackId:command.callbackId];
        
    } failure:^(NSError *error){
        
        NSDictionary *errorDict = @{@"error":error};
        
        [self.commandDelegate sendPluginResult:[CDVPluginResult
                                                resultWithStatus:CDVCommandStatus_OK messageAsDictionary:errorDict] callbackId: command.callbackId];
        
    }];
    
}
@end
