package com.hongyegroup.cpt_parttime.ui

import android.content.Intent
import android.os.Bundle
import android.view.ViewGroup
import android.widget.ImageView
import androidx.recyclerview.widget.RecyclerView
import com.guadou.cs_cptservices.base.activity.YYBaseVDBActivity
import com.guadou.lib_baselib.base.vm.EmptyViewModel
import com.guadou.lib_baselib.bean.DataBindingConfig
import com.guadou.lib_baselib.engine.ImagePreviewUtils
import com.guadou.lib_baselib.engine.extLoad
import com.guadou.lib_baselib.ext.click
import com.guadou.lib_baselib.ext.vertical
import com.guadou.lib_baselib.utils.CommUtils
import com.hongyegroup.cpt_parttime.R
import com.hongyegroup.cpt_parttime.databinding.ActivityParttimeJobAlbumBinding
import com.luck.picture.lib.decoration.GridSpacingItemDecoration
import com.lxj.easyadapter.EasyAdapter
import com.lxj.easyadapter.ViewHolder
import dagger.hilt.android.AndroidEntryPoint

@AndroidEntryPoint
class PartJobAlbumActivity : YYBaseVDBActivity<EmptyViewModel, ActivityParttimeJobAlbumBinding>() {

    val mDatas = mutableListOf<String>()
    val mAdapter = ImageAdapter(mDatas)

    companion object {
        fun startInstance() {
            val context = CommUtils.getContext()
            val intent = Intent(context, PartJobAlbumActivity::class.java)
            intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
            context.startActivity(intent)
        }
    }

    override fun getDataBindingConfig(): DataBindingConfig {
        return DataBindingConfig(R.layout.activity_parttime_job_album)
    }

    override fun init(savedInstanceState: Bundle?) {

        initData()
        initRV()
    }

    private fun initData() {
        mDatas.add("http://i02piccdn.sogoucdn.com/4fad16afd3acd5b6")
        mDatas.add("http://img.zcool.cn/community/019241565fa3df6ac7255d2e15560a.jpg@1280w_1l_2o_100sh.jpg")
        mDatas.add("http://img.zcool.cn/community/0158a658a2b836a801219c77059a95.png@1280w_1l_2o_100sh.png")
        mDatas.add("http://i02piccdn.sogoucdn.com/254b0d3e6cb288f0")
        mDatas.add("http://img.zcool.cn/community/0170755a65d091a80120a123ad53ba.jpg@1280w_1l_2o_100sh.jpg")
        mDatas.add("http://img.zcool.cn/community/0160855ac4e9c6a801212573507ad7.png@1280w_1l_2o_100sh.png")
        mDatas.add("http://img.zcool.cn/community/01abe358a2b7c6a801219c779bb54a.png@1280w_1l_2o_100sh.png")
        mDatas.add("http://i02piccdn.sogoucdn.com/69e667b2e752bdf7")
        mDatas.add("http://i02piccdn.sogoucdn.com/71390d1ade651f3a")
        mDatas.add("http://img.zcool.cn/community/01066c5a97ad44a801206d965f0e52.jpg@1280w_1l_2o_100sh.jpg")
    }

    private fun initRV() {
        mBinding.recyclerView.vertical(3).apply {
            adapter = mAdapter
            addItemDecoration(GridSpacingItemDecoration(3, CommUtils.dip2px(10), true))
        }
    }

    override fun startObserve() {

    }

    //RV的数据适配器
    inner class ImageAdapter(private val list: List<String>) : EasyAdapter<String>(list, R.layout.item_grid_rv_img) {
        override fun bind(holder: ViewHolder, t: String, position: Int) {
            val imageView = holder.getView<ImageView>(R.id.iv_image)
            imageView.extLoad(t, R.mipmap.ic_launcher, isForceOriginalSize = true)

            //设置点击事件 弹起多图的预览
            imageView.click {
                //开启多图预览
                ImagePreviewUtils.multipleImagePreview(
                    imageView.context,
                    imageView,
                    list,
                    position,
                    R.mipmap.ic_launcher
                ) { popupView, position ->
                    //当预览的图片滚动的时候需要选择是哪一个ImageView来生效了
                    val rv = holder.itemView.parent as RecyclerView
                    popupView.updateSrcView((rv.getChildAt(position) as ViewGroup).getChildAt(0) as ImageView)
                }
            }
        }

    }

}