package com.renrenliao.im.ui.activity

import android.annotation.SuppressLint
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import android.widget.LinearLayout
import androidx.core.content.ContextCompat
import androidx.core.widget.addTextChangedListener
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.ext.toast
import com.renrenliao.im.ui.adapter.GroupMemberAdapter
import com.renrenliao.im.utils.DataCacheUtils
import com.renrenliao.im.utils.DeviceUtils
import com.renrenliao.im.utils.LogUtils
import com.renrenliao.im.utils.MapUtils
import com.renrenliao.im.viewmodel.GroupMemberViewModel
import com.renrenliao.im.widget.view.VerticalDecoration
import kotlinx.android.synthetic.main.activity_groupcreate.*
import org.koin.androidx.viewmodel.ext.android.getViewModel


/**
 **@ClassName GroupMemberActivity
 **@Description
 **@Author gan
 **@Date 2022/6/10 19:23
 **/
class GroupCreateActivity : BaseVMActivity<GroupMemberViewModel>() {
    //空视图
    private var emptyView: View? = null

    private val groupMemberAdapter by lazy { GroupMemberAdapter() }

    override fun initVM(): GroupMemberViewModel = getViewModel()

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

    @SuppressLint("NotifyDataSetChanged", "SetTextI18n")
    override fun initView() {
        tool_bar.setOnBackListener { onBackPressed() }
        //空视图
        emptyView = LayoutInflater.from(this).inflate(R.layout.layout_empty_groupmember, null)
        val iv = emptyView?.findViewById<ImageView>(R.id.iv)
        iv?.click {
            LogUtils.error("点击回到主界面添加好友")
        }

        //recy
        val mDivider = ContextCompat.getDrawable(this, R.drawable.divider_left_63)
        recy_member.addItemDecoration(
            VerticalDecoration(
                this, mDivider,
                DeviceUtils.dip2px(this, 0.6f).toInt()
            )
        )
        recy_member.run {
            adapter = groupMemberAdapter
            layoutManager = LinearLayoutManager(recy_member.context)
        }
        groupMemberAdapter.setOnItemClickListener { _, _, position ->
            //点击取反
            groupMemberAdapter.data[position].ext11 = !groupMemberAdapter.data[position].ext11

            val size = if(groupMemberAdapter.data.any { it.ext11 }){
                groupMemberAdapter.data.filter { it.ext11 }.size.toString()
            }else{
                ""
            }
            tv_sure.text = "确定 $size"
            groupMemberAdapter.notifyDataSetChanged()
        }
    }


    override fun initData() {
        mViewModel.getFriends()

        //点击确定
        tv_sure.click {
            if (groupMemberAdapter.data.none { it.ext11 }) {
                toast("请选择要加入群聊的好友")
                return@click
            }
            if (!DataCacheUtils.isLogin) {
                toast("账户已下线，请重新登录")
                return@click
            }
            //创建群 请求接口
            createGroup()
        }
        //监听et_search 变化
        et_search.addTextChangedListener {
            searchForKey(it.toString().trim())
        }
    }
    //模糊查询
    private fun searchForKey(key:String){
        mViewModel.getFriendsByKey(key)
    }

    private fun createGroup() {
        val dtoList = mutableListOf<String>()
        groupMemberAdapter.data
            .filter { it.ext11 }
            .map {
                val map = HashMap<String, String>()
                map["userUid"] = it.userUid

                dtoList.add(MapUtils.map2JsonStr(map))
            }
        val mine = HashMap<String, String>()
        mine["userUid"] = DataCacheUtils.getUid()
        dtoList.add(MapUtils.map2JsonStr(mine))

        val map = HashMap<String, String>()
        map["userUid"] = DataCacheUtils.getUid()
        map["members"] = dtoList.toString()
        mViewModel.createGroup(map)
        //  LogUtils.error(map)
    }


    override fun startObserve() {
        mViewModel.run {
            friendsLiveData.observe(this@GroupCreateActivity) {
                //设置全屏空视图
                val parent= emptyView?.parent
                if (parent !=null){
                    (parent as ViewGroup).removeView(emptyView)
                }
                if (it.isEmpty()) {
                    cl_main.addView(emptyView, LinearLayout.LayoutParams(
                            LinearLayout.LayoutParams.MATCH_PARENT,
                            LinearLayout.LayoutParams.MATCH_PARENT
                        )
                    )
                }
                //set adapter
                groupMemberAdapter.setList(it)
                tv_sure.text = "确定"
            }
            //观察是否创建群成功
            createGroupLiveData.observe(this@GroupCreateActivity){
                if (it){
                    finish()
                }
            }
        }
    }
}