package com.base.scanqrclear.bean

import com.base.scanqrclear.bean.FunctionUIBean.Companion.KEY_EMAIL
import com.base.scanqrclear.utils.QRCodeUtils.getPairKeyValue

//MATMSG:TO:xxxx;SUB:xxx;BODY:xxx;
data class EmailUIBean(
    var address: String = "",
    var subject: String = "",
    var message: String = ""
) : ScanBean(KEY_EMAIL) {
    companion object {

        fun createEmailQrString(bean: EmailUIBean): String {
            //MATMSG:TO:xxxx;SUB:xxx;BODY:xxx;
            val qrContent = StringBuilder()
            qrContent.append("MATMSG:")
            qrContent.append("TO:").append(bean.address).append(";")
            qrContent.append("SUB:").append(bean.subject).append(";")
            qrContent.append("BODY:").append(bean.message).append(";")
            return qrContent.toString()
        }

        fun isEmailQR(qrCodeValue: String): Boolean {
            val flag = qrCodeValue.startsWith("MATMSG:")
            if (flag) {
                return true
            }
            return false
        }

        fun createEmailBean(qrCodeValue: String): EmailUIBean {
            val startTag = "MATMSG:"
            val subString = qrCodeValue.subSequence(startTag.length, qrCodeValue.length)

            val splitList = subString.split(";")
            val emailUIBean = EmailUIBean()

            val addressKV = splitList.find { it.startsWith("TO:") } ?: ""
            emailUIBean.address = getPairKeyValue(addressKV).second

            val subKV = splitList.find { it.startsWith("SUB:") } ?: ""
            emailUIBean.subject = getPairKeyValue(subKV).second

            val bodyKV = splitList.find { it.startsWith("BODY:") } ?: ""
            emailUIBean.message = getPairKeyValue(bodyKV).second

            return emailUIBean
        }
    }
}