package com.qmai.zqtoolkit.utils


import android.content.Context
import android.hardware.display.DisplayManager
import android.hardware.usb.UsbManager
import android.view.Display
import com.blankj.utilcode.util.DeviceUtils
import com.blankj.utilcode.util.Utils
import com.qmai.zqtoolkit.config.*


/**
 * tongzi
 * 2022/4/28
 */
object ScaleChecker {


    /**
     * 商米电子秤
     * manufacturer ： SUNMI
     */
    fun isSUNMI() = DeviceUtils.getManufacturer().lowercase().contains("sunmi")

    /**
     * 商米电子秤
     * manufacturer ： SUNMI
     */
    fun isSUNMI_S2CC() = DeviceUtils.getManufacturer().lowercase().contains("sunmi") &&
            DeviceUtils.getModel().lowercase().contains("s2_cc")


    /**
     * 中崎电子秤
     * model ：J3368    manufacturer ： rockchip
     *        C356602                  rockchip
     */
    fun isZQ() = DeviceUtils.getManufacturer().lowercase().contains("rockchip")
            && (DeviceUtils.getModel().lowercase().contains("j3368") || DeviceUtils.getModel()
        .lowercase().contains("c356602"))


    /**
     * 顶尖不是直接连接，通过 产品识别码 供应商ID
     */

    fun isAclas(): Boolean {
        var usbManager = Utils.getApp().getSystemService(Context.USB_SERVICE) as UsbManager?
        usbManager?.deviceList?.forEach {
            if (it.value.productId == ACLAS_PRODUCTID && it.value.vendorId == ACLAS_VENDORID) {
                return true
            }
        }
        return false
    }

    /**
     * 查找顶尖称的USB名称
     */
    fun getAclasPname(): String {
        var usbManager = Utils.getApp().getSystemService(Context.USB_SERVICE) as UsbManager?
        usbManager?.deviceList?.forEach {
            if (it.value.productId == ACLAS_PRODUCTID && it.value.vendorId == ACLAS_VENDORID) {
                return it.value.deviceName
            }
        }
        return ""
    }


    /**
     * 是否有副屏
     */
    fun isDoubleScreen(): Boolean {
        val mDisplayManager =
            Utils.getApp().getSystemService(Context.DISPLAY_SERVICE)
        if (mDisplayManager != null) {
            (mDisplayManager as DisplayManager).let {
                val displays = it.displays
                return (displays.size > 1 && getPresentationDisplays(displays) != null)
            }
        }
        return false
    }

    private fun getPresentationDisplays(displays: Array<Display>?): Display? {
        displays?.let {
            for (i in it.indices) {
                if (it[i].flags and Display.FLAG_SECURE != 0 && it[i]
                        .flags and Display.FLAG_SUPPORTS_PROTECTED_BUFFERS != 0 && it[i]
                        .flags and Display.FLAG_PRESENTATION != 0
                ) {
                    return it[i]
                }
            }
        }
        return null
    }

}