package rasel.lunar.launcher.workers

import io.realm.kotlin.Realm
import io.realm.kotlin.RealmConfiguration
import io.realm.kotlin.UpdatePolicy
import io.realm.kotlin.ext.query
import io.realm.kotlin.ext.realmListOf
import io.realm.kotlin.types.BaseRealmObject
import io.realm.kotlin.types.RealmList
import io.realm.kotlin.types.RealmObject
import io.realm.kotlin.types.TypedRealmObject
import io.realm.kotlin.types.annotations.Ignore
import io.realm.kotlin.types.annotations.PrimaryKey
import rasel.lunar.launcher.apps.SimpleContact
import rasel.lunar.launcher.model.AppInfo
import rasel.lunar.launcher.model.Astro
import rasel.lunar.launcher.model.BotCommandEentitie
import rasel.lunar.launcher.model.Clouds
import rasel.lunar.launcher.model.Condition
import rasel.lunar.launcher.model.Current
import rasel.lunar.launcher.model.CurrentPlayItem
import rasel.lunar.launcher.model.Day
import rasel.lunar.launcher.model.Forecast
import rasel.lunar.launcher.model.Forecastday
import rasel.lunar.launcher.model.Hour
import rasel.lunar.launcher.model.Location
import rasel.lunar.launcher.model.Main
import rasel.lunar.launcher.model.NotificationItem
import rasel.lunar.launcher.model.RssData
import rasel.lunar.launcher.model.RssDataInterface
import rasel.lunar.launcher.model.Sys
import rasel.lunar.launcher.model.TelegramBotUpdate
import rasel.lunar.launcher.model.TelegramChat
import rasel.lunar.launcher.model.TelegramData
import rasel.lunar.launcher.model.TelegramFrom
import rasel.lunar.launcher.model.TelegramMessage
import rasel.lunar.launcher.model.Weather
import rasel.lunar.launcher.model.WeatherForcast
import rasel.lunar.launcher.model.Wind
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, CurrentPlayItem::class,
        TelegramBotUpdate::class,    TelegramData::class,  TelegramMessage::class,  TelegramChat::class,   BotCommandEentitie::class,   TelegramFrom::class,
        WeatherForcast::class, Location::class, Current::class, Forecast::class, Condition::class, Forecastday::class, Day::class, Astro::class, Hour::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) {

                }
            }
        }
    }

    fun updateAppUse(pkg : String) {
        getRealm().writeBlocking {
            val result = query<AppInfo>().query("pkgName == $0",pkg).find()
            if(result.size > 0) {
                val appInfo = result.first()
                appInfo.clickCount = appInfo.clickCount + 1
                appInfo.lastUseDate = System.currentTimeMillis()
            }
        }
    }

}