//
//  NSArray+YSMASAdditions.m
//  
//
//  Created by Daniel Hammond on 11/26/13.
//
//

#import "NSArray+YSMASAdditions.h"
#import "View+YSMASAdditions.h"

@implementation NSArray (YSMASAdditions)

- (NSArray *)ysmas_makeConstraints:(void(^)(YSMASConstraintMaker *make))block {
    NSMutableArray *constraints = [NSMutableArray array];
    for (YSMAS_VIEW *view in self) {
        NSAssert([view isKindOfClass:[YSMAS_VIEW class]], @"All objects in the array must be views");
        [constraints addObjectsFromArray:[view ysmas_makeConstraints:block]];
    }
    return constraints;
}

- (NSArray *)ysmas_updateConstraints:(void(^)(YSMASConstraintMaker *make))block {
    NSMutableArray *constraints = [NSMutableArray array];
    for (YSMAS_VIEW *view in self) {
        NSAssert([view isKindOfClass:[YSMAS_VIEW class]], @"All objects in the array must be views");
        [constraints addObjectsFromArray:[view ysmas_updateConstraints:block]];
    }
    return constraints;
}

- (NSArray *)ysmas_remakeConstraints:(void(^)(YSMASConstraintMaker *make))block {
    NSMutableArray *constraints = [NSMutableArray array];
    for (YSMAS_VIEW *view in self) {
        NSAssert([view isKindOfClass:[YSMAS_VIEW class]], @"All objects in the array must be views");
        [constraints addObjectsFromArray:[view ysmas_remakeConstraints:block]];
    }
    return constraints;
}

- (void)ysmas_distributeViewsAlongAxis:(YSMASAxisType)axisType withFixedSpacing:(CGFloat)fixedSpacing leadSpacing:(CGFloat)leadSpacing tailSpacing:(CGFloat)tailSpacing {
    if (self.count < 2) {
        NSAssert(self.count>1,@"views to distribute need to bigger than one");
        return;
    }
    
    YSMAS_VIEW *tempSuperView = [self ysmas_commonSuperviewOfViews];
    if (axisType == YSMASAxisTypeHorizontal) {
        YSMAS_VIEW *prev;
        for (int i = 0; i < self.count; i++) {
            YSMAS_VIEW *v = self[i];
            [v ysmas_makeConstraints:^(YSMASConstraintMaker *make) {
                if (prev) {
                    make.width.equalTo(prev);
                    make.left.equalTo(prev.ysmas_right).offset(fixedSpacing);
                    if (i == self.count - 1) {//last one
                        make.right.equalTo(tempSuperView).offset(-tailSpacing);
                    }
                }
                else {//first one
                    make.left.equalTo(tempSuperView).offset(leadSpacing);
                }
                
            }];
            prev = v;
        }
    }
    else {
        YSMAS_VIEW *prev;
        for (int i = 0; i < self.count; i++) {
            YSMAS_VIEW *v = self[i];
            [v ysmas_makeConstraints:^(YSMASConstraintMaker *make) {
                if (prev) {
                    make.height.equalTo(prev);
                    make.top.equalTo(prev.ysmas_bottom).offset(fixedSpacing);
                    if (i == self.count - 1) {//last one
                        make.bottom.equalTo(tempSuperView).offset(-tailSpacing);
                    }                    
                }
                else {//first one
                    make.top.equalTo(tempSuperView).offset(leadSpacing);
                }
                
            }];
            prev = v;
        }
    }
}

- (void)ysmas_distributeViewsAlongAxis:(YSMASAxisType)axisType withFixedItemLength:(CGFloat)fixedItemLength leadSpacing:(CGFloat)leadSpacing tailSpacing:(CGFloat)tailSpacing {
    if (self.count < 2) {
        NSAssert(self.count>1,@"views to distribute need to bigger than one");
        return;
    }
    
    YSMAS_VIEW *tempSuperView = [self ysmas_commonSuperviewOfViews];
    if (axisType == YSMASAxisTypeHorizontal) {
        YSMAS_VIEW *prev;
        for (int i = 0; i < self.count; i++) {
            YSMAS_VIEW *v = self[i];
            [v ysmas_makeConstraints:^(YSMASConstraintMaker *make) {
                make.width.equalTo(@(fixedItemLength));
                if (prev) {
                    if (i == self.count - 1) {//last one
                        make.right.equalTo(tempSuperView).offset(-tailSpacing);
                    }
                    else {
                        CGFloat offset = (1-(i/((CGFloat)self.count-1)))*(fixedItemLength+leadSpacing)-i*tailSpacing/(((CGFloat)self.count-1));
                        make.right.equalTo(tempSuperView).multipliedBy(i/((CGFloat)self.count-1)).with.offset(offset);
                    }
                }
                else {//first one
                    make.left.equalTo(tempSuperView).offset(leadSpacing);
                }
            }];
            prev = v;
        }
    }
    else {
        YSMAS_VIEW *prev;
        for (int i = 0; i < self.count; i++) {
            YSMAS_VIEW *v = self[i];
            [v ysmas_makeConstraints:^(YSMASConstraintMaker *make) {
                make.height.equalTo(@(fixedItemLength));
                if (prev) {
                    if (i == self.count - 1) {//last one
                        make.bottom.equalTo(tempSuperView).offset(-tailSpacing);
                    }
                    else {
                        CGFloat offset = (1-(i/((CGFloat)self.count-1)))*(fixedItemLength+leadSpacing)-i*tailSpacing/(((CGFloat)self.count-1));
                        make.bottom.equalTo(tempSuperView).multipliedBy(i/((CGFloat)self.count-1)).with.offset(offset);
                    }
                }
                else {//first one
                    make.top.equalTo(tempSuperView).offset(leadSpacing);
                }
            }];
            prev = v;
        }
    }
}

- (YSMAS_VIEW *)ysmas_commonSuperviewOfViews
{
    YSMAS_VIEW *commonSuperview = nil;
    YSMAS_VIEW *previousView = nil;
    for (id object in self) {
        if ([object isKindOfClass:[YSMAS_VIEW class]]) {
            YSMAS_VIEW *view = (YSMAS_VIEW *)object;
            if (previousView) {
                commonSuperview = [view ysmas_closestCommonSuperview:commonSuperview];
            } else {
                commonSuperview = view;
            }
            previousView = view;
        }
    }
    NSAssert(commonSuperview, @"Can't constrain views that do not share a common superview. Make sure that all the views in this array have been added into the same view hierarchy.");
    return commonSuperview;
}

@end
