/*
package com.remax.visualnovel.widget.imageviewer.viewer

import android.animation.Animator
import android.annotation.SuppressLint
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.core.view.isInvisible
import androidx.core.view.isVisible
import androidx.fragment.app.FragmentActivity
import androidx.lifecycle.DefaultLifecycleObserver
import androidx.lifecycle.LifecycleOwner
import androidx.lifecycle.ViewModelProvider
import androidx.recyclerview.widget.RecyclerView
import androidx.viewpager2.adapter.FragmentStateAdapter
import com.remax.visualnovel.R
import com.remax.visualnovel.app.widget.tips.TipsMoreWindow
import com.remax.visualnovel.databinding.LayoutIndicatorBinding
import com.remax.visualnovel.entity.imbean.raw.CustomAlbumData
import com.remax.visualnovel.entity.request.ChatAlbum
import com.remax.visualnovel.entity.response.Album
import com.remax.visualnovel.entity.response.AppearanceImage
import com.remax.visualnovel.entity.response.ChatBackground
import com.remax.visualnovel.entity.response.Wallet
import com.remax.visualnovel.extension.changeLikedStatus
import com.remax.visualnovel.extension.formatPrice
import com.remax.visualnovel.extension.setMargin
import com.remax.visualnovel.extension.setOnClick
import com.remax.visualnovel.extension.showDoubleBtnDialog
import com.remax.visualnovel.extension.showSingleBtnDialog
import com.remax.visualnovel.manager.login.LoginManager
import com.remax.visualnovel.ui.chat.ChatActivity
import com.remax.visualnovel.ui.chat.message.setting.background.ChatBackgroundActivity
import com.remax.visualnovel.ui.main.MainActivity
import com.remax.visualnovel.ui.main.create.CreateActivity
import com.remax.visualnovel.ui.main.create.image.ImageGenerateUtil
import com.remax.visualnovel.ui.main.foryou.ForYouFragment
import com.remax.visualnovel.ui.profile.CharacterProfileActivity
import com.remax.visualnovel.ui.profile.album.AlbumFragment
import com.remax.visualnovel.ui.profile.album.create.AlbumCreateActivity
import com.remax.visualnovel.ui.profile.album.um.UnlockMethodActivity
import com.remax.visualnovel.utils.EpalUtils
import com.remax.visualnovel.utils.StatusBarUtils
import com.remax.visualnovel.utils.spannablex.utils.dp
import com.remax.visualnovel.widget.imageviewer.ImageViewerActionViewModel
import com.remax.visualnovel.widget.imageviewer.ImageViewerBuilder
import com.remax.visualnovel.widget.imageviewer.adapter.ItemType
import com.remax.visualnovel.widget.imageviewer.core.ImageLoader
import com.remax.visualnovel.widget.imageviewer.core.OverlayCustomizer
import com.remax.visualnovel.widget.imageviewer.core.Photo
import com.remax.visualnovel.widget.imageviewer.core.VHCustomizer
import com.remax.visualnovel.widget.imageviewer.core.ViewerCallback
import com.remax.visualnovel.widget.imageviewer.utils.Config
import com.remax.visualnovel.widget.imageviewer.widgets.PhotoView2
import com.remax.visualnovel.widget.imageviewer.widgets.video.ExoVideoView
import com.remax.visualnovel.widget.ui.lock.getLockLabel
import com.pengxr.modular.eventbus.generated.events.EventDefineOfAlbumEvents
import com.pengxr.modular.eventbus.generated.events.EventDefineOfWalletEvents
import io.reactivex.disposables.Disposable
import timber.log.Timber

*/
/**
 * viewer 自定义业务&UI
 *//*

class MyViewerCustomizer : DefaultLifecycleObserver, VHCustomizer, OverlayCustomizer, ViewerCallback {
    private var activity: FragmentActivity? = null
    private var viewerActions: ImageViewerActionViewModel? = null
    private var videoTask: Disposable? = null
    private var lastVideoVH: RecyclerView.ViewHolder? = null
    private var indicatorBinding: LayoutIndicatorBinding? = null
    private var currentPosition = -1
    private var currData: Photo? = null
    private var imageLoader: ImageLoader? = null

    private val albumLikeFailedObserver = androidx.lifecycle.Observer<Album?> {
        it?.let {
            changeAlbumStatus(it, true)
        }
    }

    private val walletObserver = androidx.lifecycle.Observer<Wallet?> {
        changeWallet()
    }

    */
