//
/* NOTICE: All information contained herein is, and remains the property of Brightinsight Inc. or its customer. 
The intellectual and technical concepts contained herein are proprietary to Brightinsight Inc. or its customer and may be covered by U.S. and Foreign Patents, patents in process, and are protected by trade secret or copyright law.
Dissemination of this information or reproduction of this material is strictly forbidden unless prior written permission is obtained from Brightinsight Inc. or its customer. Access to the source code contained herein is hereby forbidden to anyone except current Brightinsight Inc. employees, managers or contractors who have executed
Confidentiality and Non-disclosure agreements explicitly covering such access.
*/
/**
* System Name : BISDKCore
* Component ID : BISDKUploaderStorageManager
* Description : 
* Author: Arnold Dominguez
* Copyright © : Brightinsight, Inc.
* -----------------------
* Revision Change
* -----------------------
* Author                        Date             Version            Change-Description
 Arnold Dominguez       08/16/21            1.0                 Creation
 Arnold Dominguez       09/15/21            1.1                 Documentation
*========================================================================================================
*/

import Foundation
import CoreData

class BISDKUploaderStorageManager {
    private let database: BISDKDatabase
    
    init(database: BISDKDatabase) {
        self.database = database
    }
    
    func create(fields: [String: Any?], asEntitiesOf type: BISDKUploaderRecordType) {
        let context = database.context
        guard let entity = NSEntityDescription.entity(forEntityName: type.rawValue, in: context) else { return }
        
        context.performAndWait {
            let object = NSManagedObject(entity: entity, insertInto: context)
            for field in fields {
                let key = field.key
                let value = field.value
                object.setValue(value, forKey: key)
            }
            database.saveContext()
        }
    }
    
    func read(entitiesOf type: BISDKUploaderRecordType, using predicate: NSPredicate?) -> [NSManagedObject]? {
        let request = NSFetchRequest<NSManagedObject>(entityName: type.rawValue)
        request.predicate = predicate
        
        do {
            return try database.context.fetch(request)
        } catch let error as NSError {
            print("Couldn't read from database. \(error), \(error.userInfo)")
            return nil
        }
    }
    
    func update(fields: [String: Any?], forEntitiesOf type: BISDKUploaderRecordType, using predicate: NSPredicate?) {
        guard let records = read(entitiesOf: type, using: predicate) else { return }
        
        database.context.performAndWait {
            for record in records {
                for field in fields {
                    let key = field.key
                    let value = field.value
                    record.setValue(value, forKey: key)
                }
            }
            database.saveContext()
        }
    }
    
    func delete(entitiesOf type: BISDKUploaderRecordType, using predicate: NSPredicate?) {
        guard let records = read(entitiesOf: type, using: predicate) else { return }
        let context = database.context
        context.performAndWait {
            for record in records {
                context.delete(record)
            }
            database.saveContext()
        }
    }
}
