package com.sketch.ar.artdrawing.utils

import android.content.Context
import android.util.Log
import com.sketch.ar.artdrawing.MyApp
import com.sketch.ar.artdrawing.listener.AddLikeListener
import com.sketch.ar.artdrawing.dataBean.DataLocal

import com.sketch.ar.artdrawing.dataBean.DataImport
import com.sketch.ar.artdrawing.dataBean.DataImport_
import com.sketch.ar.artdrawing.dataBean.DataLocal_

import com.sketch.ar.artdrawing.dataBean.MyObjectBox
import io.objectbox.Box
import io.objectbox.BoxStore
import io.objectbox.query.QueryBuilder

object ObjectBoxHelper {


        private var boxStore: BoxStore? = null

        private var LikeBox: Box<DataLocal>? = null

        private var ImportBox: Box<DataImport>? = null

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

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

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

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

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

                return ImportBox
            }

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

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

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

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

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

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

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


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


