//
//  Autolayout.swift
//  Astro
//
//  Created by Jeremy Wiebe on 2015-04-16.
//  Copyright (c) 2015 Mobify Research & Development Inc. All rights reserved.
//

import Foundation

// This had to be pulled out of the UIView extension in order for the
// build to succeed in the "Release" build configuration.
public enum UIViewLayoutDirection {
    case up
    case down
}

public extension UIView {

    @objc func pinToSuperviewEdges() {
        pinToSuperviewEdgesHorizontally()
        pinToSuperviewEdgesVertically()
    }

    @objc func pinToSuperviewEdgesHorizontally(withMargin margin: Double = 0.0) {
        let margin = ["margin": margin]
        self.superview!.addConstraints(NSLayoutConstraint.constraints(withVisualFormat: "H:|-margin-[view]-margin-|", options: [], metrics: margin, views: ["view": self]))
    }

    @objc func pinToSuperviewEdgesVertically(withMargin margin: Double = 0.0) {
        let margin = ["margin": margin]
        self.superview!.addConstraints(NSLayoutConstraint.constraints(withVisualFormat: "V:|-margin-[view]-margin-|", options: [], metrics: margin, views: ["view": self]))
    }

    @objc func centerInSuperview() {
        self.superview!.addConstraint(NSLayoutConstraint(item: self, attribute: .centerX, relatedBy: .equal, toItem: self.superview!, attribute: .centerX, multiplier: 1.0, constant: 0))
        self.superview!.addConstraint(NSLayoutConstraint(item: self, attribute: .centerY, relatedBy: .equal, toItem: self.superview!, attribute: .centerY, multiplier: 1.0, constant: 0))
    }

    @objc func constrainToHeight(_ height: CGFloat) {
        self.addConstraint(NSLayoutConstraint(item: self, attribute: .height, relatedBy: .equal, toItem: nil, attribute: .notAnAttribute, multiplier: 1.0, constant: height))
    }

    @available(iOS 11.0, *)
    func constrainWithinSafeAreaOfSuperview() {
        constrainWithinSafeAreaOfSuperviewHorizontally()
        constrainWithinSafeAreaofSuperviewVertically()
    }

    @available(iOS 11.0, *)
    func constrainWithinSafeAreaOfSuperviewHorizontally() {
        self.superview?.safeAreaLayoutGuide.leadingAnchor.constraint(equalTo: self.leadingAnchor).isActive = true
        self.superview?.safeAreaLayoutGuide.trailingAnchor.constraint(equalTo: self.trailingAnchor).isActive = true
    }

    @available(iOS 11.0, *)
    func constrainWithinSafeAreaofSuperviewVertically() {
        self.superview?.safeAreaLayoutGuide.bottomAnchor.constraint(equalTo: self.bottomAnchor).isActive = true
        self.superview?.safeAreaLayoutGuide.topAnchor.constraint(equalTo: self.topAnchor).isActive = true
    }

    func findPositioningConstraint(inParent parentView: UIView, direction: UIViewLayoutDirection) -> NSLayoutConstraint? {

        let attribute: NSLayoutConstraint.Attribute = (direction == .down) ? .top : .bottom

        return parentView.constraints.first {
            return $0.matches(self, attribute: attribute)
        }
    }
}

extension NSLayoutConstraint {

    @objc func matches(_ view: UIView, attribute: NSLayoutConstraint.Attribute) -> Bool {
        if let firstItem = self.firstItem as? NSObject, firstItem == view, self.firstAttribute == attribute {
            return true
        }

        if let secondItem = self.secondItem as? NSObject, secondItem == view, self.secondAttribute == attribute {
            return true
        }

        return false
    }
}
