package com.adealink.weparty.profile.ui.about

import androidx.fragment.app.activityViewModels
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.weparty.commonui.BaseFragment
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.VerticalSpaceItemDecoration
import com.adealink.weparty.profile.R
import com.adealink.weparty.profile.databinding.FragmentProfileBinding
import com.adealink.weparty.profile.viewmodel.ProfileViewModel
import com.adealink.weparty.profile.viewmodel.ProfileViewModelFactory

class ProfileFragment : BaseFragment(R.layout.fragment_profile) {

    private val binding by viewBinding(FragmentProfileBinding::bind)

    private val profileViewModel by activityViewModels<ProfileViewModel> { ProfileViewModelFactory() }

    private val skillAdapter by fastLazy { MultiTypeListAdapter<SkillItemData>() }

    override fun initViews() {
        super.initViews()
        skillAdapter.register(SKillViewBinder {
            createOrder()
        })
        binding.clSkill.rvSkil.adapter = skillAdapter
        binding.clSkill.rvSkil.layoutManager =
            LinearLayoutManager(context, RecyclerView.VERTICAL, false)
        binding.clSkill.rvSkil.addItemDecoration(
            VerticalSpaceItemDecoration(6.dp())
        )
    }

    override fun observeViewModel() {
        super.observeViewModel()
        profileViewModel.userInfoLD.observe(viewLifecycleOwner) { userInfo ->
            binding.clIntroduce.tvLanguage.text =
                userInfo?.languageNames?.joinToString(separator = ",")
            binding.clIntroduce.tvIntroduce.text = userInfo?.intro

            if (userInfo?.isPlaymate() == true) {
                binding.clSkill.root.show()
                skillAdapter.submitList(
                    userInfo.skills?.map {
                        SkillItemData(it)
                    } ?: emptyList()
                )
            } else {
                binding.clSkill.root.gone()
            }
        }
    }

    private fun createOrder() {

    }

}