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.adapter.PermissionAdapter
import com.module.backstage.databinding.BackstageFragmentPermissionBinding
import com.module.backstage.vm.PermissionFragmentVM
import com.quyunshuo.androidbaseframemvvm.base.utils.SpUtils
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 PermissionFragment : BaseFragment<BackstageFragmentPermissionBinding, PermissionFragmentVM>() {
    private  val TAG = "PermissionFragment"
    private var permissionAdapter: PermissionAdapter? = null

    override val mViewModel: PermissionFragmentVM by viewModels()
    override fun createVB() = BackstageFragmentPermissionBinding.inflate(layoutInflater)


    override fun BackstageFragmentPermissionBinding.initView() {
        with(rvPayType){
            //设置布局排列方式，默认垂直排列
            val gridLayoutManager: GridLayoutManager =
                GridLayoutManager(this@PermissionFragment.context, 5, androidx.recyclerview.widget.GridLayoutManager.VERTICAL, false)
            layoutManager = gridLayoutManager
            permissionAdapter = PermissionAdapter(mViewModel.getPayData())
            adapter = permissionAdapter
        }
        tvConfirm.setOnClickListener {
            savePayType()
        }
    }

    override fun initObserve() {
    }

    override fun initRequestData() {
        mViewModel.getBanners()
        mViewModel.getArticleData()
    }
    private fun savePayType() {
        if (permissionAdapter?.arrayListState?.size!! >0){
            SpUtils.put(MMKVName.SETTING_TYPE, Gson().toJson(permissionAdapter?.arrayListState))
            Log.d(TAG, "savePayType: "+ Gson().toJson(permissionAdapter?.arrayListState))
            ToastUtil.switchToastStyleToSuccess("修改成功")
        }

    }

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

        super.onDestroyView()
    }
}