//
//  NSLayoutConstraint+MFConvenience.h
//  myverizon
//
//  Created by Chris Yang on 1/15/16.
//  Copyright © 2016 Verizon Wireless. All rights reserved.
//

#import <UIKit/UIKit.h>

//************************************************************************
//*
//* IMPORTANT::make sure add subView to superView before call the methods
//*
//************************************************************************

// constraint constants
extern NSString * _Nonnull const ConstraintTop;
extern NSString * _Nonnull const ConstraintBot;
extern NSString * _Nonnull const ConstraintLeading;
extern NSString * _Nonnull const ConstraintTrailing;
extern NSString * _Nonnull const ConstraintCenterX;
extern NSString * _Nonnull const ConstraintCenterY;
extern NSString * _Nonnull const ConstraintHeight;
extern NSString * _Nonnull const ConstraintWidth;

@interface NSLayoutConstraint (MFConvenience)

//this will pin subview to superview
+ (nullable NSDictionary *)constraintPinSubviewToSuperview:(nullable UIView *)subview;

+ (nullable NSDictionary *)constraintPinSubview:(nullable UIView *)subview pinTop:(BOOL)pinTop pinBottom:(BOOL)pinBottom pinLeft:(BOOL)pinLeft pinRight:(BOOL)pinRight;

+ (nullable NSDictionary *)constraintPinSubview:(nullable UIView *)subview topRelation:(NSLayoutRelation)topRelation bottomRelation:(NSLayoutRelation)bottomRelation leftRelation:(NSLayoutRelation)leftRelation rightRelation:(NSLayoutRelation)rightRelation;

+ (nullable NSDictionary *)constraintPinSubview:(nullable UIView *)subview pinTop:(BOOL)pinTop topConstant:(CGFloat)topConstant pinBottom:(BOOL)pinBottom bottomConstant:(CGFloat)bottomConstant pinLeft:(BOOL)pinLeft leftConstant:(CGFloat)leftConstant pinRight:(BOOL)pinRight rightConstant:(CGFloat)rightConstant;

+ (nullable NSDictionary *)constraintPinSubview:(nullable UIView *)subview pinTop:(BOOL)pinTop topConstant:(CGFloat)topConstant topRelation:(NSLayoutRelation)topRelation pinBottom:(BOOL)pinBottom bottomConstant:(CGFloat)bottomConstant bottomRelation:(NSLayoutRelation)bottomRelation pinLeft:(BOOL)pinLeft leftConstant:(CGFloat)leftConstant leftRelation:(NSLayoutRelation)leftRelation pinRight:(BOOL)pinRight rightConstant:(CGFloat)rightConstant rightRelation:(NSLayoutRelation)rightRelation;

// Pin subview with 1 side
+ (nullable NSDictionary *)constraintPinTopSubview:(nonnull UIView *)subview topConstant:(CGFloat)topConstant;
+ (nullable NSDictionary *)constraintPinBottomSubview:(nonnull UIView *)subview bottomConstant:(CGFloat)bottomConstant;
+ (nullable NSDictionary *)constraintPinLeftSubview:(nonnull UIView *)subview leftConstant:(CGFloat)leftConstant;
+ (nullable NSDictionary *)constraintPinRightSubview:(nonnull UIView *)subview rightConstant:(CGFloat)rightConstant;

//pin the subview to the horizontal and vertical center of super view
+ (nullable NSDictionary *)constraintPinSubviewToCenterOfSuperview:(nullable UIView *)subview;

+ (nullable NSDictionary *)constraintPinSubview:(nullable UIView *)subview pinCenterX:(BOOL)pinCenterX pinCenterY:(BOOL)pinCenterY;

+ (nullable NSDictionary *)constraintPinSubview:(nullable UIView *)subview pinCenterX:(BOOL)pinCenterX centerXConstant:(CGFloat)centerXConstant pinCenterY:(BOOL)pinCenterY centerYConstant:(CGFloat)centerYConstant;


//set height and width constraint for a view
+ (nullable NSDictionary *)constraintPinView:(nonnull UIView*)view heightConstraint:(BOOL)pinHeight heightConstant:(CGFloat)heightConstant widthConstraint:(BOOL)pinWidth widthConstant:(CGFloat)widthConstant;

// pins 2 views in same hierarchy
+(nonnull NSLayoutConstraint *)constraintPinFirstView :(nonnull UIView*)firstView toSecondView :(nonnull UIView*)secondView withConstant :(CGFloat)constant directionVertical :(BOOL)directionVertical;

+(nullable NSDictionary *)constraintAlignView :(nonnull UIView *)firstView toSecondView :(nonnull UIView *)secondView alignX :(BOOL)alignX alignY :(BOOL)alignY;

