/*
 * This file is part of the YSSDWebImage package.
 * (c) Olivier Poitrey <rs@dailymotion.com>
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */

#import "UIView+YSWebCacheOperation.h"
#import "objc/runtime.h"

static char loadOperationKey;

// key is strong, value is weak because operation instance is retained by YSSDWebImageManager's runningOperations property
// we should use lock to keep thread-safe because these method may not be accessed from main queue
typedef NSMapTable<NSString *, id<YSSDWebImageOperation>> YSSDOperationsDictionary;

@implementation UIView (YSWebCacheOperation)

- (YSSDOperationsDictionary *)yssd_operationDictionary {
    @synchronized(self) {
        YSSDOperationsDictionary *operations = objc_getAssociatedObject(self, &loadOperationKey);
        if (operations) {
            return operations;
        }
        operations = [[NSMapTable alloc] initWithKeyOptions:NSPointerFunctionsStrongMemory valueOptions:NSPointerFunctionsWeakMemory capacity:0];
        objc_setAssociatedObject(self, &loadOperationKey, operations, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
        return operations;
    }
}

- (nullable id<YSSDWebImageOperation>)yssd_imageLoadOperationForKey:(nullable NSString *)key  {
    id<YSSDWebImageOperation> operation;
    if (key) {
        YSSDOperationsDictionary *operationDictionary = [self yssd_operationDictionary];
        @synchronized (self) {
            operation = [operationDictionary objectForKey:key];
        }
    }
    return operation;
}

- (void)yssd_setImageLoadOperation:(nullable id<YSSDWebImageOperation>)operation forKey:(nullable NSString *)key {
    if (key) {
        [self yssd_cancelImageLoadOperationWithKey:key];
        if (operation) {
            YSSDOperationsDictionary *operationDictionary = [self yssd_operationDictionary];
            @synchronized (self) {
                [operationDictionary setObject:operation forKey:key];
            }
        }
    }
}

- (void)yssd_cancelImageLoadOperationWithKey:(nullable NSString *)key {
    if (key) {
        // Cancel in progress downloader from queue
        YSSDOperationsDictionary *operationDictionary = [self yssd_operationDictionary];
        id<YSSDWebImageOperation> operation;
        
        @synchronized (self) {
            operation = [operationDictionary objectForKey:key];
        }
        if (operation) {
            if ([operation conformsToProtocol:@protocol(YSSDWebImageOperation)]) {
                [operation cancel];
            }
            @synchronized (self) {
                [operationDictionary removeObjectForKey:key];
            }
        }
    }
}

- (void)yssd_removeImageLoadOperationWithKey:(nullable NSString *)key {
    if (key) {
        YSSDOperationsDictionary *operationDictionary = [self yssd_operationDictionary];
        @synchronized (self) {
            [operationDictionary removeObjectForKey:key];
        }
    }
}

@end
