package com.yidian.yac.userinfo.ui

import android.annotation.SuppressLint
import android.app.Activity
import android.graphics.drawable.ColorDrawable
import android.view.*
import android.widget.PopupWindow
import android.widget.TextView
import androidx.viewpager2.widget.ViewPager2
import com.yidian.yac.userinfo.R
import com.yidian.yac.userinfo.adapter.CommenViewPagerAdapter
import com.yidian.yac.userinfo.adapter.OnItemClickListener
import com.yidian.yac.userinfo.bean.ImageItemBean

class ImagePopupWindow(private val activity: Activity) {
    private val view: View = LayoutInflater.from(activity).inflate(R.layout.image_popup_window, null)
    private val mPopUpWindow: PopupWindow = PopupWindow(view)
    private val vpImage = view.findViewById<ViewPager2>(R.id.vp_image)
    private val tvPage = view.findViewById<TextView>(R.id.tv_page)
    private val vpAdapter: CommenViewPagerAdapter = CommenViewPagerAdapter()
    private var imageTotalNum: Int = 0

    init {
        vpImage.adapter = vpAdapter
        mPopUpWindow.isFocusable = false
        mPopUpWindow.isTouchable = true
        mPopUpWindow.isOutsideTouchable = true
        setClickListener()
    }

    fun updateImageList(imageList: ArrayList<ImageItemBean>){
        imageTotalNum = imageList.size
        if(imageTotalNum > 0){
            vpAdapter.update(imageList)
            tvPage.text = "1/$imageTotalNum"
        }
    }

    private fun setClickListener(){
        vpImage.registerOnPageChangeCallback(object : ViewPager2.OnPageChangeCallback() {
            @SuppressLint("SetTextI18n")
            override fun onPageSelected(position: Int) {
                var currentPage = position + 1
                tvPage.text = "$currentPage/$imageTotalNum"
            }
        })
        vpAdapter.setOnItemClickListener(object : OnItemClickListener<ImageItemBean> {
            override fun onClick(view: View?, position: Int, item: ImageItemBean) {
                mPopUpWindow.dismiss()
            }
        })
        mPopUpWindow.setOnDismissListener {
            val lp: WindowManager.LayoutParams = activity.window.attributes
            lp.alpha = 1.0f
            activity.window.addFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND)
            activity.window.attributes = lp
        }
    }

    fun show(parent: View){
        if (activity.isFinishing) {
            return
        }
        mPopUpWindow.width = ViewGroup.LayoutParams.MATCH_PARENT
        mPopUpWindow.height = ViewGroup.LayoutParams.WRAP_CONTENT
        mPopUpWindow.setBackgroundDrawable(ColorDrawable(0x00000000))
        mPopUpWindow.isOutsideTouchable = true
        mPopUpWindow.isFocusable = true
        mPopUpWindow.showAtLocation(parent, Gravity.BOTTOM or Gravity.CENTER_HORIZONTAL, 0, 0)
        val lp: WindowManager.LayoutParams = activity.window.attributes
        lp.alpha = 0.8f
        activity.window.addFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND)
        activity.window.attributes = lp
    }

    fun dismiss() {
        if (mPopUpWindow.isShowing) {
            mPopUpWindow.dismiss()
        }
    }
}