//
//  GoalController.swift
//  Master Your Money Build Week
//
//  Created by Lambda_School_Loaner_141 on 8/29/19.
//  Copyright © 2019 Seschwan. All rights reserved.
//

import Foundation
import UIKit

class GoalController {
    
    //singleton
    static let shared = GoalController()
    init() {}
    let userController = UserController()
    
    //source of truth
    var goals: [Goals] = []
    
    //c.r.u.d. create read update delete
    func createGoal(current: Double, final: Double, title: String) {
        let goal = Goals(current: current, final: final, title: title)
        goals.append(goal)
        userController.saveToPersistentStore()
    }
    
    func updateGoal(goal: Goals, with newAmount: Double) {
        goal.amount = newAmount
        do {
            try CoreDataStack.shared.save()
        }
        catch {
            print("Error saving goal")
        }
        
    }
    
    func deleteGoal(goalToDelete: Goals) {
        CoreDataStack.shared.mainContext.delete(goalToDelete)
        do {
            try CoreDataStack.shared.save()
        }
        catch {
            print("Error saving goal")
        }
    }
    
}
