/*
 * 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 "YSSDWebImageTransition.h"

#if YSSD_UIKIT || YSSD_MAC

#if YSSD_MAC
#import "YSSDWebImageTransitionInternal.h"
#import "YSSDInternalMacros.h"

CAMediaTimingFunction * YSSDTimingFunctionFromAnimationOptions(YSSDWebImageAnimationOptions options) {
    if (YSSD_OPTIONS_CONTAINS(YSSDWebImageAnimationOptionCurveLinear, options)) {
        return [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionLinear];
    } else if (YSSD_OPTIONS_CONTAINS(YSSDWebImageAnimationOptionCurveEaseIn, options)) {
        return [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseIn];
    } else if (YSSD_OPTIONS_CONTAINS(YSSDWebImageAnimationOptionCurveEaseOut, options)) {
        return [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseOut];
    } else if (YSSD_OPTIONS_CONTAINS(YSSDWebImageAnimationOptionCurveEaseInOut, options)) {
        return [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
    } else {
        return [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionDefault];
    }
}

CATransition * YSSDTransitionFromAnimationOptions(YSSDWebImageAnimationOptions options) {
    if (YSSD_OPTIONS_CONTAINS(options, YSSDWebImageAnimationOptionTransitionCrossDissolve)) {
        CATransition *trans = [CATransition animation];
        trans.type = kCATransitionFade;
        return trans;
    } else if (YSSD_OPTIONS_CONTAINS(options, YSSDWebImageAnimationOptionTransitionFlipFromLeft)) {
        CATransition *trans = [CATransition animation];
        trans.type = kCATransitionPush;
        trans.subtype = kCATransitionFromLeft;
        return trans;
    } else if (YSSD_OPTIONS_CONTAINS(options, YSSDWebImageAnimationOptionTransitionFlipFromRight)) {
        CATransition *trans = [CATransition animation];
        trans.type = kCATransitionPush;
        trans.subtype = kCATransitionFromRight;
        return trans;
    } else if (YSSD_OPTIONS_CONTAINS(options, YSSDWebImageAnimationOptionTransitionFlipFromTop)) {
        CATransition *trans = [CATransition animation];
        trans.type = kCATransitionPush;
        trans.subtype = kCATransitionFromTop;
        return trans;
    } else if (YSSD_OPTIONS_CONTAINS(options, YSSDWebImageAnimationOptionTransitionFlipFromBottom)) {
        CATransition *trans = [CATransition animation];
        trans.type = kCATransitionPush;
        trans.subtype = kCATransitionFromBottom;
        return trans;
    } else if (YSSD_OPTIONS_CONTAINS(options, YSSDWebImageAnimationOptionTransitionCurlUp)) {
        CATransition *trans = [CATransition animation];
        trans.type = kCATransitionReveal;
        trans.subtype = kCATransitionFromTop;
        return trans;
    } else if (YSSD_OPTIONS_CONTAINS(options, YSSDWebImageAnimationOptionTransitionCurlDown)) {
        CATransition *trans = [CATransition animation];
        trans.type = kCATransitionReveal;
        trans.subtype = kCATransitionFromBottom;
        return trans;
    } else {
        return nil;
    }
}
#endif

@implementation YSSDWebImageTransition

- (instancetype)init {
    self = [super init];
    if (self) {
        self.duration = 0.5;
    }
    return self;
}

@end

@implementation YSSDWebImageTransition (Conveniences)

+ (YSSDWebImageTransition *)fadeTransition {
    return [self fadeTransitionWithDuration:0.5];
}

+ (YSSDWebImageTransition *)fadeTransitionWithDuration:(NSTimeInterval)duration {
    YSSDWebImageTransition *transition = [YSSDWebImageTransition new];
#if YSSD_UIKIT
    transition.animationOptions = UIViewAnimationOptionTransitionCrossDissolve | UIViewAnimationOptionAllowUserInteraction;
#else
    transition.animationOptions = YSSDWebImageAnimationOptionTransitionCrossDissolve;
#endif
    return transition;
}

+ (YSSDWebImageTransition *)flipFromLeftTransition {
    return [self flipFromLeftTransitionWithDuration:0.5];
}

+ (YSSDWebImageTransition *)flipFromLeftTransitionWithDuration:(NSTimeInterval)duration {
    YSSDWebImageTransition *transition = [YSSDWebImageTransition new];
#if YSSD_UIKIT
    transition.animationOptions = UIViewAnimationOptionTransitionFlipFromLeft | UIViewAnimationOptionAllowUserInteraction;
#else
    transition.animationOptions = YSSDWebImageAnimationOptionTransitionFlipFromLeft;
#endif
    return transition;
}

+ (YSSDWebImageTransition *)flipFromRightTransition {
    return [self flipFromRightTransitionWithDuration:0.5];
}

+ (YSSDWebImageTransition *)flipFromRightTransitionWithDuration:(NSTimeInterval)duration {
    YSSDWebImageTransition *transition = [YSSDWebImageTransition new];
#if YSSD_UIKIT
    transition.animationOptions = UIViewAnimationOptionTransitionFlipFromRight | UIViewAnimationOptionAllowUserInteraction;
#else
    transition.animationOptions = YSSDWebImageAnimationOptionTransitionFlipFromRight;
#endif
    return transition;
}

+ (YSSDWebImageTransition *)flipFromTopTransition {
    return [self flipFromTopTransitionWithDuration:0.5];
}

+ (YSSDWebImageTransition *)flipFromTopTransitionWithDuration:(NSTimeInterval)duration {
    YSSDWebImageTransition *transition = [YSSDWebImageTransition new];
#if YSSD_UIKIT
    transition.animationOptions = UIViewAnimationOptionTransitionFlipFromTop | UIViewAnimationOptionAllowUserInteraction;
#else
    transition.animationOptions = YSSDWebImageAnimationOptionTransitionFlipFromTop;
#endif
    return transition;
}

+ (YSSDWebImageTransition *)flipFromBottomTransition {
    return [self flipFromBottomTransitionWithDuration:0.5];
}

+ (YSSDWebImageTransition *)flipFromBottomTransitionWithDuration:(NSTimeInterval)duration {
    YSSDWebImageTransition *transition = [YSSDWebImageTransition new];
#if YSSD_UIKIT
    transition.animationOptions = UIViewAnimationOptionTransitionFlipFromBottom | UIViewAnimationOptionAllowUserInteraction;
#else
    transition.animationOptions = YSSDWebImageAnimationOptionTransitionFlipFromBottom;
#endif
    return transition;
}

+ (YSSDWebImageTransition *)curlUpTransition {
    return [self curlUpTransitionWithDuration:0.5];
}

+ (YSSDWebImageTransition *)curlUpTransitionWithDuration:(NSTimeInterval)duration {
    YSSDWebImageTransition *transition = [YSSDWebImageTransition new];
#if YSSD_UIKIT
    transition.animationOptions = UIViewAnimationOptionTransitionCurlUp | UIViewAnimationOptionAllowUserInteraction;
#else
    transition.animationOptions = YSSDWebImageAnimationOptionTransitionCurlUp;
#endif
    return transition;
}

+ (YSSDWebImageTransition *)curlDownTransition {
    return [self curlDownTransitionWithDuration:0.5];
}

+ (YSSDWebImageTransition *)curlDownTransitionWithDuration:(NSTimeInterval)duration {
    YSSDWebImageTransition *transition = [YSSDWebImageTransition new];
#if YSSD_UIKIT
    transition.animationOptions = UIViewAnimationOptionTransitionCurlDown | UIViewAnimationOptionAllowUserInteraction;
#else
    transition.animationOptions = YSSDWebImageAnimationOptionTransitionCurlDown;
#endif
    transition.duration = duration;
    return transition;
}

@end

#endif
