 //
 
 import UIKit
+//Global constants for user defaults
+internal let debugKey = "Debug"
+internal let screenKey = "ScreenNotifications"
 
-private let debugKey = "Debug"
-private let screenKey = "ScreenNotifications"
-
+//Protocol for alerting different parts of the app about a new notification.
 public protocol NewNotificationDelegate {
     func gotNewNotification(theNotification: Notification)
 }

    
    
    //MARK: Init
    
    init(){

        self.notifications = notificationArchiver.getNotifications()
        
    }
    
    //MARK: Functions
    public func fireNotification(theNotification: Notification) {
        
        if self.fireDebugNotifications == false && theNotification.debug == true {
            return
        }
        
        if self.fireScreenedNotifications == false && theNotification.screenedNotification == true {
            return
        }
        
        
        UIApplication.sharedApplication().applicationIconBadgeNumber = UIApplication.sharedApplication().applicationIconBadgeNumber + 1
        
        self.refreshTabBarItem()
        
        let notification = UILocalNotification()
        notification.applicationIconBadgeNumber = UIApplication.sharedApplication().applicationIconBadgeNumber
        notification.alertTitle = theNotification.text
        if theNotification.text != nil && theNotification.expandedText != nil {
            notification.alertBody = theNotification.text! + " - " + theNotification.expandedText!
        }else{
            notification.alertBody = theNotification.expandedText
        }
        notification.userInfo = theNotification.info
        UIApplication.sharedApplication().presentLocalNotificationNow(notification)
        
        self.notificationArchiver.saveNotification(theNotification)
        self.notifications.insert(theNotification, atIndex: 0)
        self.notificationDelegate?.gotNewNotification(theNotification)
    }
    
    public func popUpNotification(title: String, message: String?, viewThatWillPresentAlert view: UIViewController) {
        let alertController = UIAlertController(title: title, message: message, preferredStyle: UIAlertControllerStyle.Alert)
        
        let okAction = UIAlertAction(title: "OK", style: UIAlertActionStyle.Default, handler: nil)
        
        alertController.addAction(okAction)
        
        view.presentViewController(alertController, animated: true, completion: nil)
    }
    
    public func refreshTabBarItem() {
        if UIApplication.sharedApplication().applicationIconBadgeNumber == 0 {
            
            switch self.viewToBadge {
            case is UITabBarItem:
                guard let tabBar = self.viewToBadge as? UITabBarItem else{
                    return
                }
                
                tabBar.badgeValue = ""
            case is UIView:
//                guard let view = self.viewToBadge as? UIView else{
//                    return
//                }
                print("Is UIView")
            default:
                return
            }
            
            
            if self.viewToBadge is UITabBarItem {

            }

        }else{
            
            if self.viewToBadge is UITabBarItem {
                guard let tabBar = self.viewToBadge as? UITabBarItem else{
                    return
                }
                
                tabBar.badgeValue = "\(UIApplication.sharedApplication().applicationIconBadgeNumber)"
            }

        }
        
    }
    
    public func deleteAllNotifications() {
        self.notifications = self.notificationArchiver.removeAllNotifications()
    }

    public var notificationDelegate: NewNotificationDelegate?
    //Array Of notifications
    public var notifications = [Notification]()
    //Array of Tuples that keep a bagde view associated with a view that the uset wants badged. The user adds a new view that they wan badged throug the addViewToBadgeFunction.
    private var viewsToBadge: [(aView: UIView, aBadger: Badger)] = [(aView: UIView, aBadger: Badger)]()
    //Array of tab bars that the user wants badged
    public var tabBarsToBadge: [UITabBarItem] = [UITabBarItem]()
    //Variable to determine if we should fire debug notifications or not
    public var fireDebugNotifications = NSUserDefaults.standardUserDefaults().boolForKey(debugKey) {
        didSet {
