//
//  NSLayoutConstraintExtension.swift
//  MVMCoreUI
//
//  Created by Scott Pfeil on 8/28/19.
//  Copyright © 2019 Verizon Wireless. All rights reserved.
//


public extension NSLayoutConstraint {
    
    /// Pins the views vertically to the top and bottom anchor, allowing the super to expand depending on the tallest view. Shorter views are aligned center.
    static func pinViewsVerticalExpandableAlignCenter(_ views: [UIView], topAnchor: NSLayoutYAxisAnchor? = nil, bottomAnchor: NSLayoutYAxisAnchor? = nil, constant: CGFloat = 0) {
        for view in views {
            guard let superView = view.superview else { return }
            let top = topAnchor ?? superView.layoutMarginsGuide.topAnchor
            let bottom = bottomAnchor ?? superView.layoutMarginsGuide.bottomAnchor
            view.centerYAnchor.constraint(equalTo: superView.centerYAnchor).isActive = true
            view.topAnchor.constraint(greaterThanOrEqualTo: top).isActive = true
            bottom.constraint(greaterThanOrEqualTo: view.bottomAnchor).isActive = true
        
            var constraint = view.topAnchor.constraint(equalTo: top)
            constraint.priority = .defaultLow
            constraint.isActive = true
        
            constraint = bottom.constraint(equalTo: view.bottomAnchor)
            constraint.priority = .defaultLow
            constraint.isActive = true
        }
    }
    
    /// Pins the views vertically to the top and bottom anchor, allowing the super to expand depending on the tallest view. Shorter views are aligned top.
    static func pinViewsVerticalExpandableAlignTop(_ views: [UIView], topAnchor: NSLayoutYAxisAnchor? = nil, bottomAnchor: NSLayoutYAxisAnchor? = nil, constant: CGFloat = 0) {
        for view in views {
            guard let superView = view.superview else { return }
            
            let top = topAnchor ?? superView.layoutMarginsGuide.topAnchor
            let bottom = bottomAnchor ?? superView.layoutMarginsGuide.bottomAnchor
            view.topAnchor.constraint(equalTo: top).isActive = true
            bottom.constraint(greaterThanOrEqualTo: view.bottomAnchor, constant: constant).isActive = true
        
            let constraint = bottom.constraint(equalTo: view.bottomAnchor)
            constraint.priority = .defaultLow
            constraint.isActive = true
        }
    }
    
    /// Pins a view to the left and a view to the right, flexible space in between. The super can expand depending on the taller view. Shorter views are aligned top if alignTop true, else aligned center.
    static func pinViews(leftView: UIView, rightView: UIView, alignTop: Bool, topAnchor: NSLayoutYAxisAnchor? = nil, bottomAnchor: NSLayoutYAxisAnchor? = nil, constant: CGFloat = 0) {
        guard let superView = leftView.superview else { return }
        
        if alignTop {
            pinViewsVerticalExpandableAlignTop([leftView, rightView], topAnchor: topAnchor, bottomAnchor: bottomAnchor, constant: constant)
        } else {
            pinViewsVerticalExpandableAlignCenter([leftView, rightView], topAnchor: topAnchor, bottomAnchor: bottomAnchor, constant: constant)
        }
        
        leftView.leadingAnchor.constraint(equalTo: superView.layoutMarginsGuide.leadingAnchor).isActive = true
        superView.layoutMarginsGuide.trailingAnchor.constraint(equalTo: rightView.trailingAnchor).isActive = true
        rightView.leftAnchor.constraint(greaterThanOrEqualTo: leftView.rightAnchor, constant: Padding.Component.gutterForApplicationWidth).isActive = true
    }
}
