//
//  NavigationController.swift


import Foundation
import UIKit

extension UINavigationBar {
    
    func addShadow()  {
        self.layer.shadowOffset = CGSize(width: 0, height: 1.5)
        self.layer.shadowRadius = 1.5
        self.layer.shadowColor = UIColor.black.cgColor
        self.layer.shadowOpacity = 0.2
    }
    
}

extension CAShapeLayer {
    func drawCircleAtLocation(location: CGPoint, withRadius radius: CGFloat, andColor color: UIColor, filled: Bool) {
        fillColor = filled ? color.cgColor : UIColor.white.cgColor
        strokeColor = color.cgColor
        let origin = CGPoint(x: location.x - radius, y: location.y - radius)
        path = UIBezierPath(ovalIn: CGRect(origin: origin, size: CGSize(width: radius * 2, height: radius * 2))).cgPath
    }
}

private var handle: UInt8 = 0

extension UIBarButtonItem{
    
    func setHeaderButtonItem() -> Void {
        
        self.tintColor = UIColor.white
        self.setTitleTextAttributes([ NSAttributedString.Key.font: Font.HelveticaNeue_Medium(fontSize: 16)], for: .normal)

    }
    
    private var badgeLayer: CAShapeLayer? {
        if let b: AnyObject = objc_getAssociatedObject(self, &handle) as AnyObject? {
            return b as? CAShapeLayer
        } else {
            return nil
        }
    }
    
    func addBadge(number: Int, withOffset offset: CGPoint = CGPoint.zero, andColor color: UIColor = UIColor.red, andFilled filled: Bool = true) {
        
        guard let view = self.value(forKey: "view") as? UIView else { return }
        
        badgeLayer?.removeFromSuperlayer()
        
        // Initialize Badge
        let badge = CAShapeLayer()
        let radius = CGFloat(7)
        let location = CGPoint(x: view.frame.width - (radius + offset.x), y: (radius + offset.y))
        badge.drawCircleAtLocation(location: location, withRadius: radius, andColor: color, filled: filled)
        view.layer.addSublayer(badge)
        
        // Initialiaze Badge's label
        let label = CATextLayer()
        label.string = "\(number)"
        label.alignmentMode = .center
        label.fontSize = 11
        label.frame = CGRect(origin: CGPoint(x: location.x - 4, y: offset.y), size: CGSize(width: 8, height: 16))
        label.foregroundColor = filled ? UIColor.white.cgColor : color.cgColor
        label.backgroundColor = UIColor.clear.cgColor
        label.contentsScale = UIScreen.main.scale
        badge.addSublayer(label)
        
        // Save Badge as UIBarButtonItem property
        objc_setAssociatedObject(self, &handle, badge, .OBJC_ASSOCIATION_RETAIN_NONATOMIC)
    }
    
    func updateBadge(number: Int) {
        if let text = badgeLayer?.sublayers?.filter({ $0 is CATextLayer }).first as? CATextLayer {
            text.string = "\(number)"
        }
    }
    
    func removeBadge() {
        badgeLayer?.removeFromSuperlayer()
    }
}

extension UINavigationController{
    
    func setTitleImage() {
       
        let imageView = UIImageView(image: #imageLiteral(resourceName: "LogoGray").imageWithInsets(insetDimen: 10))
        imageView.contentMode = .scaleAspectFit
        imageView.frame = CGRect(x: 0, y: 0, width: 120, height: 36)
        self.navigationBar.topItem?.titleView = imageView
        
    }
    func setBarWhiteColorForTableScroll() {
       // self.navigationBar.addShadow()
        self.navigationBar.setBackgroundImage(#imageLiteral(resourceName: "header_view"), for: UIBarMetrics.default)
        self.navigationBar.shadowImage = UIImage()
        self.navigationBar.barTintColor = UIColor.hexStringToUIColor(hex: "#016d9f")
        self.navigationBar.titleTextAttributes = [NSAttributedString.Key.foregroundColor : LWColor.AppThemColor,NSAttributedString.Key.font: Font.HelveticaNeue_Bold(fontSize: 18)]
    }

    func clearNavigationBar() {
        self.navigationBar.setBackgroundImage(UIImage(), for: UIBarMetrics.default)
        self.navigationBar.shadowImage = UIImage()
        self.navigationBar.isTranslucent = true
        self.navigationBar.barTintColor = UIColor.clear
        self.view.backgroundColor = UIColor.clear
        self.navigationBar.titleTextAttributes = [NSAttributedString.Key.foregroundColor : UIColor.white]
        self.navigationBar.titleTextAttributes = [NSAttributedString.Key.foregroundColor : LWColor.AppThemColor,NSAttributedString.Key.font: Font.HelveticaNeue_Bold(fontSize: 18)]
    }
    
    func setTransParentNavigation() {
        self.navigationBar.barTintColor = UIColor.clear
        self.navigationBar.setBackgroundImage(#imageLiteral(resourceName: "OverLayTopDown"), for: UIBarMetrics.default)
        self.navigationBar.isTranslucent = false
        self.view.backgroundColor = UIColor.clear
    }
    
    open override var supportedInterfaceOrientations: UIInterfaceOrientationMask {
        return UIInterfaceOrientationMask.portrait
    }

}
