//
//  PersistenceHelper.swift
//  Feels (iOS)
//
//  Created by Trey Tartt on 2/17/22.
//

import CoreData

extension PersistenceController {
    private var childContext: NSManagedObjectContext {
        return NSManagedObjectContext(concurrencyType: .mainQueueConcurrencyType)
    }
    
    public func randomEntries(count: Int) -> [MoodEntry] {
        var entries = [MoodEntry]()
        
        for idx in 0..<count {
            let newItem = MoodEntry(context: childContext)
            newItem.timestamp = Calendar.current.date(byAdding: .day, value: -idx, to: Date())
            newItem.moodValue = Int16(Mood.allValues.randomElement()!.rawValue)
            let date = Calendar.current.date(byAdding: .day, value: -idx, to: Date())!
            newItem.forDate = date
            newItem.weekDay = Int16(Calendar.current.component(.weekday, from: date))
            newItem.canEdit = true
            newItem.canDelete = true
            entries.append(newItem)
        }
        return entries
    }
    
    func populateMemory() {
        for idx in 1..<25 {
            let newItem = MoodEntry(context: viewContext)
            newItem.timestamp = Calendar.current.date(byAdding: .day, value: -idx, to: Date())
            newItem.moodValue = Int16(Mood.allValues.randomElement()!.rawValue)
            newItem.canEdit = true
            newItem.canDelete = true
            
            let date = Calendar.current.date(byAdding: .day, value: -idx, to: Date())!
            newItem.forDate = date
            newItem.weekDay = Int16(Calendar.current.component(.weekday, from: date))
        }
        do {
            try viewContext.save()
        } catch {
            // Replace this implementation with code to handle the error appropriately.
            // fatalError() causes the application to generate a crash log and terminate. You should not use this function in a shipping application, although it may be useful during development.
            let nsError = error as NSError
            fatalError("Unresolved error \(nsError), \(nsError.userInfo)")
        }
    }
    
    func generateObjectNotInArray() -> MoodEntry {
        let newItem = MoodEntry(context: childContext)
        newItem.timestamp = Date()
        newItem.moodValue = Int16(Mood.placeholder.rawValue)
        newItem.forDate = Date()
        newItem.weekDay = Int16(Calendar.current.component(.weekday, from: Date()))
        newItem.canEdit = false
        newItem.canDelete = false
        return newItem
    }
    
    func populateTestData() {
        for idx in 1..<120 {
            let newItem = MoodEntry(context: viewContext)
            newItem.timestamp = Date()
            newItem.moodValue = Int16(Mood.allValues.randomElement()!.rawValue)
            newItem.canEdit = true
            newItem.canDelete = true
            
            let date = Calendar.current.date(byAdding: .day, value: -idx, to: Date())!
            newItem.forDate = date
            newItem.weekDay = Int16(Calendar.current.component(.weekday, from: date))
        }
        do {
            try viewContext.save()
        } catch {
            // Replace this implementation with code to handle the error appropriately.
            // fatalError() causes the application to generate a crash log and terminate. You should not use this function in a shipping application, although it may be useful during development.
            let nsError = error as NSError
            fatalError("Unresolved error \(nsError), \(nsError.userInfo)")
        }
    }
    
    func longestStreak() -> [MoodEntry] {
//        let predicate = NSPredicate(format: "forDate == %@", date as NSDate)
        
        let fetchRequest = NSFetchRequest<MoodEntry>(entityName: "MoodEntry")
//        fetchRequest.predicate = predicate
        fetchRequest.sortDescriptors = [NSSortDescriptor(key: "forDate", ascending: true)]
        let data = try! viewContext.fetch(fetchRequest)
        return data
    }
}
