/*
 *    	Copyright 2015-2016 GetSocial B.V.
 *
 *	Licensed under the Apache License, Version 2.0 (the "License");
 *	you may not use this file except in compliance with the License.
 *	You may obtain a copy of the License at
 *
 *    	http://www.apache.org/licenses/LICENSE-2.0
 *
 *	Unless required by applicable law or agreed to in writing, software
 *	distributed under the License is distributed on an "AS IS" BASIS,
 *	WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *	See the License for the specific language governing permissions and
 *	limitations under the License.
 */

#import "GetSocialChatCordova.h"

@implementation GetSocialChatCordova

-(void)unreadConversationsCount:(CDVInvokedUrlCommand *)command {
    NSString *callbackId = [command callbackId];
    
    [self successWithNumberResult:[GetSocialChat sharedInstance].unreadConversationsCount andCallbackId:callbackId];
}

-(void)isEnable:(CDVInvokedUrlCommand *)command {
    NSString *callbackId = [command callbackId];
    
    [self successWithBoolResult:[GetSocialChat sharedInstance].isEnabled andCallbackId:callbackId];
}

-(void)setOnUnreadConversationsCountChangeListener:(CDVInvokedUrlCommand *)command {
    NSString *callbackId = [command callbackId];
    
    [[GetSocialChat sharedInstance] setOnChatNotificationsChangeHandler:^(NSInteger unreadConversationsCount) {
        CDVPluginResult *result =
        [CDVPluginResult resultWithStatus:CDVCommandStatus_OK
                          messageAsInt:(int)unreadConversationsCount];
        [result setKeepCallbackAsBool:YES];
        [self.commandDelegate sendPluginResult:result callbackId:callbackId];
    }];
}

-(void)createChatListView:(CDVInvokedUrlCommand *)command{
    NSString *callbackId = [command callbackId];
    NSDictionary *option = [[command arguments] objectAtIndex:0];
    
    GetSocialChatListViewBuilder *viewBuilder = [[GetSocialChat sharedInstance] createChatListView];
    
    NSString *title = [option objectForKey:@"title"];
    if (![self isEmptyOrNull:title])
        viewBuilder.title = title;
    
    [viewBuilder show];
    
    [self successWithCallbackId:callbackId];
}

-(void) createChatView:(CDVInvokedUrlCommand *)command {
    NSString *callbackId = [command callbackId];
    NSDictionary *option = [[command arguments] objectAtIndex:0];
    
    GetSocialChatViewBuilder *globalChatView = nil;
    
    NSString *roomName = [option objectForKey:@"roomName"];
    if (![self isEmptyOrNull:roomName]) {
        globalChatView = [[GetSocialChat sharedInstance] createChatViewForRoomName:roomName];
    }
    
    NSString *conversationId = [option objectForKey:@"conversationId"];
    if (![self isEmptyOrNull:conversationId]) {
        [self errorWithMsg:@"Not implemented in this version of plugin" andCallbackId:callbackId];
        return;
    }
    
    NSString *userId = [option objectForKey:@"userId"];
    if(![self isEmptyOrNull:userId]) {
        //globalChatView = [[GetSocialChat sharedInstance] createChatViewForUserId:userId];
        [self errorWithMsg:@"Not implemented" andCallbackId:callbackId];
        return;
    }
    
    NSString *providerId = [option objectForKey:@"providerId"];
    if(![self isEmptyOrNull:userId] && ![self isEmptyOrNull:providerId]) {
        //globalChatView = [[GetSocialChat sharedInstance] createChatViewForUserId:userId onProvider:providerId];
        [self errorWithMsg:@"Not implemented" andCallbackId:callbackId];
        return;
    }
    
    NSString *title = [option objectForKey:@"title"];
    if(globalChatView != nil) {
        if(![self isEmptyOrNull:title])
           [globalChatView setTitle:title];
        
        [globalChatView show];
        [self successWithCallbackId:callbackId];
        return;
    }
    
    [self errorWithMsg:@"Wrong params" andCallbackId:callbackId];
}

- (BOOL)isEmptyOrNull:(NSString *)data {
    if(data == nil || data.length == 0) {
        return true;
    }
    
    return false;
}

@end
