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.onClick
import com.adealink.weparty.commonui.BaseActivity
import com.adealink.weparty.commonui.ext.dp
import com.adealink.weparty.commonui.ext.hide
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.module.image.data.PhotoData
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.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.PhotoItemData
import com.adealink.weparty.util.applyImmersive
import com.adealink.weparty.util.formatStar
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) }

    private var selectPhotoItemData: PhotoItemData? = null
    private var selectSkillList: List<UserPlaymateSkill>? = null

    private lateinit var shareComp: SharePosterComp

    override fun initViews() {
        super.initViews()
        setContentView(binding.root)
        applyImmersive { top ->
            binding.topBar.setPadding(
                0, top, 0, 0
            )
            binding.topBar.updateLayoutParams<ConstraintLayout.LayoutParams> {
                height = top + getCompatDimension(APP_R.dimen.common_top_bar_height).toInt()
            }
            binding.nsvContent.updateLayoutParams<ConstraintLayout.LayoutParams> {
                topMargin = top
            }
        }

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

        binding.clBottom.onClick {
            showCategoryList()
        }
        binding.btnShare.onClick {
            shareComp.sharePoster(
                selectPhotoItemData,
                selectSkillList
            )
        }
    }

    override fun initComponents() {
        super.initComponents()
        SharePosterComp(this, binding.root).also { shareComp = it }.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 = formatStar(userInfo?.star)
        binding.tvSummary.text = userInfo?.intro

        //陪玩技能
        onSkillChanged(userInfo?.skills?.take(3) ?: emptyList())

        //图片
        val photoListData = userInfo?.photos?.map {
            PhotoData(it, null, null)
        } ?: emptyList()
        var selectedPhoto: PhotoItemData? = null
        photoItemList.clear()
        //默认选择头像
        if (!userInfo?.avatar.isNullOrEmpty()) {
            val item = PhotoItemData(
                PhotoData(userInfo.avatar, null, null),
                true
            )
            selectedPhoto = item
            photoItemList.add(item)
        }
        photoItemList.addAll(
            photoListData.mapIndexed { index, data ->
                PhotoItemData(data).also {
                    if (selectedPhoto == null) {
                        it.selected = true
                        selectedPhoto = it
                    }
                }
            }
        )
        listAdapter.submitList(photoItemList)
        onSelectPhotoChanged(selectedPhoto)
    }

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

        }
        listAdapter.notifyDataSetChanged()
        onSelectPhotoChanged(data)
    }

    private fun showCategoryList() {
        PosterChooseCategoryDialog().apply {
            setCategoryList(
                ProfileModule.getMyUserInfo()?.skills ?: emptyList(),
                selectSkillList ?: emptyList()
            )
            setSelectListener {
                onSkillChanged(it)
            }
            show(supportFragmentManager, "PosterChooseCategoryDialog")
        }
    }

    private fun onSkillChanged(skillList: List<UserPlaymateSkill>?) {
        selectSkillList = skillList
        val item1 = skillList?.getOrNull(0)
        if (item1 != null) {
            binding.vGame1.root.show()
            binding.vGame1.ivGame.setImageUrl(item1.icon)
            binding.vGame1.tvGame.text = item1.name
        } else {
            binding.vGame1.root.hide()
        }

        val item2 = skillList?.getOrNull(1)
        if (item2 != null) {
            binding.vGame2.root.show()
            binding.vGame2.ivGame.setImageUrl(item2.icon)
            binding.vGame2.tvGame.text = item2.name
        } else {
            binding.vGame2.root.hide()
        }

        val item3 = skillList?.getOrNull(2)
        if (item3 != null) {
            binding.vGame3.root.show()
            binding.vGame3.ivGame.setImageUrl(item3.icon)
            binding.vGame3.tvGame.text = item3.name
        } else {
            binding.vGame3.root.hide()
        }
    }

    private fun onSelectPhotoChanged(data: PhotoItemData?) {
        if (selectPhotoItemData === data) {
            return
        }
        selectPhotoItemData = data
        binding.ivPhoto.setImageUrl(data?.data?.url)
    }

}