package rasel.lunar.launcher.workers

import com.google.gson.Gson
import io.realm.kotlin.Realm
import io.realm.kotlin.RealmConfiguration
import io.realm.kotlin.UpdatePolicy
import io.realm.kotlin.types.BaseRealmObject
import io.realm.kotlin.types.TypedRealmObject
import rasel.lunar.launcher.apps.SimpleContact
import rasel.lunar.launcher.model.AppInfo
import rasel.lunar.launcher.model.NotificationItem
import rasel.lunar.launcher.model.RssData
import rasel.lunar.launcher.model.RssDataInterface
import rasel.lunar.launcher.model.getRssData
import rasel.lunar.launcher.utils.BLog
import kotlin.reflect.KClass

object WorkersDb {

    val clazz : Set<KClass<out BaseRealmObject>> = setOf(RssData::class, NotificationItem::class, AppInfo::class,SimpleContact::class, RecentCall::class, RecentSms::class)
    val schemaVersion : Long = 0L

    private var pRealm : Realm? = null
    fun getRealm() : Realm {
        if (pRealm == null) {
            pRealm =  Realm.open(RealmConfiguration.Builder(clazz as Set<KClass<out TypedRealmObject>>)
                .schemaVersion(schemaVersion)
                .deleteRealmIfMigrationNeeded()
                .build())
        }
        return pRealm!!
    }

    fun insertData(rssData: RssData) {
        getRealm().apply {
            this.writeBlocking {
                try {
                    this.copyToRealm(rssData, UpdatePolicy.ERROR)
                } catch (e : Exception) {

                }
            }
        }
    }

    fun insertBulkData(rssDatas: Collection<RssData>) {
        rssDatas.forEach {
            try {
                getRealm().writeBlocking {
                    try {
                        this.copyToRealm(it, UpdatePolicy.ERROR)
                    } catch (e : Exception) {

                    }
                }
            } catch (e : Exception) {
            }
        }
    }

    fun insertBulkInteface(rssInterfaceDatas: Collection<RssDataInterface>) {

        rssInterfaceDatas.forEach {
            try {
                getRealm().writeBlocking {
                    if (it is RssData) {
                        try {
                            this.copyToRealm(it, UpdatePolicy.ERROR)
                        } catch (e : Exception) {

                        }
                    }
                }
            } catch (e : Exception) {
            }
        }
    }

    fun insertNoti(data: NotificationItem) {
        getRealm().apply {
            this.writeBlocking {
                try {
                    this.copyToRealm(data, UpdatePolicy.ALL)
                } catch (e : Exception) {

                }
            }
        }
    }

    fun update(info: AppInfo) {
        getRealm().apply {
            this.writeBlocking {
                try {
                    this.copyToRealm(info, UpdatePolicy.ALL)
                } catch (e : Exception) {

                }
            }
        }
    }

    fun update(contact: SimpleContact) {
        getRealm().apply {
            this.writeBlocking {
                try {
                    this.copyToRealm(contact, UpdatePolicy.ALL)
                } catch (e : Exception) {

                }
            }
        }
    }
    fun insertCall(contact: RecentCall) {
        getRealm().apply {
            this.writeBlocking {
                try {
                    this.copyToRealm(contact, UpdatePolicy.ALL)
                } catch (e : Exception) {

                }
            }
        }
    }
}