package com.adealink.weparty.im.list

import android.os.Bundle
import androidx.constraintlayout.widget.ConstraintLayout
import androidx.core.view.updateLayoutParams
import androidx.fragment.app.activityViewModels
import com.adealink.frame.aab.util.getCompatDimension
import com.adealink.frame.aab.util.getCompatString
import com.adealink.frame.mvvm.view.viewBinding
import com.adealink.frame.router.annotation.RouterUri
import com.adealink.frame.util.onClick
import com.adealink.weparty.commonui.BaseFragment
import com.adealink.weparty.commonui.widget.CommonDialog
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.util.applyImmersive
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 sessionListViewModel by activityViewModels<SessionListViewModel> { IMViewModelFactory() }

    override fun initViews() {
        super.initViews()
        activity.applyImmersive { top ->
            binding.clTopBar.updateLayoutParams<ConstraintLayout.LayoutParams> {
                topMargin = top
            }
        }
        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() {
        CommonDialog.Builder()
            .title(getCompatString(R.string.im_clear_all_unread_message))
            .setShowDefaultCancel(true)
            .positiveText(getCompatString(APP_R.string.commonui_confirm))
            .onPositive {
                sessionListViewModel.clearMessage()
            }
            .show(childFragmentManager, "ClearUnread")
    }

}