//
//  CGPoint+Utils.swift
//  SBrowser
//
//  Created by James Magahern on 7/23/20.
//

import Foundation

extension CGRect
{
    var center: CGPoint {
        get {
            return CGPoint(x: size.width / 2.0, y: size.height / 2.0)
        }
    }
    
    public func avoiding(verticalInsets insets: UIEdgeInsets) -> CGRect {
        var rect = self
        rect.origin.y += insets.top
        rect.size.height -= insets.top
        
        return rect
    }
    
    public func subtracting(width: CGFloat, height: CGFloat) -> CGRect {
        var rect = self
        rect.size.width -= width
        rect.size.height -= height
        
        return rect
    }
    
    public func centeredY(inRect: CGRect) -> CGRect {
        var rect = self
        rect.origin.y = (inRect.origin.y + (inRect.height - rect.height) / 2.0).rounded()
        
        return rect
    }
    
    public func centeredX(inRect: CGRect) -> CGRect {
        var rect = self
        rect.origin.x = (inRect.origin.x + (inRect.width - rect.width) / 2.0).rounded()
        
        return rect
    }
    
    public func centered(inRect: CGRect) -> CGRect {
        self.centeredX(inRect: inRect).centeredY(inRect: inRect)
    }
}

extension CGSize
{
    public func extendingBy(dw: CGFloat, dh: CGFloat) -> CGSize {
        var ourSize = self
        ourSize.width  += dw
        ourSize.height += dh
        
        return ourSize
    }
}
