/*
 * 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 "NSButton+YSWebCache.h"

#if YSSD_MAC

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

static NSString * const YSSDAlternateImageOperationKey = @"NSButtonAlternateImageOperation";

@implementation NSButton (YSWebCache)

#pragma mark - Image

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

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

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

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

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

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

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

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

- (void)yssd_setImageWithURL:(nullable NSURL *)url
          placeholderImage:(nullable UIImage *)placeholder
                   options:(YSSDWebImageOptions)options
                   context:(nullable YSSDWebImageContext *)context
                  progress:(nullable YSSDImageLoaderProgressBlock)progressBlock
                 completed:(nullable YSSDExternalCompletionBlock)completedBlock {
    self.yssd_currentImageURL = url;
    [self yssd_internalSetImageWithURL:url
                    placeholderImage:placeholder
                             options:options
                             context:context
                       setImageBlock:nil
                            progress:progressBlock
                           completed:^(NSImage * _Nullable image, NSData * _Nullable data, NSError * _Nullable error, YSSDImageCacheType cacheType, BOOL finished, NSURL * _Nullable imageURL) {
                               if (completedBlock) {
                                   completedBlock(image, error, cacheType, imageURL);
                               }
                           }];
}

#pragma mark - Alternate Image

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

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

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

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

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

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

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

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

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

#pragma mark - Cancel

- (void)yssd_cancelCurrentImageLoad {
    [self yssd_cancelImageLoadOperationWithKey:NSStringFromClass([self class])];
}

- (void)yssd_cancelCurrentAlternateImageLoad {
    [self yssd_cancelImageLoadOperationWithKey:YSSDAlternateImageOperationKey];
}

#pragma mar - Private

- (NSURL *)yssd_currentImageURL {
    return objc_getAssociatedObject(self, @selector(yssd_currentImageURL));
}

- (void)setYssd_currentImageURL:(NSURL *)yssd_currentImageURL {
    objc_setAssociatedObject(self, @selector(yssd_currentImageURL), yssd_currentImageURL, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}

- (NSURL *)yssd_currentAlternateImageURL {
    return objc_getAssociatedObject(self, @selector(yssd_currentAlternateImageURL));
}

- (void)setYssd_currentAlternateImageURL:(NSURL *)yssd_currentAlternateImageURL {
    objc_setAssociatedObject(self, @selector(yssd_currentAlternateImageURL), yssd_currentAlternateImageURL, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}

@end

#endif
