package com.base.scanqr.bean

import com.base.scanqr.utils.QRCodeUtils.getPairKeyValue

//MECARD:
// N:王雷;
// TEL:17608923756;
// EMAIL:179621228@qq.com;
// ADR:成都;
data class ContractUIBean(
    var n: String = "",
    var tel: String = "",
    var email: String = "",
    var adr: String = ""
) : ScanBean(FunctionUIBean.KEY_CONTACT) {

    companion object {

        fun isContractQR(qrCodeValue: String): Boolean {
            if (qrCodeValue.startsWith("MECARD:")) {
                return true
            }
            return false
        }

        fun createContractQrString(bean: ContractUIBean): String {
            val qrContent = StringBuilder()
            qrContent.append("MECARD:")
            qrContent.append("N:").append(bean.n).append("")
            qrContent.append("TEL:").append(bean.tel).append("")
            qrContent.append("EMAIL:").append(bean.email).append("")
            qrContent.append("ADR:").append(bean.adr).append("")
            return qrContent.toString()
        }

        fun createContractBean(qrCodeValue: String): ContractUIBean {
            val contractUIBean = ContractUIBean()
            val start = "MECARD:"
            val sub = qrCodeValue.subSequence(start.length, qrCodeValue.length)
            val list = sub.split(";")
            list.forEach {
                val kv = getPairKeyValue(it)
                if (kv.first == "N") {
                    contractUIBean.n = kv.second
                }
                if (kv.first == "TEL") {
                    contractUIBean.tel = kv.second
                }
                if (kv.first == "EMAIL") {
                    contractUIBean.email = kv.second
                }
                if (kv.first == "ADR") {
                    contractUIBean.adr = kv.second
                }
            }
            return contractUIBean
        }
    }
}
