/*
 * 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 "YSSDImageLoadersManager.h"
#import "YSSDWebImageDownloader.h"
#import "YSSDInternalMacros.h"

@interface YSSDImageLoadersManager ()

@property (nonatomic, strong, nonnull) dispatch_semaphore_t loadersLock;

@end

@implementation YSSDImageLoadersManager
{
    NSMutableArray<id<YSSDImageLoader>>* _imageLoaders;
}

+ (YSSDImageLoadersManager *)sharedManager {
    static dispatch_once_t onceToken;
    static YSSDImageLoadersManager *manager;
    dispatch_once(&onceToken, ^{
        manager = [[YSSDImageLoadersManager alloc] init];
    });
    return manager;
}

- (instancetype)init {
    self = [super init];
    if (self) {
        // initialize with default image loaders
        _imageLoaders = [NSMutableArray arrayWithObject:[YSSDWebImageDownloader sharedDownloader]];
        _loadersLock = dispatch_semaphore_create(1);
    }
    return self;
}

- (NSArray<id<YSSDImageLoader>> *)loaders {
    YSSD_LOCK(self.loadersLock);
    NSArray<id<YSSDImageLoader>>* loaders = [_imageLoaders copy];
    YSSD_UNLOCK(self.loadersLock);
    return loaders;
}

- (void)setLoaders:(NSArray<id<YSSDImageLoader>> *)loaders {
    YSSD_LOCK(self.loadersLock);
    [_imageLoaders removeAllObjects];
    if (loaders.count) {
        [_imageLoaders addObjectsFromArray:loaders];
    }
    YSSD_UNLOCK(self.loadersLock);
}

#pragma mark - Loader Property

- (void)addLoader:(id<YSSDImageLoader>)loader {
    if (![loader conformsToProtocol:@protocol(YSSDImageLoader)]) {
        return;
    }
    YSSD_LOCK(self.loadersLock);
    [_imageLoaders addObject:loader];
    YSSD_UNLOCK(self.loadersLock);
}

- (void)removeLoader:(id<YSSDImageLoader>)loader {
    if (![loader conformsToProtocol:@protocol(YSSDImageLoader)]) {
        return;
    }
    YSSD_LOCK(self.loadersLock);
    [_imageLoaders removeObject:loader];
    YSSD_UNLOCK(self.loadersLock);
}

#pragma mark - YSSDImageLoader

- (BOOL)canRequestImageForURL:(nullable NSURL *)url {
    NSArray<id<YSSDImageLoader>> *loaders = self.loaders;
    for (id<YSSDImageLoader> loader in loaders.reverseObjectEnumerator) {
        if ([loader canRequestImageForURL:url]) {
            return YES;
        }
    }
    return NO;
}

- (id<YSSDWebImageOperation>)requestImageWithURL:(NSURL *)url options:(YSSDWebImageOptions)options context:(YSSDWebImageContext *)context progress:(YSSDImageLoaderProgressBlock)progressBlock completed:(YSSDImageLoaderCompletedBlock)completedBlock {
    if (!url) {
        return nil;
    }
    NSArray<id<YSSDImageLoader>> *loaders = self.loaders;
    for (id<YSSDImageLoader> loader in loaders.reverseObjectEnumerator) {
        if ([loader canRequestImageForURL:url]) {
            return [loader requestImageWithURL:url options:options context:context progress:progressBlock completed:completedBlock];
        }
    }
    return nil;
}

- (BOOL)shouldBlockFailedURLWithURL:(NSURL *)url error:(NSError *)error {
    NSArray<id<YSSDImageLoader>> *loaders = self.loaders;
    for (id<YSSDImageLoader> loader in loaders.reverseObjectEnumerator) {
        if ([loader canRequestImageForURL:url]) {
            return [loader shouldBlockFailedURLWithURL:url error:error];
        }
    }
    return NO;
}

@end
