package com.module.backstage.fragment.product

import android.view.View
import androidx.fragment.app.viewModels
import androidx.recyclerview.widget.GridLayoutManager
import com.hboxs.serialport.plc.DialogClickListener
import com.module.backstage.adapter.DebugProductAdapter
import com.module.backstage.databinding.BackstageFragmentDebugProductBinding
import com.module.backstage.dialog.OKCancelDialog
import com.module.backstage.vm.DebugProductFragmentVM
import com.quyunshuo.androidbaseframemvvm.common.listener.AdapterClickListener
import com.quyunshuo.androidbaseframemvvm.common.ui.BaseFragment
import com.quyunshuo.androidbaseframemvvm.common.util.XLogUtil
import dagger.hilt.android.AndroidEntryPoint

/**
 * 调试花型Fragment
 *
 * @author Qu Yunshuo
 * @since 2021/8/1 11:46 下午
 */
@AndroidEntryPoint
class DebugProductFragment : BaseFragment<BackstageFragmentDebugProductBinding, DebugProductFragmentVM>() {


    override val mViewModel: DebugProductFragmentVM by viewModels()
    override fun createVB() = BackstageFragmentDebugProductBinding.inflate(layoutInflater)
    private var productAdapter: DebugProductAdapter? = null
    var okCancelDialog: OKCancelDialog? = null


    override fun BackstageFragmentDebugProductBinding.initView() {
        with(homeRvProduct) {
            //设置布局排列方式，默认垂直排列
            val gridLayoutManager: GridLayoutManager =
                GridLayoutManager(this@DebugProductFragment.context, 4, androidx.recyclerview.widget.GridLayoutManager.VERTICAL, false)
            layoutManager = gridLayoutManager

            productAdapter = DebugProductAdapter(mViewModel.getPriceData())
            productAdapter!!.setItemListener(object : AdapterClickListener {
                override fun onClickListener(view: View?, position: Int, data: String?) {
                    XLogUtil.d("DebugProductFragment position:$position")
                    showOkCancelDialog(data!!)
                }
            })
            adapter = productAdapter
        }
    }

    override fun initObserve() {
    }

    override fun initRequestData() {
        mViewModel.getBanners()
        mViewModel.getArticleData()
    }
    private fun showOkCancelDialog(data: String) {
        if (okCancelDialog == null) {
            okCancelDialog = this@DebugProductFragment.context?.let { OKCancelDialog(it) }
        }
        okCancelDialog?.setListener(object: DialogClickListener {
            override fun onClickListener(type: Int, text: String?) {
                when (type) {
                    okCancelDialog?.type1-> {
                        okCancelDialog?.dismiss()
                        //是否确认操作
                        if (data == "MAKE") {
                            //开始制作的逻辑
                        }else if(data =="PIC"){
                            //切换图片
                        }
                    }
                    else -> {}
                }
            }
        })
        okCancelDialog?.setTitle("是否确定制作?")
        okCancelDialog?.show()
    }

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