package com.zxbw.modulemain.adapter

import android.app.Activity
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import androidx.viewpager.widget.PagerAdapter
import com.bumptech.glide.Glide
import com.zxbw.modulemain.R
import com.zxhl.cms.common.Constant
import com.zxhl.cms.net.model.qy.HomeBannerEntity
import com.zxhl.cms.utils.JumpUtils
import com.zxhl.cms.utils.Utils
import java.util.ArrayList

class HomeBannerAdapter : PagerAdapter {


    private var mActivity: Activity? = null
    private var mViewList: List<HomeBannerEntity> = ArrayList()

    constructor(
        mList: List<HomeBannerEntity>,
        context: Activity,
    ) : super() {
        mViewList = mList //构造方法，参数是我们的页卡，这样比较方便。
        mActivity = context
    }


    override fun isViewFromObject(p0: View, p1: Any): Boolean {
        return p0 == p1
    }


    override fun instantiateItem(container: ViewGroup, position: Int): Any {
        val data = mViewList[position]
        val view = LayoutInflater.from(mActivity).inflate(R.layout.item_layout_home_banner, null)
        val bg = view.findViewById<ImageView>(R.id.id_img_banner_bg)
        if (data.bannerType.equals("blindBox")) {
            bg.setImageResource(R.drawable.img_banner_mh_bg)
        } else if (data.bannerType.equals("H5")) {
            bg.setImageResource(R.drawable.img_banner_wm_bg)
        } else {
            bg.setImageResource(R.drawable.img_banner_ms_bg)
        }
        bg.setOnClickListener {
            if (data.bannerType.equals("blindBox")) {
                if (!Constant.Switch.isOpenVip) {
                    JumpUtils.MemberOrderJump()
                }else{
                    JumpUtils.goodsDetailJump(data.boxId)
                }
            } else if (data.bannerType.equals("H5")) {
                if (!Constant.Switch.isOpenVip) {
                    JumpUtils.MemberOrderJump()
                }else{
                    JumpUtils.webJump("免费领红包", data.url)
                }
            } else {
                JumpUtils.FlashSaleJump()
            }
        }
        container.addView(view)// 添加页卡
        return view
    }

    override fun getItemPosition(`object`: Any): Int {
        return super.getItemPosition(`object`)
    }

    override fun destroyItem(container: ViewGroup, position: Int, `object`: Any) {

        container.removeView(`object` as View)
    }

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

}