package com.hd.live.wall.wallpapers.act

import android.view.View
import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.hd.live.wall.wallpapers.adapter.AdaLike
import com.hd.live.wall.wallpapers.base.BasisActivity
import com.hd.live.wall.wallpapers.databinding.ActivityCollectionBinding
import com.hd.live.wall.wallpapers.utils.ListDecoration
import com.hd.live.wall.wallpapers.utils.dbManager
import com.hd.live.wall.wallpapers.viewmode.VmCollction
import io.objectbox.reactive.DataSubscription

class ActivityCollection : BasisActivity<ActivityCollectionBinding>() {

    private var dataSubscription: DataSubscription? = null

    private var vmCollction: VmCollction? = null
    override fun onClick(v: View?) {

        v?.let {
            if(v == vb.ivBack){
                finish()
            }
        }


    }

    override fun getViewBinding(): ActivityCollectionBinding = ActivityCollectionBinding.inflate(layoutInflater)

    override fun onCreateInit() {

        val adaLike = AdaLike(this)
        vb.recycler.run {
            adapter = adaLike
            layoutManager = GridLayoutManager(
                this@ActivityCollection,
                2,
                RecyclerView.VERTICAL,
                false
            )
            addItemDecoration(
                ListDecoration(
                    this@ActivityCollection,
                    3,
                    3,
                    0
                )
            )
        }


        vmCollction = getActivityScopeViewModel<VmCollction>(
            VmCollction::class.java)

        vmCollction!!.likeLiveData.observe(
            this@ActivityCollection
        ) {
            adaLike.submitData(lifecycle, it)
        }
        dataSubscription = dbManager.setLikeUpdateListener { data ->
            if (data.size == 0) {
                vb.layoutEmpty.visibility = View.VISIBLE
            } else {
                vb.layoutEmpty.visibility = View.GONE
            }
            vmCollction!!.update(data)
        }

    }

    override fun onInitClick() {
        vb.ivBack.setOnClickListener(this)
    }

    override fun isFullScreen(): Boolean = true

    override fun statusBarLight(): Boolean = true


    override fun onDestroy() {
        super.onDestroy()
        if (dataSubscription != null) {
            dataSubscription!!.cancel()
        }
    }
}