package com.yidian.shenghuoquan.buserorder.ui.widget

import android.content.Context
import android.util.AttributeSet
import android.view.LayoutInflater
import androidx.constraintlayout.widget.ConstraintLayout
import androidx.databinding.DataBindingUtil
import com.yidian.shenghuoquan.buserorder.R
import com.yidian.shenghuoquan.buserorder.databinding.BorderViewOrderConsigneeInfoBinding
import com.yidian.shenghuoquan.buserorder.dto.OrderShippingInfo
import com.yidian.shenghuoquan.buserorder.manager.OrderStatus
import com.yidian.shenghuoquan.buserorder.viewmodel.OrderConsigneeInfoViewModel

/**
 * author: yinjiacheng
 * date: 2021/8/21 15:15
 * description: 订单收货人信息
 */
class OrderConsigneeInfoView @JvmOverloads constructor(context: Context, attrs: AttributeSet? = null, defStyle: Int = 0) :
  ConstraintLayout(context, attrs, defStyle) {

  private val binding =
    DataBindingUtil.inflate<BorderViewOrderConsigneeInfoBinding>(
      LayoutInflater.from(getContext()),
      R.layout.border_view_order_consignee_info,
      this,
      true
    )

  /**
   * 设置收货人信息
   * @param status 订单状态 确认订单时固定传ORDER_STATUS_WAIT_PAY
   * @param info 收货人信息
   */
  fun setConsigneeInfo(status: OrderStatus?, info: OrderShippingInfo?) {
    if (status != OrderStatus.ORDER_STATUS_UNKNOWN && status != OrderStatus.ORDER_STATUS_WAIT_PAY) {
      // 订单状态除未知和未支付 收件人信息禁止修改（仅展示）
      disableModify()
    }
    binding.vm = OrderConsigneeInfoViewModel()
    binding.data = info
  }

  /**
   * 禁止修改
   */
  private fun disableModify() {
    binding.etConsigneeName.keyListener = null
    binding.etConsigneePhone.keyListener = null
  }

}
