 
 protocol AlarmApplicationDelegate
 {
-    //typealias Weekday
+
     func playAlarmSound()
-    //something wrong with typealias, use Int instead
-    func setNotificationWithDate(date: NSDate, onWeekdaysForNotify:[Int]?)
-    func setupNotificationSettings()
-    
-    
+   
 }
 
 @UIApplicationMain

        audioPlayer!.play()
    }
    
    func setupNotificationSettings() {
        let notificationSettings: UIUserNotificationSettings! = UIApplication.sharedApplication().currentUserNotificationSettings()
        
        //if (notificationSettings.types == UIUserNotificationType.None){
            // Specify the notification types.
            var notificationTypes: UIUserNotificationType = UIUserNotificationType.Alert | UIUserNotificationType.Sound
            
            
            // Specify the notification actions.
            var stopAction = UIMutableUserNotificationAction()
            stopAction.identifier = "myStop"
            stopAction.title = "OK"
            stopAction.activationMode = UIUserNotificationActivationMode.Background
            stopAction.destructive = false
            stopAction.authenticationRequired = false
            
            var snoozeAction = UIMutableUserNotificationAction()
            snoozeAction.identifier = "mySnooze"
            snoozeAction.title = "Snooze"
            snoozeAction.activationMode = UIUserNotificationActivationMode.Background
            snoozeAction.destructive = false
            snoozeAction.authenticationRequired = false
        
            
            let actionsArray = [UIUserNotificationAction](arrayLiteral: stopAction, snoozeAction)
            let actionsArrayMinimal = [UIUserNotificationAction](arrayLiteral: snoozeAction, stopAction)
            // Specify the category related to the above actions.
            var alarmCategory = UIMutableUserNotificationCategory()
            alarmCategory.identifier = "myAlarmCategory"
            alarmCategory.setActions(actionsArray, forContext: .Default)
            alarmCategory.setActions(actionsArrayMinimal, forContext: .Minimal)
            
            
            let categoriesForSettings = Set(arrayLiteral: alarmCategory)
            
            
            // Register the notification settings.
            let newNotificationSettings = UIUserNotificationSettings(forTypes: notificationTypes, categories: categoriesForSettings)
            UIApplication.sharedApplication().registerUserNotificationSettings(newNotificationSettings)
        //}
    }
    
    private func correctDate(date: NSDate, onWeekdaysForNotify weekdays:[Int]?) -> [NSDate]
    {
        let calendar = NSCalendar(identifier: NSCalendarIdentifierGregorian)!
        let now = NSDate()
        var correctedDate: [NSDate] = [NSDate]()
        let flags = NSCalendarUnit.CalendarUnitEra|NSCalendarUnit.CalendarUnitYear|NSCalendarUnit.CalendarUnitWeekday|NSCalendarUnit.CalendarUnitWeekdayOrdinal | NSCalendarUnit.CalendarUnitTimeZone | NSCalendarUnit.CalendarUnitWeekOfMonth | NSCalendarUnit.CalendarUnitWeekOfYear
        var dateComponents = calendar.components(flags, fromDate: date)
        var nowComponents = calendar.components(flags, fromDate: now)
        var weekday:Int!
        if date.compare(now) == NSComparisonResult.OrderedAscending
        {
            nowComponents.hour = dateComponents.hour
            nowComponents.minute = dateComponents.minute
            nowComponents.second = 0
            weekday = nowComponents.weekday
            correctedDate.append(calendar.dateFromComponents(nowComponents)!)
        }
        else
        {
            weekday = dateComponents.weekday
            correctedDate.append(calendar.dateFromComponents(dateComponents)!)
        }
        if weekdays == nil{
            return correctedDate
        }
        else
        {
            let daysInWeek = 7
            correctedDate.removeAll(keepCapacity: true)
            for wd in weekdays!
            {
                
                var wdDate: NSDate!
                if date.compare(now) == NSComparisonResult.OrderedAscending
                {
                    
                    wdDate =  calendar.dateByAddingUnit(NSCalendarUnit.CalendarUnitDay, value: wd+daysInWeek-weekday, toDate: date, options:.MatchStrictly)!
                    
                }
                else
                {
                    wdDate =  calendar.dateByAddingUnit(NSCalendarUnit.CalendarUnitDay, value: wd-weekday, toDate: date, options:.MatchStrictly)!
                }
                
                correctedDate.append(wdDate)
            }
            return correctedDate
        }
    }
    
    func setNotificationWithDate(date: NSDate, onWeekdaysForNotify weekdays:[Int]?) {
        let AlarmNotification: UILocalNotification = UILocalNotification()
        let calendar = NSCalendar(identifier: NSCalendarIdentifierGregorian)!
        AlarmNotification.alertBody = "Wake Up!"
        AlarmNotification.alertAction = "Open App"
        AlarmNotification.category = "myAlarmCategory"
        //AlarmNotification.applicationIconBadgeNumber = 0
        //AlarmNotification.repeatCalendar = calendar
        //TODO, not working
        //AlarmNotification.repeatInterval = NSCalendarUnit.CalendarUnitWeekOfYear
        AlarmNotification.soundName = "bell.mp3"
        AlarmNotification.timeZone = NSTimeZone.defaultTimeZone()
        
        let datesForNotification = correctDate(date, onWeekdaysForNotify:weekdays)
        for d in datesForNotification
        {
            AlarmNotification.fireDate = d
            UIApplication.sharedApplication().scheduleLocalNotification(AlarmNotification)
        }
        
    }
    
    
    
    


    var window: UIWindow?
    var audioPlayer: AVAudioPlayer?
    var alarmScheduler: AlarmSchedulerDelegate = Scheduler()


    func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
