//
//  MFSizeThreshold.m
//  mobilefirst
//
//  Created by Scott Pfeil on 1/31/17.
//  Copyright © 2017 Verizon Wireless Inc. All rights reserved.
//

#import "MFSizeThreshold.h"
#import <MVMCoreUI/MFSizeObject.h>

@implementation MFSizeThreshold

- (CGFloat)getValueForSizeScalingIfNeeded:(CGFloat)size {
    if (self.scale) {
        return [MFSizeObject getScaledValue:self.size forSize:size fromBase:self.scaleBaseValue];
    } else {
        return self.size;
    }
}

- (NSString *)description {
    NSString *descriptionString = [NSString stringWithFormat:@"Threshold: %f Size: %f Compare Direction: %li Block Set: %i", self.threshold, self.size, (long)self.direction, self.thresholdBlock == NULL];
    return descriptionString;
    
}

#pragma mark - Size Initializers

- (instancetype)initWithSize:(CGFloat)size forThreshold:(CGFloat)threshold compareDirection:(MFSizeCompareDirection)compareDirection {
    return [self initWithSize:size forThreshold:threshold compareDirection:compareDirection scaleBasedOnScreenSize:NO];
}

- (instancetype)initWithSize:(CGFloat)size forThreshold:(CGFloat)threshold compareDirection:(MFSizeCompareDirection)compareDirection scaleBasedOnScreenSize:(BOOL)scaleBasedOnScreenSize {
    if (self = [super init]) {
        self.size = size;
        self.threshold = threshold;
        self.direction = compareDirection;
        self.scale = scaleBasedOnScreenSize;
    }
    return self;
}

#pragma mark - Block Initializers

- (nullable instancetype)initWithThreshold:(CGFloat)threshold compareDirection:(MFSizeCompareDirection)compareDirection block:(nonnull void (^)(void))thresholdBlock {
    return [self initWithThreshold:threshold compareDirection:compareDirection scaleBasedOnScreenSize:NO block:thresholdBlock];
}

- (nullable instancetype)initWithThreshold:(CGFloat)threshold compareDirection:(MFSizeCompareDirection)compareDirection scaleBasedOnScreenSize:(BOOL)scaleBasedOnScreenSize block:(nonnull void (^)(void))thresholdBlock {
    if (self = [super init]) {
        self.threshold = threshold;
        self.direction = compareDirection;
        self.scale = scaleBasedOnScreenSize;
        self.thresholdBlock = thresholdBlock;
    }
    return self;
}

#pragma mark - Common Size Getters

+ (nullable instancetype)largerThanCustomSize:(CGFloat)size forThreshold:(CGFloat)threshold {
    MFSizeThreshold *sizeThreshold = [[MFSizeThreshold alloc] initWithSize:size forThreshold:threshold compareDirection:MFSizeGreaterThan];
    return sizeThreshold;
}

+ (nullable instancetype)smallerThanCustomSize:(CGFloat)size forThreshold:(CGFloat)threshold {
    MFSizeThreshold *sizeThreshold = [[MFSizeThreshold alloc] initWithSize:size forThreshold:threshold compareDirection:MFSizeLessThan];
    return sizeThreshold;
}

+ (nullable instancetype)scalingLargerThanCustomSize:(CGFloat)size forThreshold:(CGFloat)threshold {
    MFSizeThreshold *sizeThreshold = [[MFSizeThreshold alloc] initWithSize:size forThreshold:threshold compareDirection:MFSizeGreaterThan scaleBasedOnScreenSize:YES];
    return sizeThreshold;
}

+ (nullable instancetype)scalingSmallerThanCustomSize:(CGFloat)size forThreshold:(CGFloat)threshold {
    MFSizeThreshold *sizeThreshold = [[MFSizeThreshold alloc] initWithSize:size forThreshold:threshold compareDirection:MFSizeLessThan scaleBasedOnScreenSize:YES];
    return sizeThreshold;
}

#pragma mark - Common Block Getters

// Convenience functions to return common threshold pairings.
+ (nullable instancetype)largerThanCustomThreshold:(CGFloat)threshold block:(nonnull void (^)(void))thresholdBlock {
    MFSizeThreshold *sizeThreshold = [[MFSizeThreshold alloc] initWithThreshold:threshold compareDirection:MFSizeGreaterThan block:thresholdBlock];
    return sizeThreshold;
}

+ (nullable instancetype)smallerThanCustomThreshold:(CGFloat)threshold block:(nonnull void (^)(void))thresholdBlock {
    MFSizeThreshold *sizeThreshold = [[MFSizeThreshold alloc] initWithThreshold:threshold compareDirection:MFSizeLessThan block:thresholdBlock];
    return sizeThreshold;
}

+ (nullable instancetype)scalingLargerThanCustomThreshold:(CGFloat)threshold block:(nonnull void (^)(void))thresholdBlock {
    MFSizeThreshold *sizeThreshold = [[MFSizeThreshold alloc] initWithThreshold:threshold compareDirection:MFSizeGreaterThan scaleBasedOnScreenSize:YES block:thresholdBlock];
    return sizeThreshold;
}

+ (nullable instancetype)scalingSmallerThanCustomThreshold:(CGFloat)threshold block:(nonnull void (^)(void))thresholdBlock {
    MFSizeThreshold *sizeThreshold = [[MFSizeThreshold alloc] initWithThreshold:threshold compareDirection:MFSizeLessThan scaleBasedOnScreenSize:YES block:thresholdBlock];
    return sizeThreshold;
}

@end
