//
//  NavigationBar.swift
//  Kalker
//

import Foundation
import UIKit

func imageFromColor(color: UIColor, size: CGSize) -> UIImage {
    let rect = CGRect(x: 0, y: 0, width: size.width, height: size.height)
    UIGraphicsBeginImageContext(rect.size)
    let context = UIGraphicsGetCurrentContext()
    
    context!.setFillColor(color.cgColor)
    context!.fill(rect)
    
    var image = UIGraphicsGetImageFromCurrentImageContext()
    UIGraphicsEndImageContext()
    
    UIGraphicsBeginImageContext(size)
    image?.draw(in: rect)
    image = UIGraphicsGetImageFromCurrentImageContext()
    UIGraphicsEndImageContext()
    
    return image!
}

func setTransparentNavigationBar(navBar: UINavigationController) {
    let image=imageFromColor(color: UIColor(red: 0, green: 0, blue: 0, alpha: 0.2), size: CGSize(width: 1, height: 1))
    
    navBar.navigationBar.isTranslucent = true;
    navBar.navigationBar.setBackgroundImage(image, for: UIBarMetrics.default)
}

func setBlueNavigationBar(navBar: UINavigationController) {
    navBar.navigationBar.isTranslucent = true
    navBar.navigationBar.backgroundColor = UIColor.init(red: 10/255, green: 126/255, blue: 173/255, alpha: 1.0)
}

func profileButtonInNavigationBar(img: UIImage) -> UIButton {
    let button = UIButton.init(type: .custom)
    let profileImage = img
    
    let widthConstraint = button.widthAnchor.constraint(equalToConstant: 30)
    let heightConstraint = button.heightAnchor.constraint(equalToConstant: 30)
    heightConstraint.isActive = true
    widthConstraint.isActive = true
    
    button.setImage(profileImage, for: UIControlState.normal)
    button.frame = CGRect.init(x: 0, y: 0, width: 30, height: 30)
    button.layer.cornerRadius = button.frame.width / 2
    button.layer.masksToBounds = true
    
    return button
}

func buttonInNavigationBar(img: UIImage) -> UIButton {
    let button = UIButton.init(type: .custom)
    let profileImage = img
    
    let widthConstraint = button.widthAnchor.constraint(equalToConstant: 30)
    let heightConstraint = button.heightAnchor.constraint(equalToConstant: 30)
    heightConstraint.isActive = true
    widthConstraint.isActive = true
    
    button.setImage(profileImage, for: UIControlState.normal)
    button.frame = CGRect.init(x: 0, y: 0, width: 30, height: 30)
    
    return button
}
