package com.module.backstage.activity.setting.sugarparametr

import android.view.View
import android.view.WindowManager
import androidx.activity.viewModels
import androidx.recyclerview.widget.GridLayoutManager
import com.alibaba.android.arouter.facade.annotation.Route
import com.hboxs.serialport.plc.DialogClickListener
import com.hboxs.serialport.sbc.VBoxMessage
import com.module.backstage.R
import com.module.backstage.adapter.GeneralParamAdapter
import com.module.backstage.databinding.ActivityCoreParametersBinding
import com.module.backstage.dialog.OKCancelDialog
import com.quyunshuo.androidbaseframemvvm.base.ktx.observeLiveData
import com.quyunshuo.androidbaseframemvvm.base.utils.RegisterEventBus
import com.quyunshuo.androidbaseframemvvm.common.constant.RouteUrl
import com.quyunshuo.androidbaseframemvvm.common.keyboard.KeyUtils
import com.quyunshuo.androidbaseframemvvm.common.keyboard.KeyboardView
import com.quyunshuo.androidbaseframemvvm.common.listener.AdapterClickListener
import com.quyunshuo.androidbaseframemvvm.common.ui.BaseActivity
import com.quyunshuo.androidbaseframemvvm.base.utils.LanguageUtil
import com.quyunshuo.androidbaseframemvvm.common.util.ToastUtil
import com.quyunshuo.androidbaseframemvvm.common.util.UiUtil
import dagger.hilt.android.AndroidEntryPoint
import org.greenrobot.eventbus.Subscribe
import org.greenrobot.eventbus.ThreadMode

@Route(path = RouteUrl.Home.SugarParametersActivity)
@AndroidEntryPoint
@RegisterEventBus
class SugarParametersActivity :
    BaseActivity<ActivityCoreParametersBinding, SugarParameterViewModel>() {
    private val TAG = "GeneralParamFragment"
    var okCancelDialog: OKCancelDialog? = null
    protected var keyboardView: KeyboardView? = null
    private var debugParamAdapter: GeneralParamAdapter? = null

    private var debugParamTwoAdapter: GeneralParamAdapter? = null

    private var debugParamThreeAdapter: GeneralParamAdapter? = null
    override val mViewModel: SugarParameterViewModel by viewModels<SugarParameterViewModel>()

    override fun createVB() = ActivityCoreParametersBinding.inflate(layoutInflater)


    override fun initObserve() {
        lifecycle.addObserver(mViewModel.getThreadGeneralParam())
        observeLiveData(mViewModel.readData1Success, ::readAllDataSuccess)


    }


    override fun onDestroy() {
        keyboardView?.onDestroy()
        debugParamAdapter = null
        super.onDestroy()
    }

    private fun readAllDataSuccess(data: List<Int>) {
        debugParamAdapter?.setData(data);
        debugParamTwoAdapter?.setData(data)
        debugParamThreeAdapter?.setData(data)
    }


    override fun initRequestData() {

    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    fun event(messageEvent: VBoxMessage) {
        // Log.d(TAG, "GeneralParamFragment: "+messageEvent.data)
        mViewModel.disposeData(messageEvent)
    }

    override fun ActivityCoreParametersBinding.initView() {
        LanguageUtil.setLanguage();
        mViewModel.startData()
        // 设置布局延伸到状态栏和导航栏下方
        window.addFlags(WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS)
        keyboardView = KeyboardView(mBinding.root.context)
        keyboardView!!.setDefaultUpperCase(true)
        keyboardView!!.setBackgroundResource(com.quyunshuo.androidbaseframemvvm.common.R.drawable.key_keyboard_background_shape)
        keyboardView!!.setPadding(16, 24, 16, 24)
//        keyboardView!!.addAllInputView(mBinding.root)可以不加
        KeyUtils.init(this@SugarParametersActivity, keyboardView!!)
        with(rvOtherSearch) {
            //设置布局排列方式，默认垂直排列
            val gridLayoutManager: GridLayoutManager =
                GridLayoutManager(
                    this@SugarParametersActivity,
                    1,
                    GridLayoutManager.VERTICAL,
                    false
                )
            layoutManager = gridLayoutManager
            debugParamAdapter = GeneralParamAdapter(mViewModel.getGeneralParamData())
            debugParamAdapter!!.setItemListener(object : AdapterClickListener {
                override fun onClickListener(view: View?, position: Int, data: String?) {
                    if (data == null) {
                        addAllKeyboardView(view)
                        return
                    }
                    if ("".equals(data)) {
                        ToastUtil.switchToastStyleToWarn(UiUtil.getStringRes(R.string.base_backstage_data_null_tips))
                        return
                    }
                    showOkCancelDialog(position, data)
                }

            })
            adapter = debugParamAdapter
        }
        with(generalTwo) {
            val gridLayoutManager: GridLayoutManager =
                GridLayoutManager(
                    this@SugarParametersActivity,
                    1,
                    GridLayoutManager.VERTICAL,
                    false
                )
            layoutManager = gridLayoutManager
            debugParamTwoAdapter = GeneralParamAdapter(mViewModel.getGeneralParamTwoData())
            debugParamTwoAdapter!!.setItemListener(object : AdapterClickListener {
                override fun onClickListener(view: View?, position: Int, data: String?) {
                    if (data == null) {
                        addAllKeyboardView(view)
                        return
                    }
                    if ("".equals(data)) {
                        ToastUtil.switchToastStyleToWarn(UiUtil.getStringRes(R.string.base_backstage_data_null_tips))
                        return
                    }
                    showOkCancelDialog(position, data)
                }

            })
            adapter = debugParamTwoAdapter
        }
        with(generalThree) {
            val gridLayoutManager: GridLayoutManager =
                GridLayoutManager(
                    this@SugarParametersActivity,
                    1,
                    GridLayoutManager.VERTICAL,
                    false
                )
            layoutManager = gridLayoutManager
            debugParamThreeAdapter = GeneralParamAdapter(mViewModel.getGeneralParamThreeData())
            debugParamThreeAdapter!!.setItemListener(object : AdapterClickListener {
                override fun onClickListener(view: View?, position: Int, data: String?) {
                    if (data == null) {
                        addAllKeyboardView(view)
                        return
                    }
                    if ("".equals(data)) {
                        ToastUtil.switchToastStyleToWarn(UiUtil.getStringRes(R.string.base_backstage_data_null_tips))
                        return
                    }
                    showOkCancelDialog(position, data)
                }

            })
            adapter = debugParamThreeAdapter
        }
        backLy.setOnClickListener { finish() }
    }

    private fun showOkCancelDialog(position: Int, data: String) {
        if (okCancelDialog == null) {
            okCancelDialog = this@SugarParametersActivity?.let { OKCancelDialog(it) }
        }
        okCancelDialog?.setListener(object : DialogClickListener {
            override fun onClickListener(type: Int, text: String?) {
                when (type) {
                    okCancelDialog?.type1 -> {
                        okCancelDialog?.dismiss()
                        mViewModel.sqWriteData(data)

                    }

                    else -> {}
                }
            }

        })

        okCancelDialog?.show()
    }

    fun addAllKeyboardView(view: View?): KeyboardView {
        keyboardView!!.addAllInputView(view)
        return keyboardView!!
    }
}