package com.yidian.shenghuoquan.newscontent.adapter

import android.content.Intent
import android.widget.ImageView
import android.widget.TextView
import androidx.core.content.ContextCompat
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.viewholder.BaseViewHolder
import com.yidian.bcommon.extensions.clickAntiShake
import com.yidian.shenghuoquan.newscontent.R
import com.yidian.shenghuoquan.newscontent.http.httpbean.GetShopListBean
import com.yidian.shenghuoquan.newscontent.ui.store.EditStoreActivity


/**
 * 选择门店的adapter
 */
class ChooseStoreApter : BaseQuickAdapter<GetShopListBean, BaseViewHolder>(R.layout.layout_choose_store_list_item) {

  override fun convert(holder: BaseViewHolder, item: GetShopListBean) {
    val position = holder.layoutPosition
    val tvStoreName = holder.getView<TextView>(R.id.tv_store_name)
    val tvStoreAddress = holder.getView<TextView>(R.id.tv_store_address)
    tvStoreName.text = item.shop_name
    tvStoreAddress.text = item.address
    val ivStoreSelect = holder.getView<ImageView>(R.id.iv_store_select)
    if (item.isSelected) {
      ivStoreSelect.background = ContextCompat.getDrawable(context, R.drawable.icon_protocol_checked)
    } else {
      ivStoreSelect.background = ContextCompat.getDrawable(context, R.drawable.icon_protocol_unchecked)
    }
    // 选择门店(增大选择触控区域，故增加点击事件)
    ivStoreSelect.clickAntiShake {
      updateSelected(item, position)
    }
    tvStoreName.clickAntiShake {
      updateSelected(item, position)
    }
    tvStoreAddress.clickAntiShake {
      updateSelected(item, position)
    }
    // 点击编辑
    holder.getView<ImageView>(R.id.iv_store_edit).clickAntiShake {
      val intent = Intent(context, EditStoreActivity::class.java)
      intent.putExtra("shop_name", item.shop_name)
      intent.putExtra("shop_id", item.shop_id)
      intent.putExtra("sub_shop_id", item.sub_shop_id)
      intent.putExtra("address", item.address)
      intent.putExtra("phone", item.phone)
      intent.putExtra("latitude", item.latitude)
      intent.putExtra("longitude", item.longitude)
      intent.putExtra("from", EditStoreActivity.fromEditStore)
      context.startActivity(intent)
    }
  }

  /**
   * 更新选中
   */
  private fun updateSelected(item: GetShopListBean, position: Int) {
    item.isSelected = !item.isSelected
    notifyItemChanged(position)
  }
}