#pragma mark - With Margins

+ (nonnull NSDictionary <NSString *, NSLayoutConstraint *>*)pinViewToSuperview:(nonnull UIView *)subview useMargins:(BOOL)useMargins;
+ (nonnull NSDictionary <NSString *, NSLayoutConstraint *>*)pinViewToSuperview:(nonnull UIView *)subview useMargins:(BOOL)useMargins pinTop:(BOOL)pinTop pinBottom:(BOOL)pinBottom pinLeft:(BOOL)pinLeft pinRight:(BOOL)pinRight;
+ (nonnull NSDictionary <NSString *, NSLayoutConstraint *>*)pinViewToSuperview:(nonnull UIView *)subview useMargins:(BOOL)useMargins pinTop:(BOOL)pinTop topConstant:(CGFloat)topConstant pinBottom:(BOOL)pinBottom bottomConstant:(CGFloat)bottomConstant pinLeft:(BOOL)pinLeft leftConstant:(CGFloat)leftConstant pinRight:(BOOL)pinRight rightConstant:(CGFloat)rightConstant;
+ (nonnull NSLayoutConstraint *)pinViewTopToSuperview:(nonnull UIView *)view useMargins:(BOOL)useMargins constant:(CGFloat)constant;
+ (nonnull NSLayoutConstraint *)pinViewLeftToSuperview:(nonnull UIView *)view useMargins:(BOOL)useMargins constant:(CGFloat)constant;
+ (nonnull NSLayoutConstraint *)pinViewRightToSuperview:(nonnull UIView *)view useMargins:(BOOL)useMargins constant:(CGFloat)constant;
+ (nonnull NSLayoutConstraint *)pinViewBottomToSuperview:(nonnull UIView *)view useMargins:(BOOL)useMargins constant:(CGFloat)constant;

#pragma mark - Scaling constraints

// These functions will pin a view to its super view with space that is a ratio of the anchor (by default the super view's width if nothing is passed in). (So if the space is 24 and the super's width is 320 in the design, then the ratio should be 24/320 and this will be multiplied by the super's current width, so that the constraint always linearly scales).
+ (nullable NSDictionary *)scalingConstraintPinSubview:(nullable UIView *)subview pinTop:(BOOL)pinTop topConstant:(CGFloat)topConstant pinBottom:(BOOL)pinBottom bottomConstant:(CGFloat)bottomConstant pinLeft:(BOOL)pinLeft leftConstant:(CGFloat)leftConstant pinRight:(BOOL)pinRight rightConstant:(CGFloat)rightConstant baseConstant:(CGFloat)baseConstant;
+ (nullable NSDictionary *)scalingConstraintPinSubview:(nullable UIView *)subview pinTop:(BOOL)pinTop topConstant:(CGFloat)topConstant pinBottom:(BOOL)pinBottom bottomConstant:(CGFloat)bottomConstant pinLeft:(BOOL)pinLeft leftConstant:(CGFloat)leftConstant pinRight:(BOOL)pinRight rightConstant:(CGFloat)rightConstant baseConstant:(CGFloat)baseConstant anchor:(nullable NSLayoutDimension *)anchor;

// More Direct helpers
+ (nonnull NSLayoutConstraint *)scalingPinViewLeftToSuper:(nonnull UIView *)view ratio:(CGFloat)ratio anchor:(nullable NSLayoutDimension *)anchor;
+ (nonnull NSLayoutConstraint *)scalingPinViewRightToSuper:(nonnull UIView *)view ratio:(CGFloat)ratio anchor:(nullable NSLayoutDimension *)anchor;
+ (nonnull NSLayoutConstraint *)scalingPinViewTopToSuper:(nonnull UIView *)view ratio:(CGFloat)ratio anchor:(nullable NSLayoutDimension *)anchor;
+ (nonnull NSLayoutConstraint *)scalingPinViewBottomToSuper:(nonnull UIView *)view ratio:(CGFloat)ratio anchor:(nullable NSLayoutDimension *)anchor;

// Pins one view to another. Smart enough to handle pinning a view to its super or a view to another view with the same super. Pass in the direction and relationship for the pinning. Ratio works the same as the above functions. Can optionally also pass in an anchor if you do not wish to use the super's width as an anchor.
+ (nonnull NSLayoutConstraint *)scalingConstraintPinView:(nonnull UIView *)view toView:(nonnull UIView *)toView layoutAxis:(UILayoutConstraintAxis)layoutAxis relation:(NSLayoutRelation)relation ratio:(CGFloat)ratio anchor:(nullable NSLayoutDimension *)anchor;

@end
