<commit_before>//
//  WeeklyPlanManager.swift
//  WeeklyFoodPlan
//
//  Created by vulgur on 2017/3/5.
//  Copyright © 2017年 MAD. All rights reserved.
//

import Foundation
import RealmSwift
import SwiftDate

class WeeklyPlanManager {

    static let shared = WeeklyPlanManager()
    
    let realm = try! Realm()
    static let firstWeekdayKey = "FirstWeekday"
    var firstWeekDay = WeekDay.sunday
    
    private func regionOf(firstWeekDay: WeekDay) -> Region {
        let tz = TimeZone.autoupdatingCurrent
        let loc = Locale.autoupdatingCurrent
        var cal = Calendar.autoupdatingCurrent
        cal.firstWeekday = firstWeekDay.rawValue
        
        let region = Region(tz: tz, cal: cal, loc: loc)
        return region
    }

    func nextWeeklyPlan() -> [DailyPlan] {
        let now = Date()
        let results = realm.objects(DailyPlan.self).filter("date > %@", now)
        return results.toArray()
    }

    func fakeWeeklyPlan() -> [DailyPlan] {
        let value = UserDefaults.standard.integer(forKey: WeeklyPlanManager.firstWeekdayKey)
        // random the value for test
//        let value = Int(arc4random_uniform(UInt32(7))) + 1
        if value != 0 {
            firstWeekDay = WeekDay(rawValue: value)!
        } else {
            firstWeekDay = WeekDay(rawValue: Calendar.autoupdatingCurrent.firstWeekday)!
            UserDefaults.standard.set(firstWeekDay.rawValue, forKey: WeeklyPlanManager.firstWeekdayKey)
        }

        let today = DateInRegion(absoluteDate: Date(), in: regionOf(firstWeekDay: firstWeekDay))
        
        var nextStartDay: DateInRegion
        var nextEndDay: DateInRegion

        nextStartDay = today + 1.day
        nextEndDay = nextStartDay.endWeek
        
        var components = DateComponents()
        components.day = 1
        let days = Date.dates(between: nextStartDay.absoluteDate, and: nextEndDay.absoluteDate - 1.day, increment: components)

        var plans = [DailyPlan]()
        for day in days {
            let dailyPlan = DailyPlanManager.shared.fakePlan()
            dailyPlan.date = day
            plans.append(dailyPlan)
        }
        // if before night, make meals for today
        if let todayPlan = todayPlan() {
            plans.insert(todayPlan, at: 0)<commit_msg>Fix bug calculating the nextEndDay<commit_after>//
//  WeeklyPlanManager.swift
//  WeeklyFoodPlan
//
//  Created by vulgur on 2017/3/5.
//  Copyright © 2017年 MAD. All rights reserved.
//

import Foundation
import RealmSwift
import SwiftDate

class WeeklyPlanManager {

    static let shared = WeeklyPlanManager()
    
    let realm = try! Realm()
    static let firstWeekdayKey = "FirstWeekday"
    var firstWeekDay = WeekDay.sunday
    
    private func regionOf(firstWeekDay: WeekDay) -> Region {
        let tz = TimeZone.autoupdatingCurrent
        let loc = Locale.autoupdatingCurrent
        var cal = Calendar.autoupdatingCurrent
        cal.firstWeekday = firstWeekDay.rawValue
        
        let region = Region(tz: tz, cal: cal, loc: loc)
        return region
    }

    func nextWeeklyPlan() -> [DailyPlan] {
        let now = Date()
        let results = realm.objects(DailyPlan.self).filter("date > %@", now)
        return results.toArray()
    }

    func fakeWeeklyPlan() -> [DailyPlan] {
        let value = UserDefaults.standard.integer(forKey: WeeklyPlanManager.firstWeekdayKey)
        // random the value for test
//        let value = Int(arc4random_uniform(UInt32(7))) + 1
        if value != 0 {
            firstWeekDay = WeekDay(rawValue: value)!
        } else {
            firstWeekDay = WeekDay(rawValue: Calendar.autoupdatingCurrent.firstWeekday)!
            UserDefaults.standard.set(firstWeekDay.rawValue, forKey: WeeklyPlanManager.firstWeekdayKey)
        }

        let today = DateInRegion(absoluteDate: Date(), in: regionOf(firstWeekDay: firstWeekDay))
        
        var nextStartDay: DateInRegion
        var nextEndDay: DateInRegion

        nextStartDay = today + 1.day
        nextEndDay = nextStartDay.endWeek
        
        if nextStartDay.day == nextEndDay.day { // same day, sunday
            nextEndDay = nextEndDay + 7.day
        }
        var components = DateComponents()
        components.day = 1
        let days = Date.dates(between: nextStartDay.absoluteDate, and: nextEndDay.absoluteDate - 1.day, increment: components)

        var plans = [DailyPlan]()
        for day in days {
            let dailyPlan = DailyPlanManager.shared.fakePlan()
            dailyPlan.date = day
            plans.append(dailyPlan)
        }
        // if before night, make meals for today
        if let todayPlan = todayPlan() {
            plans.insert(todayPlan, at: 0)
{'fraction_non_alphanumeric': 0.09023196424771228, 'fraction_numerical': 0.008725260693764631, 'mean_word_length': 2.364352183249821, 'pattern_counts': {'":': 0, '<': 3, '<?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': '6744597', 'n_tokens_mistral': 1413, 'n_tokens_neox': 1316, 'n_words': 340}