package com.yidian.shenghuoquan.bmanagercenter.ui.auth

import com.yidian.shenghuoquan.bmanagercenter.bean.LifeAccountMerchantAuthData
import com.yidian.shenghuoquan.bmanagercenter.bean.LifeAccountPersonalAuthData
import com.yidian.shenghuoquan.bmanagercenter.constant.Constant
import com.yidian.shenghuoquan.bmanagercenter.http.httpbean.AuthAuthenticationBean
import com.yidian.shenghuoquan.bmanagercenter.http.httpbean.AuthMerchantCheckBean
import com.yidian.shenghuoquan.bmanagercenter.http.httpbean.BusinessLicenseOCRBean
import com.yidian.shenghuoquan.bmanagercenter.http.httpbean.GetIDCardOCRBean

/**
 * author: yinjiacheng
 * date: 6/1/21 2:02 AM
 * description: 生活号认证数据管理
 */
object LifeAccountAuthDataManager {

  val personalAuthData by lazy { LifeAccountPersonalAuthData() }
  val merchantAuthData by lazy { LifeAccountMerchantAuthData() }

  /**
   * 将身份证人像面OCR结果填入
   */
  fun generateIDCardPortraitFaceData(data: GetIDCardOCRBean.Response) {
    personalAuthData.idCardPortraitFaceCompleteness = data.posit.completeness
    personalAuthData.idCardNum = data.posit.idcard_number
    personalAuthData.gender = data.posit.gender
    personalAuthData.realName = data.posit.name
    personalAuthData.address = data.posit.address
    personalAuthData.nationality = data.posit.nationality

  }

  /**
   * 将身份证国徽面OCR结果填入
   */
  fun generateIDCardNationEmblemFaceData(data: GetIDCardOCRBean.Response) {
    personalAuthData.idCardNationalEmblemFaceCompleteness = data.back.completeness
    personalAuthData.validDateStart = data.back.valid_date_start
    personalAuthData.validDateEnd = data.back.valid_date_end
    personalAuthData.issuedBy = data.back.issued_by
  }

  /**
   * 填入个人认证流程回显数据
   * 身份证两面完整度此时一定是0
   */
  fun generatePersonalAuthData(data: AuthAuthenticationBean.Response) {
    personalAuthData.isFaceAuthPass = data.live_identity_status == Constant.ALIVE_DETECT_SUCCESS
    personalAuthData.realName = data.full_name
    personalAuthData.idCardNum = data.id_card
    personalAuthData.occupation = data.occupation
    personalAuthData.idCardPortraitFaceUrl = data.posit_image_url
    personalAuthData.idCardNationalEmblemFaceUrl = data.back_image_url
    personalAuthData.address = data.address
    personalAuthData.nationality = data.nationality
    personalAuthData.gender = data.gender
    personalAuthData.validDateStart = data.validity_start_time
    personalAuthData.validDateEnd = data.validity_end_time
    personalAuthData.issuedBy = data.issued_by
    personalAuthData.isIDCardPortraitFaceUpload = true
    personalAuthData.isIDCardNationalEmblemFaceUpload = true
    personalAuthData.idCardPortraitFaceCompleteness = 0
    personalAuthData.idCardNationalEmblemFaceCompleteness = 0
  }

  /**
   * 生成身份信息校验接口请求体
   */
  fun generateAuthPersonalCheckRequest(): HashMap<String, String?> {
    val requestMap = HashMap<String, String?>()
    requestMap["posit_image_url"] = personalAuthData.idCardPortraitFaceUrl
    requestMap["back_image_url"] = personalAuthData.idCardNationalEmblemFaceUrl
    requestMap["front_completeness"] = personalAuthData.idCardPortraitFaceCompleteness.toString()
    requestMap["bak_completeness"] = personalAuthData.idCardNationalEmblemFaceCompleteness.toString()
    requestMap["id_card"] = personalAuthData.idCardNum
    requestMap["gender"] = personalAuthData.gender
    requestMap["name"] = personalAuthData.realName
    requestMap["occupation"] = personalAuthData.occupation
    requestMap["address"] = personalAuthData.address
    requestMap["nationality"] = personalAuthData.nationality
    requestMap["valid_date_start"] = personalAuthData.validDateStart
    requestMap["valid_date_end"] = personalAuthData.validDateEnd
    requestMap["issued_by"] = personalAuthData.issuedBy
    return requestMap
  }

