package com.zxbw.modulemain.adapter

import android.app.Activity
import android.text.TextUtils
import android.util.Log
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
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.JgqQyEntity
import com.zxhl.cms.utils.JumpUtils
import com.zxhl.cms.utils.OnRecycleItemClickListener
import com.zxhl.cms.utils.Utils
import java.util.ArrayList

class HomeQyBannerAdapter : PagerAdapter {


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

    constructor(
        mList: List<List<JgqQyEntity>>,
        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]
        Log.d("wxw", "data${data.size}")
        val view =
            LayoutInflater.from(mActivity).inflate(R.layout.item_layout_home_qy_grid_view, null)
        val grid_view = view.findViewById<RecyclerView>(R.id.id_rcl_home_jgq_qy_grid_list)

        var mJgqQyAdapter = JgqQyAdapter(mActivity,
            OnRecycleItemClickListener<JgqQyEntity> { view, position, data ->
                if (TextUtils.equals(data.rechargeType, "51")) {
                    JumpUtils.CardVoucherJump(data.brand)
                } else if (TextUtils.equals(data.rechargeType, "1")) {
                    if (!Constant.Switch.isOpenVip) {
                        JumpUtils.MemberOrderJump()
                    } else {
                        JumpUtils.webJump(data.brand, data.remark)
                    }
                } else {
                    JumpUtils.RightsDetailJump(data.brand, data.icon)
                }
            })
        val manager = GridLayoutManager(mActivity, 5)
        manager.isSmoothScrollbarEnabled = true;
        grid_view.setHasFixedSize(true);
        grid_view.isNestedScrollingEnabled = false;

        grid_view.layoutManager = manager
        grid_view.adapter = mJgqQyAdapter
        mJgqQyAdapter.clear()
        mJgqQyAdapter.appendToList(data)
        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
    }

}