package com.lukouguoji.module_base.model

import android.bluetooth.BluetoothAdapter
import android.content.Context
import android.view.View
import android.widget.TextView
import androidx.appcompat.widget.Toolbar
import androidx.lifecycle.MutableLiveData
import com.gprinter.bean.PrinterDevices
import com.lukouguoji.module_base.R
import com.lukouguoji.module_base.base.BaseDialogModel
import com.lukouguoji.module_base.base.BaseViewHolder
import com.lukouguoji.module_base.base.CommonAdapter
import com.lukouguoji.module_base.databinding.DialogBluetoothBinding
import com.lukouguoji.module_base.databinding.ItemBluetoothBinding
import com.lukouguoji.module_base.interfaces.IOnItemClickListener
import com.lukouguoji.module_base.ktx.addOnItemClickListener
import com.lukouguoji.module_base.ktx.commonAdapter
import com.lukouguoji.module_base.ktx.loge
import com.lukouguoji.module_base.ktx.showToast
import com.lukouguoji.module_base.util.PrinterUtils

class BluetoothDialogModel : BaseDialogModel<DialogBluetoothBinding>(DIALOG_TYPE_FULL) {

    private var callBack: (() -> Unit)? = null

    private var loading: LoadingModel? = null

    val currentBluetooth = MutableLiveData<PrinterDevices?>()

    override fun layoutId() = R.layout.dialog_bluetooth

    override fun onDialogCreated(context: Context) {
        setBackArrow("蓝牙匹配")
        binding.model = this
        loading = LoadingModel(binding.root.context)

        binding.rv.adapter = CommonAdapter(
            binding.root.context,
            R.layout.item_bluetooth,
            BluetoothViewHolder::class.java
        )

        currentBluetooth.value = PrinterUtils.portManager?.printerDevices

        PrinterUtils.searchPrinters {
            loge("回调蓝牙设备 : ${it.blueName}  ; ${binding.rv.commonAdapter() == null}")
            binding.rv.commonAdapter()?.addItem(it)
        }

        binding.rv.addOnItemClickListener(object : IOnItemClickListener {
            override fun onItemClick(position: Int, type: Int) {

                val bean = binding.rv.commonAdapter()!!.getItem(position) as PrinterDevices
                loading?.showLoading()
                PrinterUtils.connect(bean) { result ->
                    loading?.dismissLoading()
                    if (result) {
                        currentBluetooth.value = PrinterUtils.portManager?.printerDevices
                    } else {
                        showToast("蓝牙连接失败")
                    }
                }
            }
        })
    }

    private fun setBackArrow(title: String) {
        val toolbar: Toolbar = binding.root.findViewById(R.id.toolbar)
        val titleName: TextView = binding.root.findViewById(R.id.title_name)
        titleName.text = title
        val toolBack: View = binding.root.findViewById(R.id.tool_back)
        toolBack.setOnClickListener {
            dismiss()
        }
    }

    fun disconnect() {
        PrinterUtils.portManager?.closePort()
        PrinterUtils.portManager = null
        currentBluetooth.value = null
    }

    fun printClick() {
        dismiss()
        PrinterUtils.searchCallBack {
            if (it) callBack?.invoke()
        }
    }

    fun showCallBack(callBack: () -> Unit) {
        val adapter = BluetoothAdapter.getDefaultAdapter()
        if (!adapter.isEnabled) {
            showToast("请打开设备蓝牙")
            return
        }
        PrinterUtils.searchCallBack {
            if (it) {
                callBack()
            } else {
                this.callBack = callBack
                show()
            }
        }
    }
}

class BluetoothViewHolder(view: View) :
    BaseViewHolder<PrinterDevices, ItemBluetoothBinding>(view) {

    override fun onBind(item: Any?, position: Int) {
        val bean = getItemBean(item)!!
        binding.bean = bean

        notifyItemClick(position, binding.tvBtn)
    }

}