  /**
   * 将营业执照OCR结果填入
   */
  fun generateMerchantAuthData(data: BusinessLicenseOCRBean.Response) {
    merchantAuthData.registrationName = data.name
    merchantAuthData.registrationCode = data.code
    merchantAuthData.type = data.type
    merchantAuthData.addr = data.addr
    merchantAuthData.pers = data.pers
    merchantAuthData.scope = data.scope
    merchantAuthData.time = data.time
    merchantAuthData.regi = data.regi
    merchantAuthData.organizer = data.organizer
    merchantAuthData.date = data.date
    merchantAuthData.comp = data.comp
    merchantAuthData.num = data.num
    merchantAuthData.form = data.form
    merchantAuthData.funding = data.funding
    merchantAuthData.dateIssue = data.date_issue
  }

  /**
   * 填入企业认证回显数据
   * 身份证两面完整度此时一定是0
   */
  fun generateMerchantAuthData(data: AuthMerchantCheckBean.Response) {
    merchantAuthData.authRecordId = data.enterprise_auth_record_id
    merchantAuthData.registrationName = data.name
    merchantAuthData.registrationCode = data.code
    merchantAuthData.merchantType = data.data_type
    merchantAuthData.isBusinessLicenseUpload = true
    merchantAuthData.type = data.enterprice_type
    merchantAuthData.addr = data.enterprice_address
    merchantAuthData.scope = data.businesss_scope
    merchantAuthData.time = data.term_time
    merchantAuthData.regi = data.registered_capital
    merchantAuthData.organizer = data.organizer
    merchantAuthData.date = data.establishment_date
    merchantAuthData.comp = data.registration_authority
    merchantAuthData.num = data.num
    merchantAuthData.form = data.form
    merchantAuthData.funding = data.funding
    merchantAuthData.dateIssue = data.issue_date
    merchantAuthData.businessLicenseUrl = data.business_image_url
    merchantAuthData.pers = data.pers
    personalAuthData.realName = data.legal_person
    personalAuthData.idCardNum = data.legal_id_card
    personalAuthData.phoneNum = data.mobile
    personalAuthData.isFaceAuthPass = data.live_identity_status == Constant.ALIVE_DETECT_SUCCESS
    personalAuthData.address = data.legal_address
    personalAuthData.nationality = data.legal_nationality
    personalAuthData.issuedBy = data.legal_issued_by
    personalAuthData.validDateStart = data.legal_validity_start_time
    personalAuthData.validDateEnd = data.legal_validity_end_time
    personalAuthData.idCardPortraitFaceUrl = data.posit_image_url
    personalAuthData.idCardNationalEmblemFaceUrl = data.back_image_url
    personalAuthData.isIDCardPortraitFaceUpload = true
    personalAuthData.isIDCardNationalEmblemFaceUpload = true
    personalAuthData.idCardPortraitFaceCompleteness = 0
    personalAuthData.idCardNationalEmblemFaceCompleteness = 0
  }

