//
//  TSRealmManager.swift
//  AIEmoji
//
//  Created by 100Years on 2025/2/11.
//

import RealmSwift

let TSRMShared = TSRealmManager.shared



extension List {
    subscript(safe index: Int) -> Element? {
        return indices.contains(index) ? self[index] : nil
    }
}

extension Results {
    subscript(safe index: Index) -> Element? {
        return indices.contains(index) ? self[index] : nil
    }
}

extension Array {
    subscript(safe index: Index) -> Element? {
        return indices.contains(index) ? self[index] : nil
    }
}


class TSRealmManager {
    static let shared = TSRealmManager()
    private init() {
        /*设置新的版本号
         1.0 ->1
         **/
   
        let newSchemaVersion: UInt64 = 1
        // 获取默认配置
        var config = Realm.Configuration.defaultConfiguration
        // 设置新版本号
        config.schemaVersion = newSchemaVersion
        // 设置迁移块
        config.migrationBlock = { migration, oldSchemaVersion in
            if oldSchemaVersion < newSchemaVersion {
                // 执行迁移操作
            }
        }
        // 将修改后的配置设置为默认配置
        Realm.Configuration.defaultConfiguration = config
        debugPrint("Realm 数据库已成功打开，版本号: \(newSchemaVersion)")
        debugPrint("Realm 数据库已成功打开，realm.configuration.schemaVersion: \(realm.configuration.schemaVersion)")
        
    }
    
//    var realm:Realm {
//        return try! Realm()
//    }
    
    lazy var realm:Realm = {
        return try! Realm()
    }()


    // 创建数据
    func create<T: Object>(_ object: T) {
        do {
            try realm.write {
                realm.add(object,update: .modified)
            }
        } catch {
            print("Failed to create object: \(error)")
        }
    }

    // 读取数据
    func read<T: Object>(_ type: T.Type) -> Results<T> {
        return realm.objects(type)
    }


    func update<T: Object>(_ object: T) {
        do {
            try realm.write {
                try realm.add(object,update: .modified)
            }
        } catch {
            print("Failed to update object: \(error)")
        }
    }

    // 删除数据
    func delete<T: Object>(_ object: T) {
        do {
      
            try realm.write {
                try realm.delete(object)
            }
        } catch {
            print("Failed to delete object: \(error)")
        }
    }
    
    
    func writeThread(wt: (Realm)->Void) {
        do {
            let oneRealm = realm
            try oneRealm.write {
                wt(oneRealm)
            }
        } catch {
            print("Failed to writeThread: \(error)")
        }
        
    }
}


/*
// 创建数据
let person = Person()
person.name = "John"
person.age = 30
RealmManager.shared.create(person)
// 读取数据
let allPersons = RealmManager.shared.read(Person.self)
for person in allPersons {
    print("Name: \(person.name), Age: \(person.age)")
}
// 更新数据
if let firstPerson = allPersons.first {
    RealmManager.shared.update(firstPerson) { person in
        person.age = 31
    }
}
// 删除数据
if let firstPerson = allPersons.first {
    RealmManager.shared.delete(firstPerson)
}
*/