/**
     * 对viewer进行自定义封装. 添加自定义指示器.video绑定.图片说明等自定义配置
     *//*

    fun process(activity: FragmentActivity, builder: ImageViewerBuilder) {
        this.activity = activity
        viewerActions = ViewModelProvider(activity)[ImageViewerActionViewModel::class.java]
        activity.lifecycle.addObserver(this)
        builder.setVHCustomizer(this)
        builder.setOverlayCustomizer(this)
        builder.setViewerCallback(this)
        imageLoader = builder.imageLoader

        EventDefineOfAlbumEvents.albumLikeFailed().observe(activity, albumLikeFailedObserver)
        EventDefineOfWalletEvents.buffBalanceUpdateSucceeded().observe(activity, walletObserver)
    }

    override fun initialize(type: Int, viewHolder: RecyclerView.ViewHolder) {
        (viewHolder.itemView as? ViewGroup?)?.let {
            it.addView(LayoutInflater.from(it.context).inflate(R.layout.item_photo_custom_layout, it, false))
        }

        when (type) {
            ItemType.SUBSAMPLING -> {
                viewHolder.itemView.findViewById<View>(R.id.subsamplingView)?.run {
                    setOnClickListener {
                        viewerActions?.dismiss()
                    }
                }
            }

            ItemType.PHOTO -> {
                viewHolder.itemView.run {
                    findViewById<PhotoView2>(R.id.photoView)?.run {
                        maximumScale = 8.0f
//                        postDelayed({
//                            scale = 2.0f
//                            setScale(2.0f, false)
//                        }, 1000)

                        setOnClickListener { viewerActions?.dismiss() }
                    }
                }
            }

            ItemType.VIDEO -> {

            }
        }
    }

    override fun bind(type: Int, data: Photo, position: Int, viewHolder: RecyclerView.ViewHolder) {

    }

    override fun provideView(parent: ViewGroup): View {
        indicatorBinding = LayoutIndicatorBinding.inflate(LayoutInflater.from(parent.context), parent, false)
        indicatorBinding?.run {
            activity?.run {
                indicatorLayout.setMargin(topMargin = StatusBarUtils.statusBarHeight)
                val bottomMargin = (maxOf(StatusBarUtils.getNavBarHeight(), 20.dp) + 16.dp)
                listOf(
                    albumLikeLayout,
                    previewSetView,
                    selectLayout,
                    albumLikeLayout,
                    chatBgSetLayout,
                    chatBgSelect
                ).forEach {
                    it.setMargin(bottomMargin = bottomMargin)
                }
            }
            lockViewGroup.setMyBalance()

            setOnClick(albumLikeLayout, navBack) {
                when (this) {
                    navBack -> {
                        viewerActions?.dismiss()
                    }

                    albumLikeLayout -> {
                        (currData as? Album)?.let { data ->
                            LoginManager.checkLogin {
                                EventDefineOfAlbumEvents.albumLike().post(data)
                                changeAlbumStatus(data)
                            }
                        }
                    }
                }
            }
        }
        return indicatorBinding!!.root
    }

    override fun onDrag(viewHolder: RecyclerView.ViewHolder, view: View, fraction: Float) {
//        viewHolder.itemView.findViewById<ViewGroup>(R.id.customizeDecor)?.findViewById<View>(R.id.lockViewGroup)?.alpha = 0f
    }

    override fun onRestore(viewHolder: RecyclerView.ViewHolder, view: View, fraction: Float) {
//        viewHolder.itemView.findViewById<ViewGroup>(R.id.customizeDecor)?.findViewById<View>(R.id.lockViewGroup)?.alpha = 1f
    }

    override fun onRelease(viewHolder: RecyclerView.ViewHolder, view: View) {
        viewHolder.itemView.findViewById<ViewGroup>(R.id.customizeDecor)
            ?.animate()?.setDuration(Config.DURATION_TRANSITION)?.alpha(0f)?.start()
        indicatorBinding?.indicatorDecor?.animate()?.setDuration(Config.DURATION_TRANSITION)?.alpha(0f)?.start()
        release()
    }

    @SuppressLint("SetTextI18n")
    override fun onPageSelected(position: Int, item: Photo?, totalCount: Int, viewHolder: RecyclerView.ViewHolder) {
        currData = item

        fun setTotalIndicator(total: Int) {
            Timber.d("Image preview totalCount:$total")
            indicatorBinding?.indicator?.text = (position + 1).toString() + "/" + total
            indicatorBinding?.indicator?.isVisible = total > 1
        }

        when (item) {
            //看相册
            is Album -> {
                if (item.userId == null) {
                    item.userId = (activity as? CharacterProfileActivity)?.characterProfileViewModel?.character?.userId
                }
                val isMyself = LoginManager.isMyself(item.userId)
                indicatorBinding?.also {
                    it.navMore.isVisible = isMyself
                    setOnClick(it.navMore) {
                        TipsMoreWindow().build(
                            context,
                            listOf(TipsMoreWindow.TipsMoreUIData(R.string.delete, R.string.icon_delete)),
                            clickCallback = {
                                (currData as? Album)?.let { data ->
                                    activity?.run {
                                        if (data.isDefault == true) {
                                            showSingleBtnDialog(text = getString(R.string.dialog_delete_warning_default))
                                        } else {
                                            this.showDoubleBtnDialog(
                                                text = getString(R.string.dialog_delete_warning),
                                                topBtnClick = {
                                                    val target = listOf(data)
                                                    viewerActions?.remove(target)
                                                    (activity as? CharacterProfileActivity)?.let { act ->
                                                        (act.binding.viewPager.adapter as? FragmentStateAdapter)?.let { adapter ->
                                                            (adapter.createFragment(1) as? AlbumFragment)?.deleteAlbum(
                                                                data
                                                            )
                                                        }
                                                    }
                                                },
                                                topBtnText = getString(R.string.delete)
                                            )
                                        }

                                    }
                                }
                            }).showAsDropDown(it.navMore, 0, 8.dp)

                    }

                    //自己看自己的可修改
                    with(it.previewSetView) {
                        isVisible = isMyself
                        if (isMyself) {
                            setBlur()
                            setUI(item)
                            (activity as? CharacterProfileActivity)?.let { act ->
                                setCallback(
                                    {
                                        if ((item.unlockPrice ?: 0L) > 0) {
                                            act.showDoubleBtnDialog(
                                                text = act.getString(R.string.dialog_set_default_warning),
                                                topBtnClick = {
                                                    act.setDefaultAlbum(item) {
                                                        it.lockView.isVisible = false
                                                        setUI(item)
                                                    }
                                                },
                                                topBtnText = act.getString(R.string.confirm)
                                            )
                                        } else {
                                            act.setDefaultAlbum(item) {
                                                setUI(item)
                                            }
                                        }
                                    }
                                ) {
                                    UnlockMethodActivity.start(item, act) { unlockPrice ->
                                        item.unlockPrice = unlockPrice
                                        act.setAlbumPrice(item)
                                        setUI(item)
                                    }
                                }
                            }
                        }
                    }


                    //先关闭点赞动画
                    if (it.albumLikeLottie.isAnimating) {
                        it.albumLikeLottie.cancelAnimation()
                    }
                    it.albumLikeLottie.isVisible = false
                    it.albumLikeIcon.changeLikedStatus(item.isLike())
                    it.albumLikeNum.text = EpalUtils.formatNumberAutoSize(item.likedCount)
                    it.lockView.setLockLabel(item.userId.getLockLabel())

                    it.lockViewGroup.isVisible = false
                    it.lockView.isVisible = false
                    it.albumLikeLayout.isVisible = false

                    when {
                        //公开图片
                        item.isOpen() -> {
                            it.albumLikeLayout.isVisible = !isMyself
                        }
                        //私密图片
                        else -> {
                            //解锁可看可点赞 自己不能给自己的点
                            it.albumLikeLayout.isVisible = item.isUnLock() && !isMyself
                            it.lockView.isVisible = item.isUnLock()
                            //未解锁需要给钱
                            it.lockViewGroup.isVisible = !item.isUnLock()
                            it.lockViewGroup.setPreviewUnlockInfo(item.unlockPrice ?: 0) {
                                //解锁当前这张照片
                                when (activity) {
                                    is CharacterProfileActivity -> {
                                        (activity as? CharacterProfileActivity)?.let { act ->
                                            (act.binding.viewPager.adapter as? FragmentStateAdapter)?.let { adapter ->
                                                (adapter.createFragment(1) as? AlbumFragment)?.unlockAlbumImage(
                                                    item.albumId,
                                                    item.unlockPrice
                                                ) {
                                                    it.lockViewGroup.isVisible = false
                                                    it.lockView.isVisible = true
                                                    it.albumLikeLayout.isVisible = true
                                                    (imageLoader as? MyImageLoader)?.loadImg(
                                                        viewHolder.itemView,
                                                        item.img3,
                                                        false
                                                    )
                                                }
                                            }
                                        }
                                    }

                                    is MainActivity -> {
                                        (activity as? MainActivity)?.let { act ->
                                            (act.binding.viewPager2.adapter as? FragmentStateAdapter)?.let { adapter ->
                                                (adapter.createFragment(0) as? ForYouFragment)?.unlockAlbumImage(
                                                    ChatAlbum(item.aiId ?: "", item.albumId, item.unlockPrice)
                                                ) {
                                                    it.lockViewGroup.isVisible = false
                                                    it.lockView.isVisible = true
                                                    it.albumLikeLayout.isVisible = true
                                                    (imageLoader as? MyImageLoader)?.loadImg(
                                                        viewHolder.itemView,
                                                        item.img3,
                                                        false
                                                    )
                                                }
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }

            //AI创作选择
            is AppearanceImage -> {
                indicatorBinding?.also {
                    when {
                        activity is AlbumCreateActivity && ((activity as? AlbumCreateActivity)?.generateScene == ImageGenerateUtil.ALBUM) -> {
                            it.albumBottomLayout.isVisible = true
                            it.albumCheckBox.viewChecked(item.select)
                            fun setPrice() {
                                it.albumPriceView.setPrice(formatPrice(item.unlockPrice))
                                it.albumPriceView.isVisible = item.unlockPrice > 0
                                it.albumFree.isVisible = !it.albumPriceView.isVisible
                            }
                            setPrice()
                            setOnClick(it.albumPrice, it.albumSelectLayout) {
                                when (this) {
                                    it.albumPrice -> {
                                        (activity as? AlbumCreateActivity)?.let { act ->
                                            act.imageGenerateUtil.setPrice(item, act) {
                                                setPrice()
                                            }
                                        }
                                    }
                                    */