  /**
   * 生成营业执照上传接口请求体
   */
  fun generateAuthBusinessLicenseCommitRequest(
    lifeAccountId: String?, recordId: Long?
  ): HashMap<String, String?> {
    val requestMap = HashMap<String, String?>()
    requestMap["code"] = merchantAuthData.registrationCode
    requestMap["name"] = merchantAuthData.registrationName
    requestMap["type"] = merchantAuthData.type
    requestMap["addr"] = merchantAuthData.addr
    requestMap["pers"] = merchantAuthData.pers
    requestMap["scope"] = merchantAuthData.scope
    requestMap["time"] = merchantAuthData.time
    requestMap["regi"] = merchantAuthData.regi
    requestMap["organizer"] = merchantAuthData.organizer
    requestMap["date"] = merchantAuthData.date
    requestMap["comp"] = merchantAuthData.comp
    requestMap["num"] = merchantAuthData.num
    requestMap["form"] = merchantAuthData.form
    requestMap["funding"] = merchantAuthData.funding
    requestMap["issue_date"] = merchantAuthData.dateIssue
    requestMap["entterprise_image_url"] = merchantAuthData.businessLicenseUrl
    requestMap["data_from"] = Constant.DATA_FROM_B.toString()
    requestMap["data_type"] = merchantAuthData.merchantType.toString()
    lifeAccountId?.let { requestMap["life_account_id"] = it }
    recordId?.let { requestMap["enterprise_auth_record_id"] = it.toString() }
    return requestMap
  }

  /**
   * 生成个体工商户身份上传接口请求体
   */
  fun generateAuthIndividualBusinessIdentityCommitRequest(): HashMap<String, String?> {
    val requestMap = HashMap<String, String?>()
    requestMap["record_id"] = merchantAuthData.authRecordId.toString()
    requestMap["posit_image_url"] = personalAuthData.idCardPortraitFaceUrl
    requestMap["back_image_url"] = personalAuthData.idCardNationalEmblemFaceUrl
    requestMap["front_completeness"] = personalAuthData.idCardPortraitFaceCompleteness.toString()
    requestMap["bak_completeness"] = personalAuthData.idCardNationalEmblemFaceCompleteness.toString()
    requestMap["idcard_number"] = personalAuthData.idCardNum
    requestMap["gender"] = personalAuthData.gender
    requestMap["name"] = personalAuthData.realName
    requestMap["address"] = personalAuthData.address
    requestMap["nationality"] = personalAuthData.nationality
    requestMap["valid_date_start"] = personalAuthData.validDateStart
    requestMap["valid_date_end"] = personalAuthData.validDateEnd
    requestMap["issued_by"] = personalAuthData.issuedBy
    return requestMap
  }

  /**
   * 生成普通企业法人身份信息上传接口请求体
   */
  fun generateAuthEnterpriseLegalIdentityCommitRequest(
    lifeAccountId: String?
  ): HashMap<String, String?> {
    val requestMap = HashMap<String, String?>()
    requestMap["record_id"] = merchantAuthData.authRecordId.toString()
    requestMap["posit_image_url"] = personalAuthData.idCardPortraitFaceUrl
    requestMap["back_image_url"] = personalAuthData.idCardNationalEmblemFaceUrl
    requestMap["front_completeness"] = personalAuthData.idCardPortraitFaceCompleteness.toString()
    requestMap["bak_completeness"] = personalAuthData.idCardNationalEmblemFaceCompleteness.toString()
    requestMap["idcard_number"] = personalAuthData.idCardNum
    requestMap["gender"] = personalAuthData.gender
    requestMap["name"] = personalAuthData.realName
    requestMap["address"] = personalAuthData.address
    requestMap["nationality"] = personalAuthData.nationality
    requestMap["valid_date_start"] = personalAuthData.validDateStart
    requestMap["valid_date_end"] = personalAuthData.validDateEnd
    requestMap["issued_by"] = personalAuthData.issuedBy
    requestMap["mobile"] = personalAuthData.phoneNum
    lifeAccountId?.let { requestMap["life_account_id"] = it }
    return requestMap
  }

  /**
   * 生成个体工商户认证完成接口请求体
   */
  fun generateAuthIndividualBusinessCompleteRequest(lifeAccountId: String?): HashMap<String, String?> {
    val requestMap = HashMap<String, String?>()
    requestMap["record_id"] = merchantAuthData.authRecordId.toString()
    lifeAccountId?.let { requestMap["life_account_id"] = it }
    return requestMap
  }
}
