//
//  RNCouchbaseLite.m
//  RNCouchbaseLite
//
//  Created by Houssem Eddine ZAIEM on 30/10/2019.
//  Copyright © 2019 Facebook. All rights reserved.
//


#import "RNCouchbaseLite.h"

#include <CouchbaseLite/CouchbaseLite.h>

@implementation RNCouchbaseLite

- (dispatch_queue_t)methodQueue
{
    return dispatch_get_main_queue();
}

RCT_EXPORT_MODULE();

static NSString * const MESSAGE = @"It works !";

@synthesize database=_database;

- (NSArray<NSString *> *) supportedEvents
{
    return @[@"RNCouchbaseLite_databaseDidUpdate"];
}

RCT_EXPORT_METHOD(getMessageFromNativeModule:(RCTPromiseResolveBlock)resolve rejecter:(RCTPromiseRejectBlock)reject) {
    resolve(MESSAGE);
}

RCT_EXPORT_METHOD(createDatabase: (NSString *)databaseName resolver:(RCTPromiseResolveBlock)resolve rejecter:(RCTPromiseRejectBlock)reject) {
    NSError *error;
    _database = [[CBLDatabase alloc] initWithName:databaseName error:&error];
    if (!_database) {
        NSLog(@"Cannot open the database: %@", error);
        //reject(@"no_database", @"Cannot open the database.", error);
    } else {
        //resolve(@"database created successfully.");
    }
    
    CBLMutableDocument *mutableDoc = [[CBLMutableDocument alloc] init];
    [mutableDoc setString: @"lastName" forKey: @"ZAIEM"];
    
    // Save it to the database.
    [_database saveDocument:mutableDoc error:&error];
    
    if(error) {
        //reject(@"data_error",@"Error when saving data to database", error);
    } else {
        //resolve(@"data saved successfully !");
    }
    
    CBLQuery *query = [CBLQueryBuilder select:@[[CBLQuerySelectResult all]]
     from:[CBLQueryDataSource database:_database]];
    
    // Run the query
    CBLQueryResultSet *result = [query execute:&error];
    
    if(!result) {
        //reject(@"data_query",@"no data found", error);
    } else {
        NSLog(@"rows :: %lu", (unsigned long)[[result allResults] count]);
        //resolve(result);
    }
    resolve(@"finished !");
}

RCT_EXPORT_METHOD(saveDocument: (NSString *)key value:(NSString *)value resolver:(RCTPromiseResolveBlock)resolve rejecter:(RCTPromiseRejectBlock)reject) {
    NSError *error;
    // Create a new document (i.e. a record) in the database.

    CBLMutableDocument *mutableDoc = [[CBLMutableDocument alloc] init];
    [mutableDoc setString: value forKey: key];
    
    // Save it to the database.
    [_database saveDocument:mutableDoc error:&error];
    
    if(error) {
        reject(@"data_error",@"Error when saving data to database", error);
    } else {
        resolve(@"data saved successfully !");
    }
}

RCT_EXPORT_METHOD(fetchDocuments: (NSString *)property value:(NSString *)value resolver:(RCTPromiseResolveBlock)resolve rejecter:(RCTPromiseRejectBlock)reject) {
    NSError *error;
    // Create a new document (i.e. a record) in the database.

    CBLQuery *query = [CBLQueryBuilder select:@[[CBLQuerySelectResult all]]
     from:[CBLQueryDataSource database:_database]];
    
    // Run the query
    CBLQueryResultSet *result = [query execute:&error];
    
    if(!result) {
        reject(@"data_query",@"no data found", error);
    } else {
        NSLog(@"rows :: %lu", (unsigned long)[[result allResults] firstObject]);
        resolve([[result allResults] firstObject]);
    }
}

@end
  

