package com.adealink.weparty.share.poster

import androidx.constraintlayout.widget.ConstraintLayout
import androidx.core.view.updateLayoutParams
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.adealink.frame.aab.util.getCompatDimension
import com.adealink.frame.base.fastLazy
import com.adealink.frame.mvvm.view.viewBinding
import com.adealink.frame.router.annotation.RouterUri
import com.adealink.frame.util.statusBarHeight
import com.adealink.weparty.commonui.BaseActivity
import com.adealink.weparty.commonui.ext.dpf
import com.adealink.weparty.commonui.recycleview.adapter.MultiTypeListAdapter
import com.adealink.weparty.commonui.recycleview.itemdecoration.HorizontalItemDecoration
import com.adealink.weparty.module.profile.ProfileModule
import com.adealink.weparty.module.profile.data.UserInfo
import com.adealink.weparty.module.share.Share
import com.adealink.weparty.share.databinding.ActivitySharePosterBinding
import com.adealink.weparty.share.poster.adapter.PhotoItemViewBinder
import com.adealink.weparty.share.poster.comp.SharePosterComp
import com.adealink.weparty.share.poster.data.PhotoData
import com.adealink.weparty.share.poster.data.PhotoItemData
import com.adealink.weparty.R as APP_R

@RouterUri(path = [Share.Poster.PATH], desc = "分享给好友")
class SharePosterActivity : BaseActivity(), PhotoItemViewBinder.OnPhotoSelectedListener {
    private val binding by viewBinding(ActivitySharePosterBinding::inflate)

    private val listAdapter by fastLazy { MultiTypeListAdapter<PhotoItemData>() }
    private val photoItemList = mutableListOf<PhotoItemData>()
    private val profileViewModel by fastLazy { ProfileModule.getProfileViewModel(this@SharePosterActivity) }

    override fun initViews() {
        super.initViews()
        setContentView(binding.root)
        binding.topBar.setPadding(
            0, this@SharePosterActivity.statusBarHeight(), 0, 0
        )
        binding.topBar.updateLayoutParams<ConstraintLayout.LayoutParams> {
            height =
                this@SharePosterActivity.statusBarHeight() + getCompatDimension(APP_R.dimen.common_top_bar_height).toInt()
        }

        listAdapter.register(PhotoItemViewBinder(this))
        binding.rvAlbum.layoutManager = LinearLayoutManager(this, RecyclerView.HORIZONTAL, false)
        binding.rvAlbum.adapter = listAdapter
        binding.rvAlbum.addItemDecoration(
            HorizontalItemDecoration(10.dpf(), 16.dpf(), 0f)
        )
    }

    override fun initComponents() {
        super.initComponents()
        SharePosterComp(this, binding.btnShare).attach()
    }

    override fun loadData() {
        super.loadData()
        updatePoster(ProfileModule.getMyUserInfo())
    }

    override fun observeViewModel() {
        super.observeViewModel()
        profileViewModel?.userInfoLD?.observe(this) {
            updatePoster(it)
        }
    }

    private fun updatePoster(userInfo: UserInfo?) {
        binding.tvName.text = userInfo?.nickName
        binding.vGender.setGender(userInfo?.gender)
        binding.vGender.setAge(userInfo?.age ?: 0)
        binding.tvEvaluate.text = "4.2"

        val photoListData = userInfo?.photos?.map {
            PhotoData(it)
        } ?: emptyList()
        var selectedPhoto: PhotoItemData? = null
        photoItemList.clear()
        photoItemList.addAll(
            photoListData.mapIndexed { index, data ->
                PhotoItemData(data, index == 0).also {
                    if (it.selected) {
                        selectedPhoto = it
                    }
                }
            }
        )
        listAdapter.submitList(photoItemList)
        binding.ivPhoto.setImageUrl(selectedPhoto?.data?.url)
    }

    override fun onSelected(data: PhotoItemData) {
        photoItemList.forEach {
            it.selected = (it === data)
        }
        listAdapter.notifyDataSetChanged()
    }
}