package com.adealink.weparty.profile.dynamic.fragment.photo.adapter

import android.annotation.SuppressLint
import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.core.graphics.toColorInt
import androidx.viewpager.widget.ViewPager
import com.adealink.frame.image.view.NetworkImageView
import com.adealink.frame.util.DisplayUtil
import com.adealink.frame.util.formatTime
import com.adealink.frame.util.onClick
import com.adealink.weparty.commonui.ext.dp
import com.adealink.weparty.commonui.ext.getActivity
import com.adealink.weparty.commonui.ext.gone
import com.adealink.weparty.commonui.ext.show
import com.adealink.weparty.commonui.recycleview.adapter.BindingViewHolder
import com.adealink.weparty.commonui.recycleview.adapter.multitype.ItemViewBinder
import com.adealink.weparty.commonui.viewpager.RecyclePagerAdapter
import com.adealink.weparty.profile.R
import com.adealink.weparty.profile.databinding.LayoutDynamicDetailPhotoBinding
import com.adealink.weparty.profile.databinding.LayoutDynamicPhotoHeaderBinding
import com.adealink.weparty.profile.dynamic.data.DynamicMediaData
import com.adealink.weparty.profile.dynamic.fragment.data.DynamicDetailPhotoHeaderItem
import com.adealink.weparty.profile.dynamic.util.updatePhotoMediaViewLp
import com.adealink.weparty.util.TIME_FORMAT_DMY_HMS
import com.adealink.weparty.util.goImagePreviewActivity


class DynamicPhotoHeaderViewBinder() :
    ItemViewBinder<DynamicDetailPhotoHeaderItem, DynamicPhotoHeaderViewBinder.ViewHolder>() {

    override fun onCreateViewHolder(
        inflater: LayoutInflater,
        parent: ViewGroup
    ): ViewHolder {
        return ViewHolder(LayoutDynamicPhotoHeaderBinding.inflate(inflater, parent, false)).also {
            it.initView()
        }
    }

    @SuppressLint("SetTextI18n")
    override fun onBindViewHolder(
        holder: ViewHolder,
        item: DynamicDetailPhotoHeaderItem
    ) {
        holder.update(item)
    }

    inner class ViewHolder(
        rootBinding: LayoutDynamicPhotoHeaderBinding,
    ) : BindingViewHolder<LayoutDynamicPhotoHeaderBinding>(rootBinding) {

        private var item: DynamicDetailPhotoHeaderItem? = null
        private val viewAdapter = ViewAdapter()
        fun initView() {
            binding.vp.pageMargin = 8.dp()
            binding.vp.offscreenPageLimit = 3
            binding.vp.adapter = viewAdapter
            binding.vp.addOnPageChangeListener(object : ViewPager.OnPageChangeListener {
                override fun onPageScrolled(
                    position: Int,
                    positionOffset: Float,
                    positionOffsetPixels: Int
                ) {
                    binding.vpIndicator.onPageScrolled(
                        position,
                        positionOffset,
                        positionOffsetPixels
                    )
                }

                override fun onPageSelected(position: Int) {
                    binding.vpIndicator.onPageSelected(position)
                }

                override fun onPageScrollStateChanged(state: Int) {
                    binding.vpIndicator.onPageScrollStateChanged(state)
                }
            })
            binding.vpIndicator.config.apply {
                setIndicatorSize(4.dp())
                setIndicatorSpace(6.dp())
                setNormalWidth(4.dp())
                setSelectedWidth(4.dp())
                setNormalColor("#C8C9CB".toColorInt())
                setSelectedColor("#4E5969".toColorInt())
            }
        }

        fun update(item: DynamicDetailPhotoHeaderItem) {
            this.item = item
            val photoList = item.data.getPhotoMedia()
            if (photoList.isNullOrEmpty()) {
                binding.vp.gone()
                binding.vpIndicator.gone()
            } else {
                binding.vp.show()
                viewAdapter.setItemList(photoList) { size ->
                    binding.vpIndicator.onPageChanged(size, 0)
                    binding.vpIndicator.show(size > 1)
                }
                val firstPhoto = photoList.first()
                updatePhotoMediaViewLp(
                    binding.vp,
                    firstPhoto.getPhotoData(),
                    horizontalWidth = DisplayUtil.getScreenWidth(),
                    verticalWidth = DisplayUtil.getScreenWidth()
                )
            }
            binding.tvContent.show(!item.data.textContent.isNullOrEmpty())
            binding.tvContent.text = item.data.textContent
            binding.tvTime.text = formatTime(item.data.createdAt, TIME_FORMAT_DMY_HMS)
        }

        internal inner class PreviewViewHolder(val binding: LayoutDynamicDetailPhotoBinding) :
            RecyclePagerAdapter.PagerViewHolder(binding.root)

        internal inner class ViewAdapter : RecyclePagerAdapter<PreviewViewHolder>() {

            private val itemList = mutableListOf<DynamicMediaData>()

            fun setItemList(
                list: List<DynamicMediaData>?,
                commited: ((itemSize: Int) -> Unit)? = null
            ) {
                itemList.clear()
                itemList.addAll(list ?: emptyList())
                notifyDataSetChanged()
                commited?.invoke(list?.size ?: 0)
            }

            override fun getItemViewType(position: Int): Int {
                return 1
            }

            override fun getItemCount(): Int {
                return itemList.size
            }

            override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): PreviewViewHolder {
                val binding = LayoutDynamicDetailPhotoBinding.inflate(
                    LayoutInflater.from(parent.context),
                    parent,
                    false
                )
                return PreviewViewHolder(binding)
            }

            override fun onBindViewHolder(holder: PreviewViewHolder, position: Int) {
                val data = itemList.getOrNull(position) ?: return
                holder.itemView.findViewById<NetworkImageView>(R.id.iv_img)?.apply {
                    setImageUrl(data.url)
                    onClick {
                        previewImage(position)
                    }
                }
            }

            private fun previewImage(position: Int) {
                val imageList = itemList.map { it.url }
                val act = binding.root.getActivity() ?: return
                goImagePreviewActivity(
                    act,
                    arrayListOf<String>().apply {
                        addAll(imageList)
                    },
                    currentIndex = position
                )
            }
        }

    }


}
