package com.zxhl.cms.pay.view

import android.app.Activity
import android.app.Dialog
import android.util.Log
import android.view.Gravity
import android.widget.TextView
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.PagerSnapHelper
import androidx.recyclerview.widget.RecyclerView
import com.zxhl.cms.R
import com.zxhl.cms.pay.adapter.BecomeVipBannerAdapter
import com.zxhl.cms.pay.adapter.BecomeVipDialogBannerAdapter
import com.zxhl.cms.widget.CenterDialog
import com.zxhl.cms.widget.RecyclerViewPageChangeListenerHelper
import com.zxhl.cms.widget.UIndicator
import kotlinx.android.synthetic.main.activity_layout_vip_pay.*
import org.greenrobot.eventbus.EventBus

/**
 * @author (wangXuewei)
 * @datetime 2022-04-16 16:13 GMT+8
 * @detail :
 */
object VipPayDialog {
    private var mActivity: Activity? = null;
    private var mBannerAdapter: BecomeVipDialogBannerAdapter? = null
    private var mCallBack: VipPayCallBack? = null
    fun setDialogDismissCallback(callback: VipPayCallBack?) {
        this.mCallBack = callback
    }

    interface VipPayCallBack {
        fun pageClose();
        fun paySuccess();
        fun payCancel();
        fun payError();
    }

    fun showVipPayDialog(activity: Activity) {
        this.mActivity = activity;

        openPayDialog(activity)
    }

    private var mDialog: Dialog? = null

    private fun openPayDialog(act: Activity) {
        if (mDialog == null) {
            mDialog = Dialog(act!!, R.style.CenterCompatDialogTheme)
        }
        val mDialogView = CenterDialog.showBottomDialog(
            R.layout.dialog_layout_vip_pay,
            act,
            mDialog!!,
            0,
            Gravity.CENTER
        )

        val pay_banner = mDialogView.findViewById<RecyclerView>(R.id.id_rcl_pay_banner)
        val banner_indic = mDialogView.findViewById<UIndicator>(R.id.id_banner_indicator)
        val btn_continue = mDialogView.findViewById<TextView>(R.id.id_tv_vip_dialog_pay_btn)
        banner_indic.setIndicatorCount(5)


        mBannerAdapter = BecomeVipDialogBannerAdapter(act)
        pay_banner?.layoutManager = LinearLayoutManager(
            act, LinearLayoutManager.HORIZONTAL,
            false
        )
        pay_banner?.adapter = mBannerAdapter
        val mPagerSnapHelper = PagerSnapHelper()
        mPagerSnapHelper.attachToRecyclerView(pay_banner)
        pay_banner.addOnScrollListener(
            RecyclerViewPageChangeListenerHelper(mPagerSnapHelper,
                object : RecyclerViewPageChangeListenerHelper.OnPageChangeListener {
                    override fun onScrollStateChanged(recyclerView: RecyclerView?, newState: Int) {

                    }

                    override fun onScrolled(recyclerView: RecyclerView?, dx: Int, dy: Int) {

                    }

                    override fun onPageSelected(position: Int) {
                        banner_indic.setSelectPos(position)
                    }
                })
        )


        btn_continue.setOnClickListener {
            mDialog?.dismiss()
        }

        mDialog?.setOnDismissListener {
            mDialog = null
            this.mActivity = null
            mCallBack?.pageClose()
        }
        mDialog?.setCanceledOnTouchOutside(false)
        mDialog?.setCancelable(false)
        if (!act.isFinishing) {
            mDialog?.show()
            return
        }

    }


}