package com.adealink.weparty.profile.ui.photowall

import androidx.fragment.app.activityViewModels
import androidx.recyclerview.widget.RecyclerView
import androidx.recyclerview.widget.StaggeredGridLayoutManager
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.recycleview.adapter.MultiTypeListAdapter
import com.adealink.weparty.profile.R
import com.adealink.weparty.profile.databinding.FragmentPhotoWallBinding
import com.adealink.weparty.profile.ui.photowall.adapter.PhotoItemViewBinder
import com.adealink.weparty.profile.ui.photowall.data.PhotoItemData
import com.adealink.weparty.profile.ui.photowall.data.ProfilePhotoData
import com.adealink.weparty.profile.ui.photowall.itemdecoration.SpacingItemDecoration
import com.adealink.weparty.profile.viewmodel.ProfileViewModel
import com.adealink.weparty.profile.viewmodel.ProfileViewModelFactory
import com.adealink.weparty.util.goImagePreviewActivity

class PhotoWallFragment : BaseFragment(R.layout.fragment_photo_wall) {

    companion object {
        private const val SPAN_COUNT = 2

        private const val PHOTO_CACHE_SIZE = 10
    }

    private val binding by viewBinding(FragmentPhotoWallBinding::bind)
    private val profileViewModel by activityViewModels<ProfileViewModel> { ProfileViewModelFactory() }
    private val listAdapter by fastLazy { MultiTypeListAdapter<PhotoItemData>() }

    private val photos = mutableListOf<ProfilePhotoData>()

    override fun initViews() {
        super.initViews()
        listAdapter.register(PhotoItemViewBinder { data, position ->
            goImgPreview(data, position)
        })
        binding.rvPhotos.layoutManager =
            StaggeredGridLayoutManager(SPAN_COUNT, RecyclerView.VERTICAL)

        binding.rvPhotos.setHasFixedSize(true)

        binding.rvPhotos.itemAnimator = null
        binding.rvPhotos.setItemViewCacheSize(PHOTO_CACHE_SIZE)
        binding.rvPhotos.adapter = listAdapter
        binding.rvPhotos.addItemDecoration(
            SpacingItemDecoration(
                spanCount = SPAN_COUNT,
                spacingH = 9.dp(),
                spacingV = 10.dp()
            )
        )

        listAdapter.submitList(photos.map { PhotoItemData(it) })
    }

    override fun observeViewModel() {
        super.observeViewModel()
        profileViewModel.userInfoLD.observe(viewLifecycleOwner) { userInfo ->
            photos.clear()
            photos.addAll(userInfo?.photos?.map {
                ProfilePhotoData(it)
            } ?: emptyList())
            listAdapter.submitList(photos.map { PhotoItemData(it) })
        }
    }

    override fun onPause() {
        super.onPause()
        // 应用进入后台时清理缓存
        binding.rvPhotos.setItemViewCacheSize(0)
    }

    override fun onResume() {
        super.onResume()
        // 恢复缓存大小
        binding.rvPhotos.setItemViewCacheSize(PHOTO_CACHE_SIZE)
    }

    private fun goImgPreview(item: PhotoItemData, position: Int) {
        val act = activity ?: return
        goImagePreviewActivity(
            activity = act,
            imageUriList = arrayListOf<String>().apply {
                addAll(photos.map { it.url })
            },
            currentIndex = position
        )
    }

}