/*
 * 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 "UIImageView+YSHighlightedWebCache.h"

#if YSSD_UIKIT

#import "UIView+YSWebCacheOperation.h"
#import "UIView+YSWebCache.h"
#import "YSSDInternalMacros.h"

static NSString * const YSSDHighlightedImageOperationKey = @"UIImageViewImageOperationHighlighted";

@implementation UIImageView (YSHighlightedWebCache)

- (void)yssd_setHighlightedImageWithURL:(nullable NSURL *)url {
    [self yssd_setHighlightedImageWithURL:url options:0 progress:nil completed:nil];
}

- (void)yssd_setHighlightedImageWithURL:(nullable NSURL *)url options:(YSSDWebImageOptions)options {
    [self yssd_setHighlightedImageWithURL:url options:options progress:nil completed:nil];
}

- (void)yssd_setHighlightedImageWithURL:(nullable NSURL *)url options:(YSSDWebImageOptions)options context:(nullable YSSDWebImageContext *)context {
    [self yssd_setHighlightedImageWithURL:url options:options context:context progress:nil completed:nil];
}

- (void)yssd_setHighlightedImageWithURL:(nullable NSURL *)url completed:(nullable YSSDExternalCompletionBlock)completedBlock {
    [self yssd_setHighlightedImageWithURL:url options:0 progress:nil completed:completedBlock];
}

- (void)yssd_setHighlightedImageWithURL:(nullable NSURL *)url options:(YSSDWebImageOptions)options completed:(nullable YSSDExternalCompletionBlock)completedBlock {
    [self yssd_setHighlightedImageWithURL:url options:options progress:nil completed:completedBlock];
}

- (void)yssd_setHighlightedImageWithURL:(NSURL *)url options:(YSSDWebImageOptions)options progress:(nullable YSSDImageLoaderProgressBlock)progressBlock completed:(nullable YSSDExternalCompletionBlock)completedBlock {
    [self yssd_setHighlightedImageWithURL:url options:options context:nil progress:progressBlock completed:completedBlock];
}

- (void)yssd_setHighlightedImageWithURL:(nullable NSURL *)url
                              options:(YSSDWebImageOptions)options
                              context:(nullable YSSDWebImageContext *)context
                             progress:(nullable YSSDImageLoaderProgressBlock)progressBlock
                            completed:(nullable YSSDExternalCompletionBlock)completedBlock {
    @WeakObj(self);
    YSSDWebImageMutableContext *mutableContext;
    if (context) {
        mutableContext = [context mutableCopy];
    } else {
        mutableContext = [NSMutableDictionary dictionary];
    }
    mutableContext[YSSDWebImageContextSetImageOperationKey] = YSSDHighlightedImageOperationKey;
    [self yssd_internalSetImageWithURL:url
                    placeholderImage:nil
                             options:options
                             context:mutableContext
                       setImageBlock:^(UIImage * _Nullable image, NSData * _Nullable imageData, YSSDImageCacheType cacheType, NSURL * _Nullable imageURL) {
                           @StrongObj(self);
                           self.highlightedImage = image;
                       }
                            progress:progressBlock
                           completed:^(UIImage * _Nullable image, NSData * _Nullable data, NSError * _Nullable error, YSSDImageCacheType cacheType, BOOL finished, NSURL * _Nullable imageURL) {
                               if (completedBlock) {
                                   completedBlock(image, error, cacheType, imageURL);
                               }
                           }];
}

@end

#endif
