package com.adealink.weparty.image.preview

import android.annotation.SuppressLint
import androidx.activity.OnBackPressedCallback
import androidx.fragment.app.Fragment
import androidx.fragment.app.FragmentManager
import androidx.fragment.app.FragmentPagerAdapter
import androidx.viewpager.widget.ViewPager.OnPageChangeListener
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.ActivityImagePreviewBinding
import com.adealink.weparty.module.image.Image
import com.adealink.weparty.util.QMUIStatusBarHelper
import com.adealink.weparty.R as APP_R

@RouterUri(path = [Image.Preview.PATH], desc = "图片预览页")
class ImagePreviewActivity : BaseActivity() {
    companion object {
        private const val TAG = "ImagePreviewActivity"
    }

    @BindExtra(name = Image.Preview.KEY_IMAGE_URI_LIST, desc = "图片url列表")
    var imageUriList: ArrayList<String>? = null

    @BindExtra(name = Image.Preview.KEY_CURRENT_INDEX, desc = "展示的图片索引")
    var currentIndex = 0

    @BindExtra(name = Image.Preview.KEY_OWNER_UID, desc = "图片所有者uid")
    var uid: String = ""

    private val binding by viewBinding(ActivityImagePreviewBinding::inflate)
    private val images: List<String>
        get() = imageUriList ?: arrayListOf()
    private lateinit var adapter: ImagePreviewFragmentAdapter

    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 = ImagePreviewFragmentAdapter(supportFragmentManager)
        binding.vpImages.adapter = adapter
        binding.vpImages.addOnPageChangeListener(object : OnPageChangeListener {
            override fun onPageScrolled(
                position: Int,
                positionOffset: Float,
                positionOffsetPixels: Int,
            ) {
            }

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

            override fun onPageScrollStateChanged(state: Int) {}
        })
        if (currentIndex == 0) {
            binding.title.text = "1/${images.size}"
        }
        binding.vpImages.currentItem = currentIndex
        onBackPressedDispatcher.addCallback(object : OnBackPressedCallback(true) {
            override fun handleOnBackPressed() {
                finish()
            }
        })
    }

    inner class ImagePreviewFragmentAdapter(fm: FragmentManager) :
        FragmentPagerAdapter(fm, BEHAVIOR_RESUME_ONLY_CURRENT_FRAGMENT) {
        override fun getItem(position: Int): Fragment {
            return ImagePreviewFragment.newInstance(images[position])
        }

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