package com.yidian.shenghuoquan.newscontent.ui

import android.annotation.SuppressLint
import android.os.Bundle
import android.view.View
import androidx.viewpager2.widget.ViewPager2
import com.yidian.common.XRouterPathConstants
import com.yidian.common.XRouterPathConstants.Companion.GALLERY
import com.yidian.common.base.BaseActivity
import com.yidian.common.cardutils.adapter.OnItemClickListener
import com.yidian.shenghuoquan.newscontent.R
import com.yidian.shenghuoquan.newscontent.adapter.CommenViewPagerAdapter
import com.yidian.shenghuoquan.newscontent.bean.ImageItemBean
import com.yidian.shenghuoquan.newscontent.databinding.ActivityGalleryBinding
import com.yidian.shenghuoquan.newscontent.ui.dialog.ShareImageDialog


class GalleryActivity: BaseActivity<ActivityGalleryBinding>() {
    private val vpAdapter: CommenViewPagerAdapter = CommenViewPagerAdapter()
    private var imageTotalNum: Int = 0
    private lateinit var articleUrl: String
    private lateinit var imageUrl: String
    private var shareImageDialog: ShareImageDialog? = null
    private lateinit var imageList: ArrayList<ImageItemBean>

    override fun getXPageName(): String {
        return GALLERY
    }

    override fun createViewBinding(): ActivityGalleryBinding {
        return ActivityGalleryBinding.inflate(layoutInflater)
    }

    @SuppressLint("SetTextI18n")
    override fun init(savedInstanceState: Bundle?) {
        super.init(savedInstanceState)
        viewBind.vpImage.adapter = vpAdapter
        if(intent.getSerializableExtra(XRouterPathConstants.ParamsKey) != null){
            val paramsMap = intent.getSerializableExtra(XRouterPathConstants.ParamsKey) as HashMap<*, *>
            val index = paramsMap["index"] as Int
            imageList = paramsMap["imageList"] as ArrayList<ImageItemBean>
            articleUrl = paramsMap["articleUrl"] as String
            imageTotalNum = imageList.size
            if(imageTotalNum > 0){
                imageUrl = imageList[index].imageUrl
                vpAdapter.update(imageList)
                val showIndex = index + 1
                viewBind.tvPage.text = "$showIndex/$imageTotalNum"
                viewBind.vpImage.setCurrentItem(index, false)
            }
            setClickListener()
        }
    }

    private fun setClickListener(){
        viewBind.vpImage.registerOnPageChangeCallback(object : ViewPager2.OnPageChangeCallback() {
            @SuppressLint("SetTextI18n")
            override fun onPageSelected(position: Int) {
                val currentPage = position + 1
                viewBind.tvPage.text = "$currentPage/$imageTotalNum"
                imageUrl = imageList[position].imageUrl
            }
        })
        vpAdapter.setOnItemClickListener(object : OnItemClickListener<ImageItemBean> {
            override fun onClick(view: View?, position: Int, item: ImageItemBean?) {
                finish()
            }
        })
        viewBind.ivShare.setOnClickListener {
            shareImage(imageUrl)
        }
    }

    private fun shareImage(imageUrl: String) {
        if(shareImageDialog == null){
            shareImageDialog = ShareImageDialog(this, R.style.share_dialog)
        }
        shareImageDialog!!.setData(imageUrl)
        shareImageDialog!!.show()
    }
}