/*
 * 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 "YSSDImageCodersManager.h"
#import "YSSDImageIOCoder.h"
#import "YSSDImageGIFCoder.h"
#import "YSSDImageAPNGCoder.h"
#import "YSSDImageHEICCoder.h"
#import "YSSDInternalMacros.h"

@interface YSSDImageCodersManager ()

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

@end

@implementation YSSDImageCodersManager
{
    NSMutableArray<id<YSSDImageCoder>> *_imageCoders;
}

+ (nonnull instancetype)sharedManager {
    static dispatch_once_t once;
    static id instance;
    dispatch_once(&once, ^{
        instance = [self new];
    });
    return instance;
}

- (instancetype)init {
    if (self = [super init]) {
        // initialize with default coders
        _imageCoders = [NSMutableArray arrayWithArray:@[[YSSDImageIOCoder sharedCoder], [YSSDImageGIFCoder sharedCoder], [YSSDImageAPNGCoder sharedCoder]]];
        _codersLock = dispatch_semaphore_create(1);
    }
    return self;
}

- (NSArray<id<YSSDImageCoder>> *)coders
{
    YSSD_LOCK(self.codersLock);
    NSArray<id<YSSDImageCoder>> *coders = [_imageCoders copy];
    YSSD_UNLOCK(self.codersLock);
    return coders;
}

- (void)setCoders:(NSArray<id<YSSDImageCoder>> *)coders
{
    YSSD_LOCK(self.codersLock);
    [_imageCoders removeAllObjects];
    if (coders.count) {
        [_imageCoders addObjectsFromArray:coders];
    }
    YSSD_UNLOCK(self.codersLock);
}

#pragma mark - Coder IO operations

- (void)addCoder:(nonnull id<YSSDImageCoder>)coder {
    if (![coder conformsToProtocol:@protocol(YSSDImageCoder)]) {
        return;
    }
    YSSD_LOCK(self.codersLock);
    [_imageCoders addObject:coder];
    YSSD_UNLOCK(self.codersLock);
}

- (void)removeCoder:(nonnull id<YSSDImageCoder>)coder {
    if (![coder conformsToProtocol:@protocol(YSSDImageCoder)]) {
        return;
    }
    YSSD_LOCK(self.codersLock);
    [_imageCoders removeObject:coder];
    YSSD_UNLOCK(self.codersLock);
}

#pragma mark - YSSDImageCoder
- (BOOL)canDecodeFromData:(NSData *)data {
    NSArray<id<YSSDImageCoder>> *coders = self.coders;
    for (id<YSSDImageCoder> coder in coders.reverseObjectEnumerator) {
        if ([coder canDecodeFromData:data]) {
            return YES;
        }
    }
    return NO;
}

- (BOOL)canEncodeToFormat:(YSSDImageFormat)format {
    NSArray<id<YSSDImageCoder>> *coders = self.coders;
    for (id<YSSDImageCoder> coder in coders.reverseObjectEnumerator) {
        if ([coder canEncodeToFormat:format]) {
            return YES;
        }
    }
    return NO;
}

- (UIImage *)decodedImageWithData:(NSData *)data options:(nullable YSSDImageCoderOptions *)options {
    if (!data) {
        return nil;
    }
    UIImage *image;
    NSArray<id<YSSDImageCoder>> *coders = self.coders;
    for (id<YSSDImageCoder> coder in coders.reverseObjectEnumerator) {
        if ([coder canDecodeFromData:data]) {
            image = [coder decodedImageWithData:data options:options];
            break;
        }
    }
    
    return image;
}

- (NSData *)encodedDataWithImage:(UIImage *)image format:(YSSDImageFormat)format options:(nullable YSSDImageCoderOptions *)options {
    if (!image) {
        return nil;
    }
    NSArray<id<YSSDImageCoder>> *coders = self.coders;
    for (id<YSSDImageCoder> coder in coders.reverseObjectEnumerator) {
        if ([coder canEncodeToFormat:format]) {
            return [coder encodedDataWithImage:image format:format options:options];
        }
    }
    return nil;
}

@end
