<commit_before>//
//  Rooster.swift
//  Shifty
//
//  Created by <NAME> on 04/06/15.
//  Copyright (c) 2015 <NAME>. All rights reserved.
//

import Parse
import SwiftDate
import Foundation

class Rooster
{
    // constant for duration of fixed schedule
    let amountOfRecurringWeeks = 8

    var ownedShifts = [[Shift]]()
    var suppliedShifts = [[Shift]]()
    var requestedShifs = [[Request]]()
    
    let helper = Helper()
    
    func addRecurringShift(day: String, hour: Int, minute: Int)
    {
        let dayDict = ["Maandag": 2, "Dinsdag": 3, "Woensdag": 4, "Donderdag": 5, "Vrijdag": 6, "Zaterdag": 7, "Zondag": 1]
        
        var firstOccurrenceDate = nextOccurenceOfDay(dayDict[day]!).set(componentsDict: ["hour": hour, "minute": minute])
        
        for week in 0..<amountOfRecurringWeeks
        {
            let shift = PFObject(className: "Shifts")
            
            shift["Date"] = firstOccurrenceDate! + (7 * week).day
            shift["Status"] = "idle"
            shift["Owner"] = PFUser.currentUser()
            shift.saveInBackground()
        }
        
    }
    
    // function that calculates on which date the next occurence is of a given weekday<commit_msg>Check for double entries in fixed shifts<commit_after>//
//  Rooster.swift
//  Shifty
//
//  Created by <NAME> on 04/06/15.
//  Copyright (c) 2015 <NAME>. All rights reserved.
//

import Parse
import SwiftDate
import Foundation

class Rooster
{
    // constant for duration of fixed schedule
    let amountOfRecurringWeeks = 8

    var ownedShifts = [[Shift]]()
    var suppliedShifts = [[Shift]]()
    var requestedShifs = [[Request]]()
    
    let helper = Helper()
    
    func addRecurringShift(day: String, hour: Int, minute: Int)
    {
        checkDoubleEntries(day) { noDoubleEntries -> Void in
            if noDoubleEntries
            {
                let shift = PFObject(className: "FixedShifts")
                
                shift["Day"] = day
                shift["Hour"] = hour
                shift["Minute"] = minute
                shift["Owner"] = PFUser.currentUser()
                shift.saveInBackgroundWithBlock() { (succes: Bool, error: NSError?) -> Void in
                    if succes
                    {
                        self.generateRecurringShifts(shift)
                    }
                }
            }
        }
    }
    
    func checkDoubleEntries(day: String, callback: (noDoubleEntries: Bool) -> Void)
    {
        let query = PFQuery(className: "FixedShifts")
        query.whereKey("Owner", equalTo: PFUser.currentUser()!)
        query.findObjectsInBackgroundWithBlock() { (objects: [AnyObject]?, error: NSError?) -> Void in
            if let ownedShifts = self.helper.returnObjectAfterErrorCheck(objects, error: error) as? [PFObject]
            {
                for shift in ownedShifts
                {
                    if shift["Day"] as! String == day
                    {
                        callback(noDoubleEntries: false)
                        return
                    }
                }
                callback(noDoubleEntries: true)
            }
        }
    }
    
    func generateRecurringShifts(fixedShift: PFObject)
    {
        let day = fixedShift["Day"] as! String
        let hour = fixedShift["Hour"] as! Int
        let minute = fixedShift["Minute"] as! Int
        
        let dayDict = ["Maandag": 2, "Dinsdag": 3, "Woensdag": 4, "Donderdag": 5, "Vrijdag": 6, "Zaterdag": 7, "Zondag": 1]
        
        var firstOccurrenceDate = nextOccurenceOfDay(dayDict[day]!).set(componentsDict: ["hour": hour, "minute": minute])
        
        for week in 0..<amountOfRecurringWeeks
        {
            let shift = PFObject(className: "Shifts")
            
            shift["Date"] = firstOccurrenceDate! + (7 * week).day
            shift["Status"] = "idle"
            shift["Owner"] = PFUser.currentUser()
            shift.saveInBackground()
        }
    }
    
    // function that calculates on which date the next occurence is of a given weekday
{'fraction_non_alphanumeric': 0.11449631449631449, 'fraction_numerical': 0.009828009828009828, 'mean_word_length': 1.8468531468531468, 'pattern_counts': {'":': 18, '<': 9, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4793680', 'n_tokens_mistral': 1233, 'n_tokens_neox': 1158, 'n_words': 304}