//
// © 2018-2020 Qualcomm Technologies, Inc. and/or its subsidiaries. All rights reserved.
//

#import "MockDispatcher.h"
#import "IoTGlobals.h"
#import "IoTWrappedRepresentation.h"

@interface MockDispatcher ()

@property (nonatomic, copy) NSString *host;
@property (nonatomic, copy) NSString *uri;
@property (nonatomic, copy) NSString *resourceType;
@property (nonatomic, copy) NSArray<NSString *> *interfaces;
@property (nonatomic, assign) BOOL observable;


@property (nonatomic, copy) MockDispatcherDidConfigure configureCallback;
@property (nonatomic, copy) MockDispatcherDidGetResource getCallback;
@property (nonatomic, copy) MockDispatcherDidPostResource postCallback;
@property (nonatomic, copy) MockDispatcherDidObserveResourceChange observeCallback;

@end

@implementation MockDispatcher

- (instancetype)initWithDidConfigureCallback:(MockDispatcherDidConfigure)configureCallback
                                 getCallback:(MockDispatcherDidGetResource)getCallback
                                postCallback:(MockDispatcherDidPostResource)postCallback
                             observeCallback:(MockDispatcherDidObserveResourceChange)observeCallback
{
    self = [self init];
    if (self) {
        self->_configureCallback = [configureCallback copy];
        self->_getCallback = [getCallback copy];
        self->_postCallback = [postCallback copy];
        self->_observeCallback = [observeCallback copy];
    }
    return self;
}

- (void)configureForHost:(NSString *)host
                     uri:(NSString *)uri
            resourceType:(NSString *)resourceType
              interfaces:(NSArray<NSString *> *)interfaces
              observable:(BOOL)observable;
{
    self.host = host;
    self.uri = uri;
    self.resourceType = resourceType;
    self.interfaces = interfaces;
    self.observable = observable;
    
    makeWeak(self)
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0), ^{
        makeStrongAndReturnIfNil(self)
        if (self.configureCallback) {
            self.configureCallback(self);
        }
    });
}

- (BOOL)getWithQueryParameters:(IoTQueryParams)params
                    completion:(IoTTransportDispatcherCompletion)completion
               completionQueue:(dispatch_queue_t)completionQueue
{
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0), ^{
        if (self.getCallback) {
            self.getCallback(self, params, completion, completionQueue);
        } else {
            completion([IoTWrappedRepresentation emptyWrapper], 0);
        }
    });
    return YES;
}

- (BOOL)postAttributes:(IoTWrappedRepresentation *)representation
       queryParameters:(IoTQueryParams)queryParams
            completion:(IoTTransportDispatcherCompletion)completion
       completionQueue:(dispatch_queue_t)completionQueue
{
    makeWeak(self)
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0), ^{
        makeStrongAndReturnIfNil(self)
        if (self.postCallback) {
            self.postCallback(self, queryParams, representation, completion, completionQueue);
        } else {
            completion([IoTWrappedRepresentation emptyWrapper], 0);
        }
    });
	return YES;
}

- (BOOL)startObservingWithQueryParameters:(IoTQueryParams)params
                               didObserve:(IoTTransportDispatcherObserveCompletion)didObserve
                             observeQueue:(dispatch_queue_t)observeQueue

{
    makeWeak(self)
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0), ^{
        makeStrongAndReturnIfNil(self)
        if (self.observeCallback) {
            self.observeCallback(self, params, didObserve, observeQueue);
        }
    });
	return YES;
}

- (void)stopObserving
{
}

@end
