package com.module.backstage.fragment


import android.util.Log
import androidx.fragment.app.viewModels
import androidx.recyclerview.widget.GridLayoutManager
import com.google.gson.Gson
import com.module.backstage.adapter.PayAdapter
import com.module.backstage.databinding.BackstageFragmentPayBinding
import com.module.backstage.dialog.PayGoreSetDialog
import com.module.backstage.vm.PayFragmentVM
import com.quyunshuo.androidbaseframemvvm.base.utils.SpUtils
import com.quyunshuo.androidbaseframemvvm.common.bean.LocalAlarmClockBean
import com.quyunshuo.androidbaseframemvvm.common.constant.MMKVName
import com.quyunshuo.androidbaseframemvvm.common.ui.BaseFragment
import com.quyunshuo.androidbaseframemvvm.common.util.ToastUtil
import dagger.hilt.android.AndroidEntryPoint

/**
 * 支付选择Fragment
 *
 * @author Qu Yunshuo
 * @since 2021/8/1 11:46 下午
 */
@AndroidEntryPoint
class PayFragment : BaseFragment<BackstageFragmentPayBinding, PayFragmentVM>() {
    private val TAG = "PayFragment"

    private var priceAdapter: PayAdapter? = null
    override val mViewModel: PayFragmentVM by viewModels()
    override fun createVB() = BackstageFragmentPayBinding.inflate(layoutInflater)
    var payGoreSetDialog: PayGoreSetDialog? = null


    override fun BackstageFragmentPayBinding.initView() {
        with(rvPayType){
            //设置布局排列方式，默认垂直排列
            val gridLayoutManager: GridLayoutManager =
                GridLayoutManager(this@PayFragment.context, 4, androidx.recyclerview.widget.GridLayoutManager.VERTICAL, false)
            layoutManager = gridLayoutManager
            priceAdapter = PayAdapter(mViewModel.getPayData())
            adapter = priceAdapter
        }
        tvConfirm.setOnClickListener {
            savePayType()
        }
        tvGoreSet.setOnClickListener{
            showUpdateApp()
        }
    }

    private fun savePayType() {
        if (priceAdapter?.arrayListState?.size!! >0){
            SpUtils.put(MMKVName.PAY_TYPE,Gson().toJson(priceAdapter?.arrayListState))
            Log.d(TAG, "savePayType: "+Gson().toJson(priceAdapter?.arrayListState))
            ToastUtil.switchToastStyleToSuccess("修改成功")
        }

    }

    override fun initObserve() {
    }

    override fun initRequestData() {
        mViewModel.getBanners()
        mViewModel.getArticleData()
    }

    override fun onDestroyView() {
        mBinding.rvPayType.adapter = null
        super.onDestroyView()

    }

    private fun showUpdateApp() {
        if (payGoreSetDialog == null) {
            payGoreSetDialog = this@PayFragment.context?.let {
                PayGoreSetDialog(
                    it
                )
            }
            payGoreSetDialog?.setListener(object : PayGoreSetDialog.DialogClickListener {
                override fun onClickListener(type: Int, localAlarmClockBean: LocalAlarmClockBean) {

                }

            })
        }

        payGoreSetDialog?.show()

    }
}