package com.module.backstage.fragment


import android.util.Log
import android.view.View
import androidx.fragment.app.viewModels
import com.google.gson.Gson
import com.hboxs.serialport.plc.DialogClickListener
import com.module.backstage.R
import com.module.backstage.bean.Name
import com.module.backstage.bean.SimNetWorkResult
import com.module.backstage.databinding.BackstageFragmentErrorCheckBinding
import com.module.backstage.dialog.CheckDialog
import com.module.backstage.dialog.ErrorDialogFragment
import com.module.backstage.model.ErrorCheckTask
import com.module.backstage.vm.ErrorCheckFragmentVM
import com.quyunshuo.androidbaseframemvvm.base.ktx.observeLiveData
import com.quyunshuo.androidbaseframemvvm.base.utils.commons.Hex
import com.quyunshuo.androidbaseframemvvm.common.ui.BaseFragment
import dagger.hilt.android.AndroidEntryPoint
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch


/**
 * 故障排查 Fragment
 *
 * @author Qu Yunshuo
 * @since 2021/8/1 11:46 下午
 */
@AndroidEntryPoint
class ErrorCheckFragment : BaseFragment<BackstageFragmentErrorCheckBinding, ErrorCheckFragmentVM>(),
    View.OnClickListener {


    override val mViewModel: ErrorCheckFragmentVM by viewModels()
    override fun createVB() = BackstageFragmentErrorCheckBinding.inflate(layoutInflater)
    private var errorDialogFragment: ErrorDialogFragment? = null


    override fun BackstageFragmentErrorCheckBinding.initView() {
        addAllKeyboardView(mBinding.root)
        clNetworkCheck.setOnClickListener(this@ErrorCheckFragment)
        clSimCheck.setOnClickListener(this@ErrorCheckFragment)
        nayaxCheck.setOnClickListener(this@ErrorCheckFragment)


    }

    override fun initObserve() {
        observeLiveData(mViewModel.networkResult, ::result)
    }

    fun result(sim: SimNetWorkResult) {
        Log.d("result","resultsssssssss"+Gson().toJson(sim))
        if (checkDialog != null && checkDialog?.isShowing == true) {
            checkDialog?.setSimStatus(sim.message, sim.result)
        }

    }

    override fun initRequestData() {

        CoroutineScope(Dispatchers.Main).launch {
            mViewModel.getBanners()
        }
        mViewModel.getArticleData()
    }

    var checkDialog: CheckDialog? = null
    private fun showCheckDialog(data: Array<String>, simFunction: String) {
        if (checkDialog == null) {
            checkDialog = context?.let { CheckDialog(it, mViewModel) }
        }
        checkDialog?.setListener(object : DialogClickListener {
            override fun onClickListener(type: Int, text: String?) {
                when (type) {
                    129 -> {}
                }
            }
        })
        checkDialog?.setData(data, simFunction)
        checkDialog?.show()
    }

    /**
     * 打开购物车
     * 购物车对话框
     */
    private fun openShoppingCart(shoppingTrolleyList: List<ErrorCheckTask>) {
        if (errorDialogFragment == null) {
            errorDialogFragment = ErrorDialogFragment()
            errorDialogFragment?.setListener(object : DialogClickListener {
                override fun onClickListener(type: Int, text: String?) {

                }
            })
            lifecycle.addObserver(errorDialogFragment!!)
        }
        getParentFragmentManager().let {
            errorDialogFragment!!.show(
                it,
                "shoppingCartDialog"
            )
        }
        errorDialogFragment?.setData(shoppingTrolleyList)
    }

    override fun onDestroyView() {
        errorDialogFragment = null
        super.onDestroyView()
    }

    override fun onClick(v: View) {
        when (v.id) {
            R.id.cl_sim_check -> {
                showCheckDialog(mViewModel.getSimFunction(), Name.SIM_FUNCTION)
            }

            R.id.cl_network_check -> {
                showCheckDialog(mViewModel.getNetworkFunction(), Name.NETWORK_FUNCTION)
            }

            R.id.nayax_check -> {
                showCheckDialog(mViewModel.getNayaxFunction(), Name.NAYAX_FUNCTION)
            }
        }
    }
}