package com.adealink.weparty.room.member

import android.annotation.SuppressLint
import android.os.Bundle
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.adealink.frame.base.fastLazy
import com.adealink.frame.mvvm.view.viewBinding
import com.adealink.frame.router.Router
import com.adealink.frame.router.annotation.BindExtra
import com.adealink.frame.router.annotation.RouterUri
import com.adealink.frame.util.onClick
import com.adealink.weparty.commonui.ext.dp
import com.adealink.weparty.commonui.ext.gone
import com.adealink.weparty.commonui.ext.show
import com.adealink.weparty.commonui.recycleview.adapter.MultiTypeListAdapter
import com.adealink.weparty.commonui.recycleview.itemdecoration.HorizontalItemDecoration
import com.adealink.weparty.commonui.widget.BottomDialogFragment
import com.adealink.weparty.module.order.Order
import com.adealink.weparty.module.order.data.ORDER_SOURCE_DISPATCH_CENTER
import com.adealink.weparty.module.profile.Profile
import com.adealink.weparty.module.profile.ProfileModule
import com.adealink.weparty.module.profile.data.UserInfo
import com.adealink.weparty.module.profile.data.UserPlaymateSkill
import com.adealink.weparty.module.room.Room
import com.adealink.weparty.module.setting.Setting
import com.adealink.weparty.room.R
import com.adealink.weparty.room.databinding.DialogRoomMemberBinding
import com.adealink.weparty.room.member.adapter.RoomMemberCategoryItemBinder
import com.adealink.weparty.room.member.comp.MemberBottomComp
import com.adealink.weparty.room.member.data.MemberPlaymateCategoryItemData

@RouterUri(path = [Room.RoomMemberInfo.PATH], desc = "房间成员资料卡")
class RoomMemberDialog : BottomDialogFragment(R.layout.dialog_room_member) {

    @BindExtra(name = Room.Common.EXTRA_UID, desc = "成员uid", must = true)
    var uid: String? = null
    private val opUid: String
        get() = uid ?: ""

    private val isSelf: Boolean
        get() = opUid == ProfileModule.getMyUid()

    private val binding by viewBinding(DialogRoomMemberBinding::bind)
    private val profileViewModel by fastLazy { ProfileModule.getProfileViewModel(this) }
    private val categoryAdapter by fastLazy { MultiTypeListAdapter<MemberPlaymateCategoryItemData>() }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        Router.bind(this)
    }

    override fun initViews() {
        super.initViews()
        binding.btnReport.show(!isSelf)
        binding.btnReport.onClick {
            clickReport()
        }
        binding.ivAvatar.onClick {
            goProfile()
        }

        categoryAdapter.register(
            RoomMemberCategoryItemBinder {
                createOrder(it.skill)
            }
        )

        binding.vPlaymate.rvPlaymateCategory.adapter = categoryAdapter
        binding.vPlaymate.rvPlaymateCategory.layoutManager = LinearLayoutManager(context, RecyclerView.HORIZONTAL, false)
        binding.vPlaymate.rvPlaymateCategory.addItemDecoration(
            HorizontalItemDecoration(6.dp(), 12.dp(), 0)
        )
    }

    override fun initComponents() {
        super.initComponents()
        MemberBottomComp(this, binding.vBottom, opUid, isSelf).attach()
    }

    override fun observeViewModel() {
        super.observeViewModel()
        profileViewModel?.userInfoLD?.observe(viewLifecycleOwner) {
            updateUserInfo(it)
        }
    }

    @SuppressLint("SetTextI18n")
    private fun updateUserInfo(userInfo: UserInfo?) {
        binding.ivAvatar.setImageUrl(userInfo?.avatar)
        binding.tvName.text = userInfo?.nickName
        binding.vGender.setGender(userInfo?.gender)
        binding.vGender.setAge(userInfo?.age)
        binding.tvId.text = "ID ${userInfo?.uid ?: ""}"

        if (userInfo?.isPlaymate() == true && isSelf) {
            //隐藏我陪玩师
            binding.vPlaymate.root.gone()
        } else {
            binding.vPlaymate.root.show()
            categoryAdapter.submitList(
                userInfo?.skills?.map {
                    MemberPlaymateCategoryItemData(it)
                } ?: emptyList()
            )
        }
    }

    override fun loadData() {
        super.loadData()
        updateUserInfo(ProfileModule.getCacheUserInfo(opUid))
        profileViewModel?.pullUserInfoBy(opUid, false)
    }

    private fun goProfile() {
        val act = activity ?: return
        if (opUid.isEmpty()) {
            return
        }
        Router.build(act, Profile.UserProfile.PATH)
            .putExtra(Profile.Common.EXTRA_UID, opUid)
            .start()
    }

    private fun clickReport() {
        val act = activity ?: return
        Router.build(act, Setting.Report.PATH)
            .putExtra(Setting.Report.EXTRA_UID, opUid)
            .start()
    }

    private fun createOrder(skill: UserPlaymateSkill) {
        val act = activity ?: return
        Router.getRouterInstance<BottomDialogFragment>(Order.CreateDialog.PATH)?.let {
            it.arguments = Bundle().apply {
                putString(Order.CreateDialog.EXTRA_PLAYMATE_ID, skill.id)
                putBoolean(Order.CreateDialog.EXTRA_MODIFY, true)
                putBoolean(Order.CreateDialog.EXTRA_SHOW_REMARK, true)
                putInt(Order.CreateDialog.EXTRA_ORDER_SOURCE, ORDER_SOURCE_DISPATCH_CENTER)
            }
            it.show(act.supportFragmentManager)
        }
    }

}