package com.drawing.drawsketch

import android.content.Context
import android.util.Log
import com.drawing.drawsketch.dbmodel.MyObjectBox
import com.drawing.drawsketch.dbmodel.ResSvg
import com.drawing.drawsketch.dbmodel.ResSvg_
import com.drawing.drawsketch.dbmodel.UpLoad
import com.drawing.drawsketch.dbmodel.UpLoad_
import com.drawing.drawsketch.listener.AddRemoveFavoriteListener
import io.objectbox.Box
import io.objectbox.BoxStore
import io.objectbox.query.QueryBuilder

object DbBase {


        private var boxStore: BoxStore? = null

        private var LikeBox: Box<ResSvg>? = null

        private var ImportBox: Box<UpLoad>? = null

        private val list: MutableList<AddRemoveFavoriteListener> = ArrayList()

        fun addListener(listener: AddRemoveFavoriteListener) {
            list.add(listener)
        }

        fun init(context: Context?) {
            boxStore = context?.let { MyObjectBox.builder().androidContext(it).build() }
        }

        private val objectBoxLike: Box<ResSvg>?
            get() {
                if (LikeBox == null) {
                    LikeBox = boxStore?.boxFor(ResSvg::class.java)
                }

                return LikeBox
            }
        private val objectBoxImport: Box<UpLoad>?
            get() {
                if (ImportBox == null) {
                    ImportBox = boxStore?.boxFor(UpLoad::class.java)
                }

                return ImportBox
            }

        fun insertOrUpdateLike(mPath: String?) {
            val objectBoxLike: Box<ResSvg>? = objectBoxLike
            val first: ResSvg? = objectBoxLike!!.query()
                .equal(ResSvg_.path, mPath, QueryBuilder.StringOrder.CASE_SENSITIVE)
                .build()
                .findFirst()
            if (first == null) {
                val beanLocal  = ResSvg()
                beanLocal.path = mPath
                objectBoxLike.put(beanLocal)
                for (listener in list) {
                    listener.AddFavorite()
                }
            } else {
            }
        }

        fun insertImport(muri: String?) {
            val objectBoxImport: Box<UpLoad>? = objectBoxImport
            val first: UpLoad? = objectBoxImport!!.query()
                .equal(UpLoad_.uri, muri, QueryBuilder.StringOrder.CASE_SENSITIVE)
                .build()
                .findFirst()
            if (first == null) {
                val beanUpload: UpLoad = UpLoad()
                beanUpload.uri = muri
                objectBoxImport.put(beanUpload)
            } else {
            }
        }

        fun deleteImport(mPath: String) {
            val objectBoxImport: Box<UpLoad>? = objectBoxImport
            val first: UpLoad? = objectBoxImport!!.query()
                .equal(UpLoad_.uri, mPath, QueryBuilder.StringOrder.CASE_SENSITIVE)
                .build()
                .findFirst()
            if (first != null) {
                objectBoxImport!!.remove(first)
                for (listener in list) {
//                listener.onDeleteLike();
                }
                Log.d(App.TAG, "--------deleteImport imId=$mPath")
            }
        }

        fun deleteLike(mPath: String) {
            val objectBoxLike: Box<ResSvg>? = objectBoxLike
            val boxLike: ResSvg? = objectBoxLike!!.query()
                .equal(ResSvg_.path, mPath, QueryBuilder.StringOrder.CASE_SENSITIVE)
                .build()
                .findFirst()
            if (boxLike != null) {
                objectBoxLike.remove(boxLike)
                for (listener in list) {
                    listener.RemoveFavorite()
                }
                Log.d(App.TAG, "--------deleteLike imId=$mPath")
            }
        }

        fun queryAllLike(): MutableList<ResSvg> {
            return objectBoxLike!!.query()
                .build()
                .find()
        }

        fun queryAllImport(): MutableList<UpLoad> {
            return objectBoxImport!!.query()
                .build()
                .find()
        }

        fun queryIsImport(mPath: String?): Boolean {
            val objectBoxImport: Box<UpLoad>? = objectBoxImport
            val first: UpLoad? = objectBoxImport!!.query()
                .equal(UpLoad_.uri, mPath, QueryBuilder.StringOrder.CASE_SENSITIVE)
                .build()
                .findFirst()
            return first != null
        }


        fun queryIsLike(mPath: String?): Boolean {
            val objectBoxLike: Box<ResSvg>? = objectBoxLike
            val first: ResSvg? = objectBoxLike!!.query()
                .equal(ResSvg_.path, mPath, QueryBuilder.StringOrder.CASE_SENSITIVE)
                .build()
                .findFirst()
            return first != null
        }
    }


