package com.yidian.shenghuoquan.border.ui.widget

import android.content.Context
import android.util.AttributeSet
import android.view.LayoutInflater
import android.view.inputmethod.EditorInfo
import androidx.constraintlayout.widget.ConstraintLayout
import androidx.databinding.DataBindingUtil
import androidx.lifecycle.LifecycleOwner
import com.yidian.shenghuoquan.border.R
import com.yidian.shenghuoquan.border.databinding.BorderViewPickupPointSearchBinding
import com.yidian.shenghuoquan.border.viewmodel.PickupPointSearchViewModel

/**
 * author: yinjiacheng
 * date: 2021/8/24 15:07
 * description: 自提点搜索框
 */
class PickupPointSearchView @JvmOverloads constructor(context: Context, attrs: AttributeSet? = null, defStyle: Int = 0) :
  ConstraintLayout(context, attrs, defStyle) {

  private val binding = DataBindingUtil.inflate<BorderViewPickupPointSearchBinding>(
    LayoutInflater.from(getContext()),
    R.layout.border_view_pickup_point_search,
    this,
    true
  )
  private val vm by lazy { PickupPointSearchViewModel() }

  init {
    binding.vm = vm
    registerUIEvent()
    initView()
  }

  private fun initView() {
    binding.etSearch.setOnEditorActionListener { v, actionId, event ->
      if (actionId == EditorInfo.IME_ACTION_SEARCH) {
        // TODO: 2021/8/24 请求搜索自提点
      }
      false
    }
  }

  /**
   * 注册UI事件监听
   */
  private fun registerUIEvent() {
    vm.clearSearch.observe(context as LifecycleOwner, {
      // 清空搜索框
      binding.etSearch.setText("")
    })
  }
}
