package com.renrenliao.im.ui.fragment

import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.widget.ImageView
import android.widget.RelativeLayout
import android.widget.TextView
import androidx.core.content.ContextCompat
import androidx.recyclerview.widget.LinearLayoutManager
import com.gyf.immersionbar.ktx.statusBarHeight
import com.renrenliao.im.R
import com.renrenliao.im.base.BaseVMFragment
import com.renrenliao.im.ext.click
import com.renrenliao.im.ui.adapter.FriendSortAdapter
import com.renrenliao.im.utils.LogUtils
import com.renrenliao.im.utils.RouteUtils
import com.renrenliao.im.viewmodel.RrlFriendViewModel
import com.renrenliao.im.widget.floatmenu.FloatMenu
import com.renrenliao.im.widget.floatmenu.MenuItem
import kotlinx.android.synthetic.main.fragment_rrlfriend.*
import org.koin.androidx.viewmodel.ext.android.getViewModel

/**
 **@ClassName AlarmsFragment
 **@Description
 **@Author gan
 **@Date 2022/5/26 12:53
 **/
class RrlFriendFragment:BaseVMFragment<RrlFriendViewModel>() {
    private val TAG = RrlFriendFragment::class.java.simpleName

    private val sortAdapter by lazy { FriendSortAdapter() }
  //  val list = mutableListOf<String>("a","d","b","c")

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

    override fun initVM(): RrlFriendViewModel = getViewModel()

    override fun initView() {

        ll_parent.setPadding(0,statusBarHeight,0,0)

        tool_bar.findViewById<TextView>(R.id.tv_title_left).text = "通讯录"
        val ivMore = tool_bar.findViewById<ImageView>(R.id.iv_right_more)
        ivMore.setImageResource(R.drawable.main_friend_head_right_btn)

        //初始化右上角floatMenu
        val floatMenu = FloatMenu(requireContext(),ivMore)
        ivMore.click {
            floatMenu.showTopRightLocation()
        }
        val menuItemList = mutableListOf<MenuItem>()
        menuItemList.add(MenuItem().setItem("群发消息").setItemResId(R.drawable.roster_more_ivnvite_ico).setItemActionId(1))
        menuItemList.add(MenuItem().setItem("添加好友").setItemResId(R.drawable.roster_more_adduser_ico).setItemActionId(2))
        floatMenu.items(menuItemList,true)

        val drawable = ContextCompat.getDrawable(requireContext(), R.drawable.bg_pop_msg)
        floatMenu.setBackgroundDrawable(drawable)
        floatMenu.setOnItemClickListener { _, position ->
            when(menuItemList[position].itemActionId){
                1 ->{
                    LogUtils.error("群发消息")
                }
                2 ->{
                    LogUtils.error("添加好友")
                    RouteUtils.gotoFriendSearch(requireContext())
                }
            }
        }

        //初始化RecyclerView
        val headerView = LayoutInflater.from(activity).inflate(R.layout.view_roster_headview,null)
        //群聊
        val groupView = headerView.findViewById<RelativeLayout>(R.id.groups_chat)
        groupView.click { RouteUtils.gotoGroupList(groupView.context) }
        sortAdapter.setHeaderView(headerView)
        recy_roster.run {
            adapter = sortAdapter
            layoutManager = LinearLayoutManager(recy_roster.context)
        }
      //  sortAdapter.setList(list)
    }

    override fun initData() {

    }


    override fun startObserve() {
        mViewModel.run {
            friendLiveData.observe(viewLifecycleOwner) {
                sortAdapter.setList(it)
            }
        }

    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        LogUtils.error(TAG+"onViewCreated")
    }

    override fun onDestroyView() {
        super.onDestroyView()
        LogUtils.error(TAG+"onDestroyView")
    }

    override fun onDestroy() {
        super.onDestroy()
        LogUtils.error(TAG+"onDestroy")
    }
}