package com.adealink.weparty.im.list

import android.os.Bundle
import androidx.constraintlayout.widget.ConstraintLayout
import androidx.core.view.updateLayoutParams
import androidx.fragment.app.viewModels
import com.adealink.frame.aab.util.getCompatDimension
import com.adealink.frame.mvvm.view.viewBinding
import com.adealink.frame.router.annotation.RouterUri
import com.adealink.frame.util.onClick
import com.adealink.frame.util.statusBarHeight
import com.adealink.weparty.commonui.BaseFragment
import com.adealink.weparty.im.R
import com.adealink.weparty.im.databinding.FragmentSessionHomeListBinding
import com.adealink.weparty.im.list.viewmodel.SessionListViewModel
import com.adealink.weparty.im.viewmodel.IMViewModelFactory
import com.adealink.weparty.module.im.IM
import com.adealink.weparty.R as APP_R


@RouterUri(
    path = [IM.HomeList.PATH],
    desc = "IM首页会话列表"
)
class SessionHomeListFragment : BaseFragment(R.layout.fragment_session_home_list) {

    private val binding by viewBinding(FragmentSessionHomeListBinding::bind)

    private val viewModel by viewModels<SessionListViewModel> { IMViewModelFactory() }

    override fun initViews() {
        super.initViews()
        binding.clTopBar.updateLayoutParams<ConstraintLayout.LayoutParams> {
            topMargin = activity.statusBarHeight()
        }
        binding.ivClearMessage.onClick {
            clearMessage()
        }

        //val fragment = TUIConversationMinimalistFragment()
        val fragment = SessionListFragment()
        fragment.arguments = Bundle().apply {
            putInt(
                IM.SessionList.EXTRA_LAST_ITEM_BOTTOM,
                getCompatDimension(APP_R.dimen.main_content_padding_bottom).toInt()
            )
        }
        childFragmentManager
            .beginTransaction()
            .add(R.id.fl_content, fragment)
            .commitAllowingStateLoss()

    }

    private fun clearMessage() {
        viewModel.clearMessage()
    }

}