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

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.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 listAdapter by fastLazy { MultiTypeListAdapter<PhotoItemData>() }

    private val photos = mutableListOf<ProfilePhotoData>().apply {
        // TODO: 测试用图 
        addAll(
            listOf(
                ProfilePhotoData("https://im.sdk.qcloud.com/download/tuikit-resource/community-cover/community_cover_default.png"),
                ProfilePhotoData("https://gd-hbimg-edge.huabanimg.com/18e1e5b28cf5ff377db4082be5886c6d7dc49653af22-qNNo8A_fw1200webp?auth_key=1765512000-9184c35b89a54462bd0d88946c7592c6-0-027aeec13b8ccaed2965fe664a8d7f43"),
                ProfilePhotoData("https://gd-hbimg-edge.huabanimg.com/21789db55e9b888df6b79dacaa48b9f3cf47a42af13e-cngEKs_fw1200?auth_key=1765512000-9184c35b89a54462bd0d88946c7592c6-0-840223cb015b67e528f6f72a31269778"),
                ProfilePhotoData("https://gd-hbimg-edge.huabanimg.com/a90e3901a7aeeed6c3b9d89fc570edafaaa9fab8b8b93-e7K0KX?auth_key=1765526400-17e71cd1259e4e38b2cbc1aeea532fab-0-e63eabe78de13504a0ec1d8c4683aaf9"),
                ProfilePhotoData("https://gd-hbimg-edge.huabanimg.com/f8902bc0513d050c44f63fbe739d75dce674a65434605-iJXDh2_fw480webp?auth_key=1765512000-9184c35b89a54462bd0d88946c7592c6-0-80bfa288b5311adea8277f706059b2e5"),
                ProfilePhotoData("https://gd-hbimg-edge.huabanimg.com/909ca587893946922aca6c048d54d117baf9d9d811fd4-MXbrq7_fw1200?auth_key=1765512000-9184c35b89a54462bd0d88946c7592c6-0-93ee8d59c411a70418e6d39e858d6770"),
                ProfilePhotoData("https://gd-hbimg-edge.huabanimg.com/small/f439c4f1663ad24385c785414997112b1acbd9c2a1096-LjLsoi_fw480webp?auth_key=1765512000-9184c35b89a54462bd0d88946c7592c6-0-dea385ba1a8c64fab1746e8f9895e6fd"),
                ProfilePhotoData("https://gd-hbimg-edge.huabanimg.com/af0dc405a177ca9726729613ab1a163e2fd4969e6d89e3-kjFZfs_fw480webp?auth_key=1765512000-9184c35b89a54462bd0d88946c7592c6-0-3ff4c3dd9da13d47974de75e4d8a0408"),
                ProfilePhotoData("https://gd-hbimg-edge.huabanimg.com/e23678e11bd4284b786eb5bac871db1bc44fbd5f2db89a-tBkueA_fw480webp?auth_key=1765512000-9184c35b89a54462bd0d88946c7592c6-0-fedf31e0bad3ea8daa7a93d2d842f9d6"),
                ProfilePhotoData("https://gd-hbimg-edge.huabanimg.com/8f2e1e1054c42a84974c62ae29a7caabfda81ba0d8496-HHWevQ_fw480webp?auth_key=1765512000-9184c35b89a54462bd0d88946c7592c6-0-b532835de02a97b48bb16878949bd2a6"),
                ProfilePhotoData("https://gd-hbimg-edge.huabanimg.com/070beae99e92c4c862b2f91e920abc549f3402fd8c628-wEvP0F_fw480webp?auth_key=1765512000-9184c35b89a54462bd0d88946c7592c6-0-5c0818110169156467eb28bf7a44ecb5"),
                ProfilePhotoData("https://gd-hbimg-edge.huabanimg.com/b754ef05ca83c5379bbb7c262632d7c05f7e350cd57c9a-LaYrTb_fw480webp?auth_key=1765512000-9184c35b89a54462bd0d88946c7592c6-0-27f287b1ac251189b1df733df47f7a4b"),
                ProfilePhotoData("https://gd-hbimg-edge.huabanimg.com/5be7d808fd754e21e62aea6668379c333ae1cfcaa1df-mZuKL3_fw480webp?auth_key=1765512000-9184c35b89a54462bd0d88946c7592c6-0-f00230b4b9842e8076ed021b861b5e52"),
                ProfilePhotoData("https://gd-hbimg-edge.huabanimg.com/dd65cc7f807797def11a224daebd1fc6e4b9603157cad-vNKOJa_fw480webp?auth_key=1765512000-9184c35b89a54462bd0d88946c7592c6-0-c4045bd6b85d4caa127f0f14810f7a9b"),
                ProfilePhotoData("https://gd-hbimg-edge.huabanimg.com/4def6e640ea4737e1f54a08f3777e3d11cc081aa34e70-hV0bZJ_fw480webp?auth_key=1765526400-17e71cd1259e4e38b2cbc1aeea532fab-0-89ea2e60dbbe49330c96aa2bac32a799"),
                ProfilePhotoData("https://gd-hbimg-edge.huabanimg.com/5b12b76272054c2a34f8a80aab464efaef3686bf10b20-qrmoDy_fw480webp?auth_key=1765526400-17e71cd1259e4e38b2cbc1aeea532fab-0-c53968e241a9749718d4b0a688a77c8a"),
                ProfilePhotoData("https://gd-hbimg-edge.huabanimg.com/small/ef30be3f8517419a49f7bc8fe968677ecaa7eedd7142d-NdwnKY_fw480webp?auth_key=1765526400-17e71cd1259e4e38b2cbc1aeea532fab-0-f93953bac6e93223b5d6cd18b2f35e3c"),
                ProfilePhotoData("https://gd-hbimg-edge.huabanimg.com/9f43114a86ce1c5d29cf3cf006bff83ade4f4ae6d4abc-RWXLlW_fw1200webp?auth_key=1765526400-17e71cd1259e4e38b2cbc1aeea532fab-0-66fa9397ddbff3820d6bfaf18a47a62f"),
                ProfilePhotoData("https://gd-hbimg-edge.huabanimg.com/2b2aaf5d9fa2f038be14a7c42dae7a51f57c666f3bd7ea-BShVX5_fw480webp?auth_key=1765526400-17e71cd1259e4e38b2cbc1aeea532fab-0-8b4ec49b3848811ebcf79860296b570d"),
                ProfilePhotoData("https://gd-hbimg-edge.huabanimg.com/eea81430eed8e1357186451114c13d0b7959b49a62e8-NOGOED_fw480webp?auth_key=1765526400-17e71cd1259e4e38b2cbc1aeea532fab-0-d8c04e8af77f5e450b20c334f37bbe85"),
                ProfilePhotoData("https://gd-hbimg-edge.huabanimg.com/1f42990dfd2bceae6750b61e3e092eef3065509814774-BYU1dm_fw480webp?auth_key=1765512000-9184c35b89a54462bd0d88946c7592c6-0-5be113f9345f5dda8c638aab7953c874"),
                ProfilePhotoData("https://gd-hbimg-edge.huabanimg.com/5ece5eb3c0bf1d282c637b1af4a8dbcff99d76d31de99-qGeye6_fw480webp?auth_key=1765512000-9184c35b89a54462bd0d88946c7592c6-0-8617e80f4e96e647376044795d5af934"),
            )
        )
    }

    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 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
        )
    }

}