/**
                                     * 相册单选
                                     *//*

                                    it.albumSelectLayout -> {
                                        if (!item.select) {
                                            it.albumCheckBox.viewChecked(true)
                                            (activity as? AlbumCreateActivity)?.imageGenerateUtil?.setSelect(item)
                                        }
                                    }
                                }
                            }
                        }

                        */
/**
                         * 其他是单选
                         *//*

                        else -> {
                            it.selectLayout.isVisible = true
                            it.checkBox.viewChecked(item.select)
                            setOnClick(it.selectLayout) {
                                if (!item.select) {
                                    it.checkBox.viewChecked(true)
                                    it.checkBox.post {
                                        //创建/编辑AI
                                        (activity as? CreateActivity)?.imageGenerateUtil?.setSelect(item)
                                        //IM聊天创作背景
                                        (activity as? AlbumCreateActivity)?.imageGenerateUtil?.setSelect(item)
                                    }
                                }
                            }
                        }
                    }


                }
            }

            //聊天chat背景图
            is ChatBackground -> {
                indicatorBinding?.also {
                    it.navMore.isVisible = !item.isDefault
                    it.chatBgSelect.isVisible = true
                    it.chatBgSelect.isEnabled = item.imgUrl != (activity as? ChatBackgroundActivity)?.backgroundImg
                    setOnClick(it.chatBgSelect, it.navMore) {
                        when (this) {
                            it.chatBgSelect -> {
                                (activity as? ChatBackgroundActivity)?.let { act ->
                                    act.setBackground(item) {
                                        viewerActions?.dismiss()
                                    }
                                }
                            }

                            it.navMore -> {
                                TipsMoreWindow().build(
                                    context,
                                    listOf(TipsMoreWindow.TipsMoreUIData(R.string.delete, R.string.icon_delete)),
                                    clickCallback = {
                                        (activity as? ChatBackgroundActivity)?.let { act ->
                                            act.removeBackground(item) {
                                                val target = listOf(item)
                                                viewerActions?.remove(target)
                                            }
                                        }
                                    }).showAsDropDown(it.navMore, 0, 8.dp)

                            }
                        }

                    }
                }
            }

            //IM列表图片
            is CustomAlbumData -> {
                indicatorBinding?.also {
                    it.lockViewGroup.isVisible = item.unlockPrice != 0L
                    it.lockViewGroup.setPreviewUnlockInfo(item.unlockPrice) {
                        //解锁当前这张照片
                        (activity as? ChatActivity)?.let { act ->
                            act.unlockAlbumImage(item.albumId, item.unlockPrice, item.messageServerId) { album ->
                                it.lockViewGroup.isVisible = false
                                item.unlockPrice = 0L
                                item.url = album?.img3 ?: ""
                                (imageLoader as? MyImageLoader)?.loadImg(viewHolder.itemView, item.url, false)
                            }
                        }
                    }
                }
            }
        }

        currentPosition = position
        setTotalIndicator(totalCount)
    }

    override fun onResume(owner: LifecycleOwner) {
        lastVideoVH?.itemView?.findViewById<ExoVideoView>(R.id.videoView)?.resume()
    }

    override fun onPause(owner: LifecycleOwner) {
        lastVideoVH?.itemView?.findViewById<ExoVideoView>(R.id.videoView)?.pause()
    }

    override fun onDestroy(owner: LifecycleOwner) {
        lastVideoVH?.itemView?.findViewById<ExoVideoView>(R.id.videoView)?.release()
        videoTask?.dispose()
        videoTask = null
    }

    private fun changeWallet() {
        indicatorBinding?.lockViewGroup?.setMyBalance()
    }

    private fun changeAlbumStatus(data: Album, fromFailed: Boolean = false) {
        indicatorBinding?.run {
            if (data.isLike()) {
                if (fromFailed) {
                    albumLikeLottie.isVisible = false
                    albumLikeIcon.changeLikedStatus(true)
                } else {
                    albumLikeIcon.isInvisible = true
                    albumLikeLottie.run {
                        isVisible = true
                        progress = 0f
                        addAnimatorListener(object : Animator.AnimatorListener {
                            override fun onAnimationStart(p0: Animator) {
                                isEnabled = false
                            }

                            override fun onAnimationEnd(p0: Animator) {
                                isEnabled = true
                                progress = 1f
                            }

                            override fun onAnimationCancel(p0: Animator) {

                            }

                            override fun onAnimationRepeat(p0: Animator) {

                            }
                        })
                        playAnimation()
                    }
                }
            } else {
                albumLikeLottie.isVisible = false
                albumLikeIcon.changeLikedStatus(false)
            }
            albumLikeNum.text = EpalUtils.formatNumberAutoSize(data.likedCount)
        }
    }

    private fun release() {
        activity?.lifecycle?.removeObserver(this)
        activity = null
        videoTask?.dispose()
        videoTask = null
        lastVideoVH = null
        indicatorBinding = null
        EventDefineOfAlbumEvents.albumLikeFailed().removeObserver(albumLikeFailedObserver)
        EventDefineOfWalletEvents.buffBalanceUpdateSucceeded().removeObserver(walletObserver)
    }

}


*/
