package com.adealink.weparty.image.preview

import android.annotation.SuppressLint
import androidx.fragment.app.Fragment
import androidx.fragment.app.FragmentManager
import androidx.fragment.app.FragmentPagerAdapter
import androidx.viewpager.widget.ViewPager
import com.adealink.frame.mvvm.view.viewBinding
import com.adealink.frame.router.Router
import com.adealink.frame.router.annotation.BindExtra
import com.adealink.frame.router.annotation.RouterUri
import com.adealink.weparty.commonui.BaseActivity
import com.adealink.weparty.image.databinding.ActivityVideoPreviewBinding
import com.adealink.weparty.module.image.Video
import com.adealink.weparty.module.image.data.VideoItem
import com.qmuiteam.qmui.widget.util.QMUIStatusBarHelper
import com.adealink.weparty.R as APP_R

@RouterUri(path = [Video.Preview.PATH], desc = "视频预览页")
class VideoPreviewActivity : BaseActivity() {

    companion object {
        private const val TAG = "VideoPreviewActivity"
    }

    @BindExtra(name = Video.Preview.KEY_VIDEO_LIST, desc = "url列表")
    var videoList: ArrayList<VideoItem>? = null

    @BindExtra(name = Video.Preview.KEY_CURRENT_INDEX, desc = "当前索引")
    var currentIndex = 0

    @BindExtra(name = Video.Preview.KEY_OWNER_UID, desc = "所有者uid")
    var uid = 0L

    private val binding by viewBinding(ActivityVideoPreviewBinding::inflate)
    private val videos: List<VideoItem>
        get() = videoList ?: arrayListOf()

    private lateinit var adapter: PreviewFragmentAdapter

    override fun onBeforeCreate() {
        super.onBeforeCreate()
        Router.bind(this)
    }

    @SuppressLint("SetTextI18n")
    override fun initViews() {
        QMUIStatusBarHelper.setStatusBarDarkMode(this)
        setNavigationBarColor(APP_R.color.black)
        setContentView(binding.root)
        binding.back.setOnClickListener {
            finish()
        }
        adapter = PreviewFragmentAdapter(supportFragmentManager)
        binding.vpVideos.adapter = adapter
        binding.vpVideos.addOnPageChangeListener(object : ViewPager.OnPageChangeListener {
            override fun onPageScrolled(
                position: Int,
                positionOffset: Float,
                positionOffsetPixels: Int,
            ) {
            }

            @SuppressLint("SetTextI18n")
            override fun onPageSelected(position: Int) {
                binding.title.text = "${position + 1}/${videos.size}"
            }

            override fun onPageScrollStateChanged(state: Int) {}
        })
        if (currentIndex == 0) {
            binding.title.text = "1/${videos.size}"
        }
        binding.vpVideos.currentItem = currentIndex
//        binding.reportTv.visibility =
//            if (uid == 0L || uid == ProfileModule.getMyUid()) View.GONE else View.VISIBLE
//        binding.reportTv.setOnClickListener {
//            UserProtectModule.showReportUserDialog(uid, ReportFrom.IMAGE_PREVIEW)
//        }
    }

    inner class PreviewFragmentAdapter(fm: FragmentManager) :
        FragmentPagerAdapter(fm, BEHAVIOR_RESUME_ONLY_CURRENT_FRAGMENT) {
        override fun getItem(position: Int): Fragment {
            return VideoPreviewFragment.newInstance(videos[position])
        }

        override fun getCount(): Int {
            return videos.size
        }
    }

}