package com.renrenliao.im.ui.activity

import android.view.LayoutInflater
import android.view.View
import android.widget.ImageView
import androidx.core.content.ContextCompat
import androidx.recyclerview.widget.LinearLayoutManager
import com.renrenliao.im.R
import com.renrenliao.im.base.BaseVMActivity
import com.renrenliao.im.ext.click
import com.renrenliao.im.ui.adapter.GroupListAdapter
import com.renrenliao.im.utils.DeviceUtils
import com.renrenliao.im.utils.LogUtils
import com.renrenliao.im.utils.RouteUtils
import com.renrenliao.im.viewmodel.GroupListViewModel
import com.renrenliao.im.widget.view.VerticalDecoration
import kotlinx.android.synthetic.main.activity_grouplist.*
import org.koin.androidx.viewmodel.ext.android.getViewModel

/**
 **@ClassName GroupListActivity
 **@Description
 **@Author gan
 **@Date 2022/6/10 16:00
 **/
class GroupListActivity:BaseVMActivity<GroupListViewModel>() {

    private var emptyView:View? = null

    private val groupAdapter by lazy { GroupListAdapter() }

    override fun initVM(): GroupListViewModel = getViewModel()

    override fun getLayoutResId(): Int = R.layout.activity_grouplist

    override fun initView() {
        tool_bar.setOnBackListener { onBackPressed() }
        val ivMore = tool_bar.findViewById<ImageView>(R.id.iv_right_more)
        ivMore.setImageResource(R.drawable.ic_grouplist_creategroup_btn_normal)
        //右上角点击
        ivMore.click {
            RouteUtils.gotoGroupMember(ivMore.context)
        }
        //没有群点击空试图创建群
        emptyView = LayoutInflater.from(this).inflate(R.layout.layout_empty_grouplist,null)
        val ivAddGroup = emptyView?.findViewById<ImageView>(R.id.iv_add_group)
        ivAddGroup?.click {
            LogUtils.error("创建群")
        }
        val mDivider = ContextCompat.getDrawable(this, R.drawable.divider_left_63)
        recy_group.addItemDecoration(VerticalDecoration(this,mDivider,
            DeviceUtils.dip2px(this, 0.6f).toInt()
        ))
        recy_group.run {
            adapter = groupAdapter
            layoutManager = LinearLayoutManager(recy_group.context)
        }
        groupAdapter.setOnItemClickListener { _, _, position ->
            LogUtils.error("点击群 $position")
        }

    }

    override fun initData() {

    }



    override fun startObserve() {
        mViewModel.run {
            grouplistLiveData.observe(this@GroupListActivity) { list ->
//                list as MutableList
//                list.clear()
                if (list.isEmpty()){
                    emptyView?.let { groupAdapter.setEmptyView(it) }
                }
                groupAdapter.setList(list)
            }
        }
    }
}