//
//  UserDefaultsStore.swift
//  Feels (iOS)
//
//  Created by Trey Tartt on 1/22/22.
//

import Foundation

class UserDefaultsStore {
    enum Keys: String {
        case savedOnboardingData
        case needsOnboarding
        case useCloudKit
        case deleteEnable
        case mainViewTopHeaderIndex
        case theme
    }
    
    static func getOnboarding() -> OnboardingData {
        if let data = GroupUserDefaults.groupDefaults.object(forKey: UserDefaultsStore.Keys.savedOnboardingData.rawValue) as? Data,
           let model = try? JSONDecoder().decode(OnboardingData.self, from: data) {
            return model
        } else {
            return OnboardingData()
        }
    }
    
    static func saveOnboarding(onboardingData: OnboardingData) -> OnboardingData {
        do {
            let data = try JSONEncoder().encode(onboardingData)
            GroupUserDefaults.groupDefaults.set(data, forKey: UserDefaultsStore.Keys.savedOnboardingData.rawValue)
            return UserDefaultsStore.getOnboarding()
        } catch {
            fatalError("error saving")
        }
    }
}
